2016-05-11 12 views
0

私は50要素の配列を検索し、それらの要素を印刷するだけでなく、数値を見つける必要があるプロジェクトを持っています。私がそれを見つけたメッセージボックスを印刷したら!Java検索配列の番号

import java.util.*; 

public class IT145_Homework_5_4 { 

public static void main(String args[]) { 
    double alpha[] = new double[50]; 
    boolean check = false; 

    // Initialize the first 25 elements of the array (int i=0; i<25; i++) 
    for (int i = 0; i < 25; i++) { 
     alpha[i] = i * i; 
     } 

    // Initialize the last 25 elements of the array (i=25; i<50; i++) 
    for (int i = 25; i < 50; i++) { 
     alpha[i] = 3 * i; 
     } 



    // Print the element of the array 
    System.out.println("The values are: "); 

    print(alpha); 

} 

// Print method to display the element of the array 
private static void print(double m_array[]) { 
    for (int i = 1; i <= m_array.length; i++) { 
     System.out.print(m_array[i - 1] + " "); 

     if (i % 10 == 0) 
      System.out.print("\n"); 

     } 


    } 
} 
+0

変数を見つけたときにだけtrueにブールチェックを変更します。そしてそのループの中にメッセージボックスを印刷します。 –

+0

私は疲れていて、この検索をどこに置くのかわからない...私はYouTubeで見て、検索方法を見つけたが、それはこの高度なものとしてそれを表示していない... –

答えて

0
public class IT145_Homework_5_4 
{ 
    public static void main(String args[]) 
    { 

     double alpha[] = new double[50]; // all the values are whole numbers, why not make them of type int? 

     // Initialize the first 25 elements of the array (int i=0; i<25; i++) 
     for (int i = 0; i < 25; i++) 
      alpha[i] = i * i; 

     // Initialize the last 25 elements of the array (i=25; i<50; i++) 
     for (int i = 25; i < 50; i++) 
      alpha[i] = 3 * i; 

     // Print the element of the array 
     System.out.println("The values are: "); 
     print(alpha); 

     // Searches for value in array 
     double valueToFind = 100; 
     if(find(alpha, valueToFind)) 
      System.out.println(valueToFind + " is in the array"); 
     else 
      System.out.println(valueToFind + " is not in the array"); 

    } 

    // Print method to display the element of the array 
    private static void print(double m_array[]) 
    { 
     for (int i = 1; i <= m_array.length; i++) 
     { 
      System.out.print(m_array[i - 1] + "\t\t"); 
      if (i % 10 == 0) 
       System.out.print("\n"); 

     } 
    } 

    //Method to find element in array 
    private static boolean find(double m_array[], double valueToFind) 
    { 
     for(int i = 0; i < m_array.length; i++) //remember that arrays start with index 0 
      if(m_array[i] == valueToFind) 
       return true; 
     return false; 
    } 

} 
+0

それはまだ動作しません...私はvalueToFindに値を入れていますか?また、私はメッセージボックスを表示する必要があります....どのようにこのステートメントは私のためにそれを行うのですか?申し訳ありません、Javaに新しい、学習したい... –

+0

"double valueToFind = 100;" 100を検索したい値に置き換えます。メッセージボックスを表示するには、 "JOptionPane.showMessageDialog(null、"値が見つかりました/見つかりません "); "、メッセージボックスに "import javax.swing.JOptionPane;"を置くことを忘れないでください。クラスのトップに。また、Javaを歓迎します! –

0
private static void print(double m_array[]) { 
    boolean check = false; 
    int result = 0; 

    for (int i = 1; i <= m_array.length; i++) { 
     System.out.print(m_array[i - 1] + " "); 

     if (i % 10 == 0) 
      System.out.print("\n"); 

     if(m_array[i-1] == 10){ 
      result = m_array[i - 1]; 
      check = true; 
     } 
    } 

    if(check){ 
    //Print message box with value stored in "result" variable 
    } 
} 
+0

それは4しかし、それがすべての数字を印刷した後には、私はそれをしたい...単純な構文....うわ –

+0

編集編集の回答を確認 –