我正在尝试编写一个程序,该程序在数组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);
}}
而且它不应该说的System.out.println(运气+ “出现” +(得分/ 10)+ “次你得到” +分数+ “点”。) ; ,而不是“运气/ 10”次? –
@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
@telefunken我不知道柜台是什么。几个月前我刚开始学习代码。我如何将它添加到循环中? – bluebarry