java问题就解答
答案:2 悬赏:30 手机版
解决时间 2021-01-28 08:33
- 提问者网友:送舟行
- 2021-01-27 10:57
java问题就解答
最佳答案
- 五星知识达人网友:想偏头吻你
- 2021-01-27 12:34
具体代码如下:
import java.util.*;
public class App03 {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random(new Date().getTime());
do {
int value = random.nextInt(1000);
int i = 0;
for (i = 0; i < 8; i++) {
System.out.print("请输入一个数:");
int n = scanner.nextInt();
if (n > value) {
System.out.println("太大了!你还有" + (8 - i + 1) + "次机会。");
} else if (n < value) {
System.out.println("太小了!你还有" + (8 - i + 1) + "次机会。");
} else {
System.out.println("恭喜你猜对了!");
break;
}
}
if (i >= 8) {
System.out.println("你输了!");
}
System.out.print("是否继续(Y/N)?");
} while ("Y".equalsIgnoreCase(scanner.next()));
System.out.println("游戏结束!");
}
}
import java.util.*;
public class App03 {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random(new Date().getTime());
do {
int value = random.nextInt(1000);
int i = 0;
for (i = 0; i < 8; i++) {
System.out.print("请输入一个数:");
int n = scanner.nextInt();
if (n > value) {
System.out.println("太大了!你还有" + (8 - i + 1) + "次机会。");
} else if (n < value) {
System.out.println("太小了!你还有" + (8 - i + 1) + "次机会。");
} else {
System.out.println("恭喜你猜对了!");
break;
}
}
if (i >= 8) {
System.out.println("你输了!");
}
System.out.print("是否继续(Y/N)?");
} while ("Y".equalsIgnoreCase(scanner.next()));
System.out.println("游戏结束!");
}
}
全部回答
- 1楼网友:山河有幸埋战骨
- 2021-01-27 13:45
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
int number = (int) (Math.random() * 1000); // 产生随机数
int guess;// 用户猜的数字
int count = 0;// 猜测次数
String isEnd;
System.out.println("猜猜数字是什么?");
System.out.println(number);
// 用户猜测随机数
do {
guess = input.nextInt();
if (number < guess) {
System.out.println("大了点,再猜!");
count++;
} else if (number > guess) {
System.out.println("小了点,再猜!");
count++;
} else if (number == guess) {
System.out.println("猜对了!");
System.out.println("是否继续游戏,输入是继续,输入其他游戏结束");
isEnd = input2.next();
if (isEnd.equals("是")) {
System.out.println("游戏继续!猜猜数字是什么");
number = (int) (Math.random() * 1000);
count = 0;
continue;
} else {
System.out.println("游戏结束!");
break;
}
}
if (count == 8){
System.out.println("错误次数达到8次,游戏结束!");
break;
}
} while (true);
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯