Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用while循环_Java - Fatal编程技术网

Java 如何使用while循环

Java 如何使用while循环,java,Java,要求用户猜测一个数字,直到猜测值等于随机数。按0退出并询问用户是否希望再次播放 我的问题是,0表示退出,不起作用。如何使用while循环使其工作? 谢谢 import java.util.Random; 导入java.util.Scanner; 公共类猜测 { 公共静态void main(字符串[]args){ 字符串play=“y”; 最终int最大值=100; 整数随机数; int numberofGuess=0; 智力猜测; 布尔标志=假; 扫描仪输入=新扫描仪(System.in); R

要求用户猜测一个数字,直到猜测值等于随机数。按0退出并询问用户是否希望再次播放

我的问题是,0表示退出,不起作用。如何使用while循环使其工作? 谢谢

import java.util.Random;
导入java.util.Scanner;
公共类猜测
{
公共静态void main(字符串[]args){
字符串play=“y”;
最终int最大值=100;
整数随机数;
int numberofGuess=0;
智力猜测;
布尔标志=假;
扫描仪输入=新扫描仪(System.in);
Random rand=新的Random();
扫描仪输入是=新扫描仪(System.in);
while(playreach.equalsIgnoreCase(“y”)//do
{ 
System.out.println(“猜一个介于1和“+MAX”之间的数字);
randomNumber=rand.nextInt(最大值)+1;
numberofGuess=0;
System.out.println(“输入猜测(0退出):”;
guess=input.nextInt();
numberofGuess++;
如果(猜测>0)
如果(猜测==随机数)
{
System.out.println(“你猜到了corect”);
System.out.println(“你猜了”+numberofGuess+“次”);
}   
else if(猜测<随机数)
System.out.println(“你猜太低了。”);
else if(猜测>随机数)
System.out.println(“你猜太高了。”);
System.out.println(“您想再次播放吗?(y/n)”);
再次播放=输入yes.nextLine();
}//while(playreach.equalsIgnoreCase(“y”));
}
}       

循环将一直运行,直到Paranthesis中的表达式为false:

while (true) {
  // this will be executed forever
}

while (false) {
  // this will never be executed
}

a = 0;
while (a == 0) {
  a = 1;
  // this will be executed exactly once, because a is not equal 0 on the second run
}

guess = 1
while (guess != 0) {
  // this will executed until the user enters "0"
  readln(guess);
}

请记住,这是伪代码。它将无法编译。

第一个if和结束循环的break语句周围缺少大括号。请在提问时格式化代码
while (true) {
  // this will be executed forever
}

while (false) {
  // this will never be executed
}

a = 0;
while (a == 0) {
  a = 1;
  // this will be executed exactly once, because a is not equal 0 on the second run
}

guess = 1
while (guess != 0) {
  // this will executed until the user enters "0"
  readln(guess);
}