用Java编写一个猜数字代码,数字范围为1到100
时间: 2023-05-11 15:06:32 浏览: 80
猜数字(1到100) java
好的,以下是一个简单的Java猜数字代码,数字范围为1到100:
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
int number = (int) (Math.random() * 100) + 1;
int guess = 0;
int count = 0;
Scanner input = new Scanner(System.in);
System.out.println("猜一个1到100之间的数字:");
while (guess != number) {
guess = input.nextInt();
count++;
if (guess > number) {
System.out.println("猜大了,请再试一次:");
} else if (guess < number) {
System.out.println("猜小了,请再试一次:");
} else {
System.out.println("恭喜你,猜对了!你一共猜了" + count + "次。");
}
}
}
}
阅读全文