我已经在这个程序上学习了几个小时,但我仍然不了解其逻辑。该程序要求用户输入整数。
一旦用户输入了整数,它将向用户输出所有正数,负数和输入的总数,以及数字的平均值。
这是我被困的地方:
然后,它将询问用户是否要继续。用户将输入'y'
或'n'
来指示他们是否愿意继续。如果用户输入“ y”,则循环将再次进行;如果用户输入“ n”,则将显示再见消息。我不知道该如何设置。
我已经尝试过do while循环,而我目前正在对该程序使用while循环。我还缺少一条语句,该语句让用户知道如果没有输入其他数字,他们只会输入零。我也不知道如何设置它,但是我尝试了if语句,但是它不起作用。我的代码如下。我正在使用Java 8。
公共类CountPositiveNegativeAndAverageOfIntsProgram {
public static void main(String [] args)
{
System.out.print("Enter an integer, the input ends if it is 0: ");
//Creating a scanner object
Scanner input = new Scanner(System.in);
int userInput = input.nextInt();
//creating variables that will be used in the program.
int countPositive = 0;
int countNegative = 0;
int total = 0;
double average = 0;
while(userInput != 0)
{
if(userInput > 0)
{
//using a counter to add how many positive numbers are entered.
countPositive++;
}
else
{
countNegative++;
}
total += userInput;
//I added 0.0 to avoid integer division. This felt like a small hack.
average = total / (countPositive + countNegative + 0.0);
userInput = input.nextInt();
}
System.out.println("The number of positives is: " + countPositive);
System.out.println("The number of negatives is: " + countNegative);
System.out.println("The total is: " + total);
System.out.println("The average is : " + average);
System.out.print("Continue? (y/n) ");
String s = input.next();
}
}
一个解决方案,当然不是一个好方法,只是创建了一个函数,当用户提供y作为响应时,您可以在其中递归调用。
public static void main(String [] args) {
request();
}
private static void request() {
System.out.println("Enter an integer, the input ends if it is 0: ");
//Creating a scanner object
Scanner input = new Scanner(System.in);
int userInput = input.nextInt();
//creating variables that will be used in the program.
int countPositive = 0;
int countNegative = 0;
int total = 0;
double average = 0;
while(userInput != 0)
{
if(userInput > 0)
{
//using a counter to add how many positive numbers are entered.
countPositive++;
}
else
{
countNegative++;
}
total += userInput;
//I added 0.0 to avoid integer division. This felt like a small hack.
average = total / (countPositive + countNegative + 0.0);
userInput = input.nextInt();
}
System.out.println("The number of positives is: " + countPositive);
System.out.println("The number of negatives is: " + countNegative);
System.out.println("The total is: " + total);
System.out.println("The average is : " + average);
System.out.println("Continue? (y/n) ");
String response = input.next();
if (response.equals("y")) {
request();
} else {
System.out.println("Goodbye!");
}
}
Java 8解决方案如果您确实想对此有所改善,则可以做一些事情。
private static void request(Scanner scanner) {
System.out.println("Enter an integer, the input ends if it is 0: ");
List<Integer> values = new ArrayList<>();
while(scanner.hasNextInt()) {
int value = scanner.nextInt();
if (value == 0) {
break;
}
values.add(value);
}
System.out.println("The number of positives is: " + values.stream().filter(value -> value > 0).count());
System.out.println("The number of negatives is: " + values.stream().filter(value -> value < 0).count());
System.out.println("The total is: " + values.stream().mapToInt(value -> value).sum());
System.out.println("The average is : " + values.stream().mapToInt(value -> value).average().orElse(0));
System.out.println("Continue? (y/n) ");
String response = scanner.next();
if (response.equals("y")) {
request(scanner);
} else {
System.out.println("Goodbye!");
}
}