for循环(Java) [英] for loop ( Java)
本文介绍了for循环(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做一个猜谜游戏,很有趣.由于某些原因,for循环中的代码永远不会处理.我将其分为三个部分.请让我知道有人可以帮助我吗?我已经检查过,并且代码没有在for循环中继续进行.我确定for循环没有任何问题.谢谢你的感谢
I am making a guessing game for fun. For some reason, the code in the for loop never processes. I didvided it into three parts. Please let me knowCan someone help me? I have checked and the code doesnt proceed int the for loop. I am certain there is nothing wrong with the for loop. Thanks for your hwlp Thanks
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class Guess {
public static Random r = new Random();
public static BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
public static final String YES_S = "y";
public static final String NO_S = "n";
public static void main(String [] args) throws IOException {
boolean menu = true;
boolean start = false;
boolean end = false;
boolean ans = true;
boolean rand = true;
int num = -1;
int guessNum = -1;
while(menu) {
System.out.println( "Start game [ y ]:");
String input = in.readLine();
if(input.equals(YES_S)) {
menu = false;
start = true;
end = false;
}
}
while(start) {
while(ans) {
while(rand) {
num = r.nextInt(11);
rand = false;
}
for (int i = 0; i > 3; i++) {
System.out.println( " Guess a number from 0 to 10 :");
String input1 = in.readLine();
guessNum = Integer.parseInt(input1);
if (guessNum == num) {
System.out.println( " Congratulations !");
ans = false;
rand = true;
} else {
System.out.println( " Try again");
}
}
if(ans = true) {
end = true;
}
}
}
}
}
推荐答案
好吧,让我们分解一下for
循环:
Well, let's decompose your for
loop:
for (int i = 0; i > 3; i++)
- 以
i = 0
开头
- 在
i > 3
的同时做身体....哎呀,你看到问题了吗?
- Start with
i = 0
- Perform the body while
i > 3
.... whops, do you see the problem?
这篇关于for循环(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文