猜数字游戏:
让系统随机生成一个1-100之间的随机数,循环录入猜数直到猜中为止。记录猜中的次数。
要求:当猜的数字比随机数小时:提示您猜的小了,当猜的数字比随机数大时:提示您猜的大了。 如果超过9次还未猜中,游戏也结束并提示“您太笨了,下次再加油。” (提示:使用循环和随机数)
java编程题,猜数字游戏本人初学者才学到第八章,使用循环和随机数就好了
答案:2 悬赏:10 手机版
解决时间 2021-04-23 14:52
- 提问者网友:遁入空寂
- 2021-04-23 10:29
最佳答案
- 五星知识达人网友:三千妖杀
- 2021-04-23 11:59
public static void main(String[] args) {
Random ran = new Random();
int random = ran.nextInt(100);
System.out.println("此处打出随机数,供参考,随机数为:"+random);
System.out.println("请输入你猜的数");
int i = 0;
while (i Scanner sc = new Scanner(System.in);
int guess = 0;
try {
guess = sc.nextInt();
} catch (Exception e) {
System.out.println("请输入正确的数字");
continue;
}
if(random == guess){
System.out.println("恭喜你猜中了");
break;
}
if(random i++;
System.out.println("大了");
}
if(random > guess){
i++;
System.out.println("小了")...public static void main(String[] args) {
Random ran = new Random();
int random = ran.nextInt(100);
System.out.println("此处打出随机数,供参考,随机数为:"+random);
System.out.println("请输入你猜的数");
int i = 0;
while (i Scanner sc = new Scanner(System.in);
int guess = 0;
try {
guess = sc.nextInt();
} catch (Exception e) {
System.out.println("请输入正确的数字");
continue;
}
if(random == guess){
System.out.println("恭喜你猜中了");
break;
}
if(random i++;
System.out.println("大了");
}
if(random > guess){
i++;
System.out.println("小了");
}
}
if(i>=9){
System.out.println("您太笨了,下次再加油");
}
}
Random ran = new Random();
int random = ran.nextInt(100);
System.out.println("此处打出随机数,供参考,随机数为:"+random);
System.out.println("请输入你猜的数");
int i = 0;
while (i Scanner sc = new Scanner(System.in);
int guess = 0;
try {
guess = sc.nextInt();
} catch (Exception e) {
System.out.println("请输入正确的数字");
continue;
}
if(random == guess){
System.out.println("恭喜你猜中了");
break;
}
if(random i++;
System.out.println("大了");
}
if(random > guess){
i++;
System.out.println("小了")...public static void main(String[] args) {
Random ran = new Random();
int random = ran.nextInt(100);
System.out.println("此处打出随机数,供参考,随机数为:"+random);
System.out.println("请输入你猜的数");
int i = 0;
while (i Scanner sc = new Scanner(System.in);
int guess = 0;
try {
guess = sc.nextInt();
} catch (Exception e) {
System.out.println("请输入正确的数字");
continue;
}
if(random == guess){
System.out.println("恭喜你猜中了");
break;
}
if(random i++;
System.out.println("大了");
}
if(random > guess){
i++;
System.out.println("小了");
}
}
if(i>=9){
System.out.println("您太笨了,下次再加油");
}
}
全部回答
- 1楼网友:千杯敬自由
- 2021-04-23 13:03
import java.util.*;
public class guessnumber01{
public static void main(string[] args) {
// todo 自动生成方法存根
system.out.println("欢迎进入猜数字游戏!您只有10次机会!猜的数字在0到100之间");
random r = new random();
int num = r.nextint(100);
scanner input = new scanner(system.in);
int cai;
for (int i = 0; i < 10; i++) {
system.out.print("输入竞猜数字:");
cai = input.nextint();
if (cai < 0 || cai > 100) {
system.out.println("数字在0到100之间");
continue;
}
if (cai == num) {
system.out.println("猜中数字,胜利了");
break;
}
else if (cai > num){
system.out.println("您猜的大了");
}
else{
system.out.println("您猜的小了");
}
if (i == 9) {
system.out.println("时间到,竞猜失败");
}
}
}
}
到处都有 =。=!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯