我知道这段代码完成了我需要它为我的任务做的事情,但我不能帮助但感觉有更多......精致的方法来获得相同的结果。请注意,最后几个输出只应显示至少有一个或多于或少于0的数字
//This program will take an unspecified number of
//integers, determine how many of those integers are
//positive and how many are negative, and finally
//compute the total and average of the integers.
import java.util.Scanner;
public class exampleWork {
public static void main(String[] args) {
//create a scanner
Scanner input = new Scanner(System.in);
//create a variable to hold the positive integers
int pos = 0;
//create a variable to hold the negative integers
int neg = 0;
//create a variable to hold the total number of entries
int total = 0;
//create a variable to hold the average
float avg = 0;
//create a counter variable
int count = 0;
//prompt user to enter integer
System.out.println("Enter an integer, the input ends if it is 0: ");
int usrInput = input.nextInt();
//determine if the number is positive, negative,
//or zero, and either place in relative variables,
//or end the loop.
if (usrInput == 0)
System.out.print("Only zero was entered");
while (usrInput != 0) {
if (usrInput > 0){
pos++;
total += usrInput;
count++;
System.out.println("Enter an integer, the input ends if it is 0: ");
usrInput = input.nextInt();
} if (usrInput < 0){
neg++;
total += usrInput;
count++;
System.out.println("Enter an integer, the input ends if it is 0: ");
usrInput = input.nextInt();
}
}
if (count > 0){
avg = (total / count);
System.out.println("The number of positives is " + pos);
System.out.println("The number of negatives is " + neg);
System.out.println("The total is " + total);
System.out.println("The average is " + avg);
}
}
}
答案 0 :(得分:0)
请注意,两个if
语句中的大多数都是相同的,因此可以在if
之外提取它们而不是重复:
while (usrInput != 0) {
total += usrInput;
count++;
if (usrInput > 0) {
pos++;
} else {
neg++
}
System.out.println("Enter an integer, the input ends if it is 0: ");
usrInput = input.nextInt();
}
答案 1 :(得分:0)
您在两个主要ifs中复制了代码。这将在那部分中提取它们并将其清理一些。
while (usrInput != 0) {
if (usrInput > 0)
pos++;
else
neg++;
total += usrInput;
count++;
System.out.println("Enter an integer, the input ends if it is 0: ");
usrInput = input.nextInt();
}
[编辑其中我只做了6 XD,但现在我已经完成了]在其他一些注意事项上:
这里减少了
import java.util.Scanner;
public class exampleWork {
public static void main(String[] args) {
//create a scanner
Scanner input = new Scanner(System.in);
//create a variable to hold the positive integers
int pos = 0;
//create a variable to hold the negative integers
int neg = 0;
//create a variable to hold the total number of entries
int total = 0;
while(true) {
System.out.println("Enter an integer, the input ends if it is 0: ");
usrInput = input.nextInt();
if(usrInput==0)
break;
if (usrInput > 0)
pos++;
else
neg++;
total += usrInput;
}
int count=pos+neg;
if (count > 0){
System.out.println("The number of positives is " + pos);
System.out.println("The number of negatives is " + neg);
System.out.println("The total is " + total);
System.out.println("The average is " + ((float)total / count)); //Not sure if this typecast is correct, it's been a long time since i have worked in java
}
}
}