javawhile循环从1到100的猜数字
时间: 2023-11-18 07:53:49 浏览: 31
好的,以下是代码实现:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int answer = (int) (Math.random() * 100) + 1;
int guess = 0;
while (guess != answer) {
System.out.print("请输入你猜的数字(1-100):");
guess = scanner.nextInt();
if (guess < answer) {
System.out.println("猜小了!");
} else if (guess > answer) {
System.out.println("猜大了!");
}
}
System.out.println("恭喜你,猜对了!");
}
}
```
相关问题
1到100猜数字java
以下是一个猜数字的Java程序:
import java.util.Random;
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
Random random = new Random();
int numberToGuess = random.nextInt(100) + 1; // 生成1到100之间的随机数
Scanner scanner = new Scanner(System.in);
int guess;
int tries = 0;
boolean win = false;
System.out.println("我想了一个1到100之间的数字,请你猜一猜。");
while (!win) {
System.out.print("你猜的数字是:");
guess = scanner.nextInt();
tries++;
if (guess == numberToGuess) {
win = true;
System.out.println("恭喜你,你猜对了!");
} else if (guess < numberToGuess) {
System.out.println("你猜的数字太小了,请再试一次。");
} else {
System.out.println("你猜的数字太大了,请再试一次。");
}
}
System.out.println("你一共猜了" + tries + "次。");
}
}
程序先生成一个1到100之间的随机数,然后提示用户猜数字。用户每猜一次,程序会判断猜的数字与生成的随机数的大小关系,并给出相应的提示。如果用户猜对了,程序会结束循环并输出恭喜语句及猜的次数。
java while 循环
Java中的while循环是一种重复执行代码块的结构,只要指定的条件为真,就会一直执行循环体内的代码。while循环的语法格式为:while (condition) { // 循环体 },其中condition是一个布尔表达式,如果为true,则执行循环体内的代码,否则跳过循环体。在循环体内,可以使用break语句来跳出循环,也可以使用continue语句来跳过当前循环,进入下一次循环。while循环的使用场景非常广泛,例如可以用来实现猜数字游戏、计算平均数等功能。
下面是两个使用while循环的Java程序示例:
1. 猜数字游戏
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number = (int) (Math.random() * 100);
int guess = 0;
int count = 0;
System.out.println("猜一个1到100的整数");
while (true) {
guess = in.nextInt();
if (number < guess) {
System.out.println("大了");
count++;
} else if (number > guess) {
System.out.println("小了");
count++;
} else {
count++;
break;
}
}
System.out.println("这个数字是" + number + ",您猜的次数是" + count);
}
}
2. 计算平均数
import java.util.Scanner;
public class Average {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = 0;
int number = 0;
int sum = 0;
System.out.println("请输入一些数字,以-1结束");
while (true) {
number = in.nextInt();
if (number == -1) {
break;
}
sum += number;
count++;
}
System.out.println("平均数为" + (double) (sum / count));
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)