用Java编程实现一个猜数字的游戏:系统随机产生一个1~100的数字,然后让玩家猜测这个数字,如果玩家猜错,

如题所述

1.int num = (int)(Math.random()*100+1);//随机得出一个1~100的数
2.用一个变量去接收玩家输入的数,例如int i;
3.用if语句,当玩家输入的数与随机数相等时(i==num),则输出语句(恭喜你猜对了).
4.用else写出猜错是的语句.
大致思路就是这样.建议你自己去写,只有自己写过才会懂.如有不懂的M我,本人也在学习中,大家可以相互讨论讨论.另外你可以把数字设置成1~10;这样的话比较容易出现猜对的情况,方便你验证程序是否正确,然后再改成1~100;
最后在说下Math.random(),这个方法得到是一个0~1之间的随机小数(不包括1但包括0),所以(0~1)*100=0~100(但是不包括100),然后+1就得出1~100了;因为Math.random()是个小数所以用int强转.
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-02-28
import java.util.Scanner;

public class guess {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个1~100之间的整数!");
int j = (int) (Math.random() * 100 + 1);
while (true) {
int i = scan.nextInt();
if (i > j) {
System.out.println("您猜的数大了");
System.out.println("请继续输入!");
} else if (i < j) {
System.out.println("您猜的数小了");
System.out.println("请继续输入!");
} else{
System.out.println("恭喜您猜对了");
System.out.println("这个数是:"+i);
break;
}
}
}
}本回答被网友采纳
第2个回答  2011-03-31
public static void main(String[] args) throws Exception{
/*判断*/
Random r = new Random(100);
int ranbow = r.nextInt(100);
System.out.print("请输入您猜的数字吧:");

while(1==1){
if(startGame(ranbow)){
break;
}
}

}
public static Boolean startGame(int random) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String strNum = reader.readLine();
if(!strNum.equals(String.valueOf(random))){
System.out.print("猜错啦!继续吧");
return false;
}else{
System.out.print("恭喜,对啦!");
return true;
}
}
第3个回答  2011-03-31
你是不是没有写完啊。1-100间的随机数字:int num1=(int)(Math.random()*100+1);
第4个回答  2011-03-31
int n=100;
int j=0;
while(j<n)
{
System.out.print(rand.nextInt());
j++;
}

试试

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网