他们是写这段代码的更好方法吗?

时间:2016-03-06 20:15:43

标签: java

我知道这段代码完成了我需要它为我的任务做的事情,但我不能帮助但感觉有更多......精致的方法来获得相同的结果。请注意,最后几个输出只应显示至少有一个或多于或少于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);
    }   

}

}

2 个答案:

答案 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,但现在我已经完成了]在其他一些注意事项上:

  • 你不需要保持运行&#34;计数&#34;,因为它可以在主while循环之后通过添加(pos + neg)来计算。
  • 您正在添加额外的代码以进行首次试用。
  • 你平均为2个整数,这就是一个int,而不是浮点数。
  • 您不需要明确存储平均值,因为它只使用一次。

这里减少了

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
        }
    }
}