用intellijidea从键盘输入0结束,统计输入的正数,负数的个数,要求用do while
时间: 2024-09-28 19:07:12 浏览: 43
在IntelliJ IDEA中,你可以使用`do-while`循环结合键盘输入和条件判断来实现这个功能。以下是一个简单的示例代码:
```java
import java.util.Scanner;
public class NumberCounter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int positiveCount = 0;
int negativeCount = 0;
do {
System.out.println("请输入一个数字(输入0结束):");
double input = scanner.nextDouble();
if (input > 0) {
positiveCount++;
} else if (input < 0) {
negativeCount++;
}
} while (input != 0);
scanner.close();
System.out.println("正数有:" + positiveCount + "个,负数有:" + negativeCount + "个");
}
}
```
在这个程序中,我们创建了一个`Scanner`对象来获取用户的输入,然后在一个`do-while`循环内持续请求用户输入。如果输入的是正数,我们就增加正数计数器;如果是负数,就增加负数计数器。当用户输入0时,`do-while`条件不再满足,跳出循环。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)