当用户输入是时程序继续
continuation of program when user inputs yes
好的,所以我的程序工作正常,但最后它询问用户是否想输入另一组整数,如果他们输入 "yes" 过程开始超过。我的程序正在重新开始,但它保留了所有相同的用户输入,因此它只是不断重复原始输入的相同输出。
这是我的代码
import java.util.Scanner;
public class PositiveNegative {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input a list of integers (end with 0): ");
int nums = input.nextInt();
String answer;
int pos = 0;
int neg = 0;
int total = 0;
int count = 0;
double avg = 0;
do {
while (nums != 0) {
if (nums >= 1) {
pos++;
} else {
neg++;
}
total = total + nums;
count++;
avg = total * 1.0 / count;
System.out.print("Input a list of integers (End with 0): ");
nums = input.nextInt();
}
System.out.print("# of positives are: " + pos + "\n" + "# of negatives are : " + neg + "\n" + "The total: " + total + "\n" + "The average: " + avg);
System.out.print("\nWould you like to continue with new inputs? ");
answer = input.next();
} while (answer.equalsIgnoreCase("Yes"));
}
}
我当前的输出如下所示:
Input a list of integers (end with 0): 1
Input a list of integers (End with 0): 2
Input a list of integers (End with 0): -1
Input a list of integers (End with 0): 3
Input a list of integers (End with 0): 0
number of positives are: 3
number of negatives are : 1
The total: 5
The average: 1.25
Would you like to continue with new inputs? yes
number of positives are: 3
number of negatives are : 1
The total: 5
The average: 1.25
Would you like to continue with new inputs?
它应该看起来像这样:
number of positive inputs: 3
number of negative inputs: 1
The total: 5.0
The average: 1.25
Would you like to continue with new inputs? yes
Input a list of integers (end with 0): 0
No numbers were entered except 0
Would you like to continue with new inputs? no
您的变量声明应该放在 do ... while
块中,以便在用户想要继续时可以对其进行初始化。
替换
int pos = 0;
int neg = 0;
int total = 0;
int count = 0;
double avg = 0;
do{
//...
和
do {
int pos = 0;
int neg = 0;
int total = 0;
int count = 0;
double avg = 0;
//...
这样做
import java.util.Scanner;
public class PositiveNegative {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String answer;
do {
System.out.print("Input a list of integers (end with 0): ");
int nums = input.nextInt();
int pos = 0;
int neg = 0;
int total = 0;
int count = 0;
double avg = 0;
while (nums != 0) {
if (nums >= 1) {
pos++;
} else {
neg++;
}
total = total + nums;
count++;
avg = total * 1.0 / count;
System.out.print("Input a list of integers (End with 0): ");
nums = input.nextInt();
}
if(count == 0) {
System.out.print("No numbers were entered except 0");
} else {
System.out.print("# of positives are: " + pos + "\n" + "# of negatives are : " + neg + "\n" + "The total: " + total + "\n" + "The average: " + avg);
}
System.out.print("\nWould you like to continue with new inputs? ");
answer = input.next();
} while (answer.equalsIgnoreCase("Yes"));
}
}
好的,所以我的程序工作正常,但最后它询问用户是否想输入另一组整数,如果他们输入 "yes" 过程开始超过。我的程序正在重新开始,但它保留了所有相同的用户输入,因此它只是不断重复原始输入的相同输出。
这是我的代码
import java.util.Scanner;
public class PositiveNegative {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Input a list of integers (end with 0): ");
int nums = input.nextInt();
String answer;
int pos = 0;
int neg = 0;
int total = 0;
int count = 0;
double avg = 0;
do {
while (nums != 0) {
if (nums >= 1) {
pos++;
} else {
neg++;
}
total = total + nums;
count++;
avg = total * 1.0 / count;
System.out.print("Input a list of integers (End with 0): ");
nums = input.nextInt();
}
System.out.print("# of positives are: " + pos + "\n" + "# of negatives are : " + neg + "\n" + "The total: " + total + "\n" + "The average: " + avg);
System.out.print("\nWould you like to continue with new inputs? ");
answer = input.next();
} while (answer.equalsIgnoreCase("Yes"));
}
}
我当前的输出如下所示:
Input a list of integers (end with 0): 1
Input a list of integers (End with 0): 2
Input a list of integers (End with 0): -1
Input a list of integers (End with 0): 3
Input a list of integers (End with 0): 0
number of positives are: 3
number of negatives are : 1
The total: 5
The average: 1.25
Would you like to continue with new inputs? yes
number of positives are: 3
number of negatives are : 1
The total: 5
The average: 1.25
Would you like to continue with new inputs?
它应该看起来像这样:
number of positive inputs: 3
number of negative inputs: 1
The total: 5.0
The average: 1.25
Would you like to continue with new inputs? yes
Input a list of integers (end with 0): 0
No numbers were entered except 0
Would you like to continue with new inputs? no
您的变量声明应该放在 do ... while
块中,以便在用户想要继续时可以对其进行初始化。
替换
int pos = 0;
int neg = 0;
int total = 0;
int count = 0;
double avg = 0;
do{
//...
和
do {
int pos = 0;
int neg = 0;
int total = 0;
int count = 0;
double avg = 0;
//...
这样做
import java.util.Scanner;
public class PositiveNegative {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String answer;
do {
System.out.print("Input a list of integers (end with 0): ");
int nums = input.nextInt();
int pos = 0;
int neg = 0;
int total = 0;
int count = 0;
double avg = 0;
while (nums != 0) {
if (nums >= 1) {
pos++;
} else {
neg++;
}
total = total + nums;
count++;
avg = total * 1.0 / count;
System.out.print("Input a list of integers (End with 0): ");
nums = input.nextInt();
}
if(count == 0) {
System.out.print("No numbers were entered except 0");
} else {
System.out.print("# of positives are: " + pos + "\n" + "# of negatives are : " + neg + "\n" + "The total: " + total + "\n" + "The average: " + avg);
}
System.out.print("\nWould you like to continue with new inputs? ");
answer = input.next();
} while (answer.equalsIgnoreCase("Yes"));
}
}