java程序随机生成一个数 判断输入的数字和随机数关系 直到相等

import java.util.Scanner;
import java.util.Random;
public class suijishu {
public static void main(String[] args) {
Random no = new Random();
int a=no.nextInt(101);
int b;
do
{System.out.println("please input the number you guess");
Scanner in=new Scanner(System.in);
b=in.nextInt();
if (b<a)
System.out.println("the number is too small please input again!");
if (b>a)
System.out.println("the number is too big please input again!");
if (b==a)
System.out.println("congratulations!");}
while (b==a);
}

}
为什么无法执行循环?

这个就是个简单的猜数字游戏。可以参考下以下完整代码:


import javax.swing.Icon;
import javax.swing.JOptionPane;

public class CaiShuZi4JOptionPane {

/**
 * @param args
 */
public static void main(String[] args) {
Icon icon = null;
boolean bl = false;
int put = 0;
int c = (int) (((Math.random())*100)+1);  //获取一个1-100的随机数
System.out.println("你获取的随机数是:"+c);  //打印你的随机数字

String str1  = (String) JOptionPane.showInputDialog(null,"请输入你的猜测数字(1-100):\n","猜数字游戏",JOptionPane.PLAIN_MESSAGE,icon,null,"在这输入"); //第一次输入你的猜测数字

if(str1==null){
JOptionPane.showMessageDialog(null, "你已经取消了本次游戏"); //如果你点取消那么本次游戏结束
}else{
bl = num(str1);  //判断是输入的是不是数字或者是整数
if(true==bl){      //如果是数字的话进入与随机数比较的程序
System.out.println("你输入的数字是:"+str1);  //打印你输入的数字
 put = Integer.valueOf(str1);   
    
for(int i = 3;i > 0;i--){    //i是你可以猜测的次数
 if(put==c){
JOptionPane.showMessageDialog(null, "恭喜你猜对了,正确答案是:"+c+"。");  //如果你猜对了就直接结束循环
break;
 }else if(put>c){  //如果输大了就让你再次从新输入
 str1  = (String) JOptionPane.showInputDialog(null,"你的输入过大。你还有"+i+"次机会,请重新输入:\n","猜数字游戏",JOptionPane.PLAIN_MESSAGE,icon,null,"在这输入"); 
 if(str1==null){
 JOptionPane.showMessageDialog(null, "你已经取消了本次输入"); 
 break;
}else{
 bl =num(str1);
               if(true==bl){
      put = Integer.valueOf(str1);
                }else{
                JOptionPane.showMessageDialog(null, "你的输入不正确,请重新输入"); 
                }
}
 }else if(put<c){  //如果你输小了也让你从新输入
str1  = (String) JOptionPane.showInputDialog(null,"你的输入过小。你还有"+i+"次机会,请重新输入:\n","猜数字游戏",JOptionPane.PLAIN_MESSAGE,icon,null,"在这输入"); 
 if(str1==null){
 JOptionPane.showMessageDialog(null, "你已经取消了本次游戏"); 
 break;
}else{
         bl =num(str1);
                   if(true==bl){
         put = Integer.valueOf(str1);
                   }else{
                     JOptionPane.showMessageDialog(null, "你的输入不正确,请重新输入"); 
                    }
      }
}
}


}else if(bl==false){   //这个 是你第一次如果填写的不是数字的话也会结束本次游戏
JOptionPane.showMessageDialog(null, "请您下次按要求填写。本次游戏结束"); 
}
if(true==bl && c!=put){  //如果你i次都没猜对,那么就直接告诉你这个数十什么
 JOptionPane.showMessageDialog(null, "很遗憾你没能猜对,这个数字是:"+c+".");  
 }

  } 
 
}

    public  static boolean num(String value){  //一个静态方法,判断你输入的是不是数字
   try {
Integer.parseInt(value);
return true;
} catch (Exception e) {
return false;
}
   
   }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-06
条件是b!=a才要继续循环~
做了下调整,有前后括号的尽量括起来,这是编码习惯,方便调试和改错。
你还可以定义一个计数器,当猜测次数超过5次或者10次的时候结束游戏。
public static void main(String[] args) {
Random no = new Random();
int a=no.nextInt(101);
int b;
do{
System.out.println("please input the number you guess");
Scanner in=new Scanner(System.in);
b=in.nextInt();
if (b<a){
System.out.println("the number is too small please input again!");
}
if (b>a){
System.out.println("the number is too big please input again!");
}
if (b==a){
System.out.println("congratulations!");
break;
}
}while(b!=a);

}本回答被提问者采纳
第2个回答  2011-11-04
while (b!=a)
相似回答