2015-11-12 28 views
0

我正在尝试编写一个程序,该程序在数组Data []中生成并存储20个随机数并要求用户输入他们的猜测。如果它们的数字出现在数组中,则每次出现时都会得到10分,数组将被打印,并且所有可以找到幸运数字的位置都会被打印出来。如果它不出现在数组中,我必须打印数组的最低和最高值,并只允许再尝试一次。如果玩家第二次得到正确的结果,他们每次获得1分。 例如:Data[]={3,1,8,31,25,6,3,20,41,12,3,23,7,3,65,49,5,0,13,17} ,并且如果用户输入3则程序将输出查找阵列中的数字位置并给出分数Java

​​

并且对于相同Data[]={3,1,8,31,25,6,3,20,41,12,3,23,7,3,65,49,5,0,13,17}如果用户输入2则程序将输出

Sorry your lucky number 2 is not in the array! 
Hint: Lowest Value:1 Highest Value 65 Try again with a different number in this range. 

如果他们输入65在他们的第二次尝试然后程序会输出

You get 1 point(s)! 

这是我尝试过的代码,但它不工作。它一直告诉我我的号码不在阵列中。最低和最高值工作。我不知道如何解决它。我必须在明天之前完成这件事。任何帮助将不胜感激。

String input1 = JOptionPane.showInputDialog("Please enter your lucky number between 0 and 100."); 
luck=Integer.parseInt(input1); 
System.out.println("Input " +luck); 

int Data[] = new int [20]; 
for(int i=0; i<Data.length;++i){ 
    Data[i] = (int) (Math.random() * 100); 
    System.out.print(Data[i]+ " \t"); 
} 
for(int i=0; i<Data.length;++i){ 
    if(Data[i]==luck){ 
     System.out.println(luck+ " can be found in the following positions: "); 
     System.out.println(i); 
     score=score+10; 
     System.out.println(luck+ " appears " +(luck/10)+ " times. You get " +score+ " points."); 
    }else if(Data[i]!=luck){ 
     Arrays.sort(Data); 
     System.out.println(); 
     System.out.println(luck+ " is not in the array."); 
     System.out.println("Hint: " +Data[0]+ " is the lowest value and " +Data[19]+ " is the highest."); 
     input1 = JOptionPane.showInputDialog("Please enter another number between 0 and 100."); 
     luck=Integer.parseInt(input1); 
    } 
} 
System.out.println(); 
System.out.println("Score: " +score); 

}} 

回答

3

问题是你没有检查你的值对数组中的每个值。如果输入的值与您的数组中的第一个值(i = 0)不匹配,则您要求用户输入另一个值。

你需要做的是要求一个值,将它与表中的每个值进行比较,然后做出决定。要么存在要么不存在。

我添加了一个布尔值。如果我们找到我们的号码,我们将其设置为true,否则我们会显示该消息并要求用户再次尝试。

试试这个:

boolean found = false; 
    for(int i=0; i<Data.length;++i){ 
     if(Data[i]==luck){ 
      System.out.println(luck+ " can be found in the following positions: "); 
      System.out.println(i); 
      score=score+10; 
      System.out.println(luck+ " appears " +(luck/10)+ " times. You get " +score+ " points."); 
      found = true; 
     } 
    } 
    if(!found){ 
      Arrays.sort(Data); 
      System.out.println(); 
      System.out.println(luck+ " is not in the array."); 
      System.out.println("Hint: " +Data[0]+ " is the lowest value and " +Data[19]+ " is the highest."); 
      input1 = JOptionPane.showInputDialog("Please enter another number between 0 and 100."); 
      luck=Integer.parseInt(input1); 
     } 

最后,你需要实现你的循环内的计数器,然后嘀......眼下显示你的信息,它会显示“#可以在下面找到位置“和其他消息的数组中找到每个实例。 实现一个计数器和一个方法来跟踪发现价值的指标,然后您可以从外部相干显示所有信息循环

编辑:为了让您更完整的实现... 此代码应该基本上可以你几乎在那里你需要去:

public static void main(String[] args) { 
    int Data[] = new int [20]; 
    for(int i=0; i<Data.length;++i){ 
     Data[i] = (int) (Math.random() * 100); 
     System.out.print(Data[i]+ " \t"); 
    } 

    while(true){ 
     String input1 = JOptionPane.showInputDialog("Please enter your lucky number between 0 and 100."); 
     int luck=Integer.parseInt(input1); 
     int score = 0; 
     String positions = ""; 
     int counter = 0; 
     System.out.println("Input " +luck); 

     boolean found = false; 
     for(int i=0; i<Data.length;++i){ 
      if(Data[i]==luck){ 
       positions += i + " "; 
       counter++; 
       score=score+10; 
       found = true; 
      } 
     } 
     if(found){ 
      System.out.println(luck+ " can be found in the following positions: "); 
      System.out.println(positions); 
      System.out.println(luck+ " appears " + counter + " times. You get " +score+ " points."); 

     } 
     else{ 
      Arrays.sort(Data); 
      System.out.println(); 
      System.out.println(luck+ " is not in the array."); 
      System.out.println("Hint: " +Data[0]+ " is the lowest value and " +Data[19]+ " is the highest."); 
     } 

     System.out.println(); 
     System.out.println("Score: " +score); 
    } 

} 
+0

而且它不应该说的System.out.println(运气+ “出现” +(得分/ 10)+ “次你得到” +分数+ “点”。) ; ,而不是“运气/ 10”次? –

+0

@telefunken我试过,但现在当我输入数字3时,我得到这个作为输出: Input 3 | 73 89 81 97 14 92 71 36 1 57 89 82 3 45 5 43 66 71 46 62 | 3可在以下位置找到: | 3出现0次。你得到10分。 得分:10 – bluebarry

+0

@telefunken我不知道柜台是什么。几个月前我刚开始学习代码。我如何将它添加到循环中? – bluebarry