帮我写一个java猜数字的代码并且输出之前猜的数字
时间: 2023-03-29 22:00:26 浏览: 38
猜数字游戏,Java猜数字源码
好的,以下是Java猜数字的代码,同时输出之前猜的数字:
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
int number = (int) (Math.random() * 100) + 1;
int guess = ;
int count = ;
Scanner input = new Scanner(System.in);
System.out.println("猜一个1到100之间的数字。");
while (guess != number) {
System.out.print("请输入你猜的数字:");
guess = input.nextInt();
count++;
if (guess > number) {
System.out.println("你猜的数字太大了。");
} else if (guess < number) {
System.out.println("你猜的数字太小了。");
} else {
System.out.println("恭喜你,猜对了!");
System.out.println("你一共猜了" + count + "次。");
}
System.out.println("之前猜过的数字有:");
for (int i = 1; i < count; i++) {
System.out.print(i + ": " + input.nextInt() + " ");
}
System.out.println();
}
}
}
阅读全文