java用键盘输入10个数字,合法值为1,2,3,不是这3个数则为非法数字,并统计每个整数和非法数字的个数
时间: 2024-01-04 20:02:48 浏览: 32
好的,我可以帮你解决这个问题。以下是Java代码示例:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int validCount = 0;
int invalidCount = 0;
for (int i = 0; i < 10; i++) {
int num = scanner.nextInt();
if (num == 1 || num == 2 || num == 3) {
validCount++;
} else {
invalidCount++;
}
}
System.out.println("合法数字个数:" + validCount);
System.out.println("非法数字个数:" + invalidCount);
}
}
```
这段代码使用了 `Scanner` 类来读取用户输入的数字,然后使用 `if` 语句判断输入的数字是否为 1、2 或 3。如果是,则将 `validCount` 计数器加 1,否则将 `invalidCount` 计数器加 1。最后输出合法数字个数和非法数字个数。
相关推荐
![class](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.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)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)