我对编程很新,并且遇到了我想出的程序问题。这是一个猜谜游戏,用户必须猜测生成的随机数,你有6次机会。不幸的是,我不太明白这个程序有什么问题。每当我尝试输入"是"启动程序,没有任何反应。下面列出的是代码,请让我知道我做错了什么。
import java.util.Scanner;
import java.util.Random;
public class Project4{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play a game? (Yes or No)");
String a1 = input.next().toLowerCase();
while(a1.equals("Yes")){
Random secretnum = new Random();
int num;
for(int counter=0; counter<2; counter++){
num = 1+secretnum.nextInt(99);
System.out.println("You have 6 chances to guess the secret number");
System.out.println("Player : guess a number between 1 and 100");
int num1 = input.nextInt();
while(num1 != num){
for(int i = 6; i < 0; i--){
if(num1 < num){
System.out.println("Sorry," + num1 + "is too low");
System.out.println("You have" + i + "strikes left");
}else{
if(num1 > num){
System.out.println("Sorry," + num1 + "is too high");
System.out.println("You have" + i + "strikes left");
}
}
if(num1 == num){
System.out.println("Correct! The secret number was" + secretnum);
}
}
if(num1 < 0 || num1 > 100){
for(int i = 6; i < 0; i--){
System.out.println("You were supposed to enter a number between 1 and 100");
System.out.println("You have" + i + "strikes left");
}
}
}
}
if(a1.equals("No"));
System.out.println("Goodbye");
}
input.close();
}
}
答案 0 :(得分:1)
你的问题在这里:
String a1 = input.next().toLowerCase();
while(a1.equals("Yes")){
第一行将任何输入转换为小写,并将该操作的结果存储到 a1 。
第二行检查 a1 是否等于&#34;是&#34;。提示:小写字符串几乎不能等于&#34; Y es&#34;&#34;。这是预先降低输入量的重点!
这里真正的答案是:当你不熟悉这些电话的作用时,请研究他们的javadoc!