默认情况下,Java while循环执行三次迭代
我开始学习Java并尝试while循环,我编写了一个简单的程序,作为一个简单的猜测游戏,用户尝试猜测a和Z之间的一个字母,计算机将打印“正确”并在猜测的字母错误时重复,但是,每次我尝试运行它时,输出默认为三次迭代,我尝试将while循环更改为do-while,并得到了相同的输出,我甚至添加了int变量默认情况下,Java while循环执行三次迭代,java,while-loop,Java,While Loop,我开始学习Java并尝试while循环,我编写了一个简单的程序,作为一个简单的猜测游戏,用户尝试猜测a和Z之间的一个字母,计算机将打印“正确”并在猜测的字母错误时重复,但是,每次我尝试运行它时,输出默认为三次迭代,我尝试将while循环更改为do-while,并得到了相同的输出,我甚至添加了int变量I作为计数器,以查看它是否真的进行了3次迭代,I的值始终为4(3次错误的迭代,当我输入正确答案时,它应该是1加1) 你能告诉我我犯了什么错误吗 class Guess { public st
I
作为计数器,以查看它是否真的进行了3次迭代,I
的值始终为4(3次错误的迭代,当我输入正确答案时,它应该是1加1)
你能告诉我我犯了什么错误吗
class Guess {
public static void main (String[] args) throws java.io.IOException {
char answer = 'K', ignore, ch = 'a';
int i=0;
while (ch != 'K') {
System.out.println("I am Thinking of a letter between A and Z, can you guess it?");
ch = (char) System.in.read();
if (ch !='K')
System.out.println("Wrong ! please try again");
i++;
}
System.out.println("Correct!, you guessed after "+i+" attempts");
}
}
应该是这样的。 你的代码刚好错过了else块,当用户猜到正确的字符时,这对于跳出循环是非常必要的。我已经添加了一个
class guess {
public static void main (String[] args) throws java.io.IOException {
char answer = 'K', ignore,ch='a';
int i=0;
while (ch != answer) {
System.out.println("I am Thinking of a letter between A and Z, can you guess it?");
ch = (char) System.in.read();
i++;
if (ch !=answer) {
System.out.println("Wrong ! please try again");
}
else{
System.out.println("Correct!, you guessed after "+i+" attempts");
break;
}
}
}
}
嗨,我给你小费了。 我个人会使用扫描仪而不是System.in.read。扫描仪是读取输入的对象 要创建一个,只需提示以下内容:
Scanner sc = new Scanner(System.in); //System.in referes to a console input
要知道用户键入的内容,请使用sc.nextLine()代码>,正如我所说,它返回输入。但是,它返回的是字符串,而不是字符。因此,在本例中,还需要更改“ch”的类型
要将答案与输入进行比较,需要使用方法equals()
。基本上,如果两件事是相同的,它返回真
也就是说,您的代码应该如下所示:
Scanner sc = new Scanner(System.in);
String answer = "K",ch;
int i=0;
boolean correct = false;
while (!correct) {
System.out.println("I am Thinking of a letter between A and Z, can you guess it?");
ch = sc.nextLine();
i++;
if (!ch.toUpperCase().equals(answer)) { // or ch.equals(answer) == false
System.out.println("Wrong ! please try again");
}else{ //if they are the same
correct = true; //the answer is correct so you can leave the while loop
System.out.println("Correct!, you guessed after "+i+" attempts");
}
}
sc.close(); //you should always close a scanner after you use it, but you can ignore this step for now
注意,我还使用了一个名为toUpperCase()
的方法。该方法将字符串的所有字符转换为大写,因此,即使键入“k”而不是“k”,也会退出循环 问题是,在控制台中键入字符后按Enter键时,循环对输入的字符执行一次,对换行符执行一次,换行符为10
因此,我只是编辑了您的代码,以跳过新行,等待输入下一个字符,并且将初始提示移到循环之外。我希望此代码可以解决您的问题:
public static void main(String[] args) throws IOException {
char answer = 'K', ignore, ch = 'a';
int i = 0;
System.out.println("I am Thinking of a letter between A and Z, can you guess it?");
while (ch != 'K') {
ch = (char) System.in.read();
if(!Character.isLetter(ch)){
continue;
}
System.out.println("Wrong ! please try again");
i++;
}
System.out.println("Correct!, you guessed after " + i + " attempts");
}
首先,在循环中的每一个地方都使用“answer”代替“K”。与问题无关。请理解,正确的代码格式不是用来使代码“漂亮”,而是用来帮助您和我们进行调试。请把它格式化好,因为这对我们双方都有帮助。你的else块丢失了。很抱歉,我不明白,“如果猜到的字母是错的,计算机将打印正确的重复数”是什么意思?@JavaFan我添加了else块,它仍然表现出samethank you,这并没有完全解决问题,现在我得到了两行打印的“错!请再试一次”,i的值变为3(两行为2,正确的K为1),但我想你是对的,当我按ENTER键时循环再次执行。你确定吗,因为继续
直到System.out.println(“错误!请重试”)
所以它应该只打印一次。我已经测试了这段代码,它工作得非常完美。所以请检查你在哪里使用了继续
它应该在打印语句之前。是的,我非常确定,我复制了你的代码,但是我不断得到循环的两次迭代现在它工作起来应该非常感谢,什么呢!字符do?!Character.isLetter(ch)
检查输入的字符是否是ASCII值的字母表,对于其他字符,它将忽略循环的迭代,例如,如果输入时给出了数字、空格或enter。这是ASCII图表:我在执行代码时遇到错误,我将搜索如何使用扫描仪,并尝试一下mport scanner。我忘了提到它。在您的类之前,您需要编写:import java.util.scanner;