初学java编程if-else语句效率低下的代码

时间:2014-01-31 05:35:20

标签: java performance if-statement

我的代码是正确的,但它看起来太大了。我想知道它是否更高效,变量更少。你有任何提示吗?谢谢你

样本输出

输入三个分数:87 42 94

最低分为:42
没有最低分的平均值是:90.5
等级是:A

<code>
import java.util.Scanner;
public class GradeAverager
{

public static void main(String[] args) 
    {
       int score1,score2,score3;
       double average,average_no_lowest;

       Scanner keyboard = new Scanner(System.in);
       System.out.print("Enter three scores: ");
       score1 = keyboard.nextInt();
       score2 = keyboard.nextInt();
       score3 = keyboard.nextInt();

       average = (score1 + score2 + score3) / 3.0;

       System.out.println();
       System.out.println("The average is: " + average);


       if (score1 < score2 && score1 < score3)
             System.out.println("The lowest score was:" + score1);
       else if (score2 < score1 && score2 < score3)
             System.out.println("The lowest score was:" + score2);
       else if (score3 < score1 && score3 < score2)
             System.out.println("The lowest score was:" + score3);



       if (score1 < score2 && score1 < score3){
             average_no_lowest = (score2 + score3)/2.0; 
             System.out.println("The average without the lowest score is: " + average_no_lowest);
             if(average_no_lowest > 90)
                  System.out.print('A');
             else if(average_no_lowest < 90 && average_no_lowest > 80 )
                  System.out.print('B');
             else if(average_no_lowest > 70 && average_no_lowest < 80)
                  System.out.print('C'); 
             else
                  System.out.print('D');
             }
       else if (score2 < score1 && score2 < score3){
             average_no_lowest = (score1 + score3)/2.0;
             System.out.println("The average without the lowest score is: " + average_no_lowest);
             if(average_no_lowest > 90)
                  System.out.print('A');
             else if(average_no_lowest < 90 && average_no_lowest > 80 )
                  System.out.print('B');
             else if(average_no_lowest > 70 && average_no_lowest < 80)
                  System.out.print('C'); 
             else
                  System.out.print('D');
             }
       else if (score3 < score1 && score3 < score2){
             average_no_lowest =(score1 + score3)/2.0;
             System.out.println("The average without the lowest score is: " + (score1 + score3)/2.0);
             if(average_no_lowest > 90)
                  System.out.print('A');
             else if(average_no_lowest < 90 && average_no_lowest > 80 )
                  System.out.print('B');
             else if(average_no_lowest > 70 && average_no_lowest < 80)
                  System.out.print('C'); 
             else
                  System.out.print('D');     
             }

    }
}
</code>

3 个答案:

答案 0 :(得分:5)

您的代码部分为

if(average_no_lowest > 90)
    System.out.print('A');
else if(average_no_lowest < 90 && average_no_lowest > 80 )
    System.out.print('B');
else if(average_no_lowest > 70 && average_no_lowest < 80)
    System.out.print('C'); 
else
    System.out.print('D');
}

重复3次。把它放进一个方法。

在经过验证的问题之前,不要担心性能问题。担心阅读和维护代码是多么容易。

代码有2个受众 - 编译器和人类。人类比编译器更重要。

答案 1 :(得分:0)

将最低分数存储在变量中,例如lowest_score。 并按平均值(总 - 最低分数)/ 2进行平均 这是最有效的

答案 2 :(得分:0)

您可以使用以下代码,这些代码行代码较少,并提供相同的输出::

   public static void main(String[] args) {
    int score1, score2, score3, status;
    double average, average_no_lowest;

    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter three scores: ");
    score1 = keyboard.nextInt();
    score2 = keyboard.nextInt();
    score3 = keyboard.nextInt();

    average = (score1 + score2 + score3) / 3;
    System.out.println("Average Score was:: " + average);

    if (score1 < score2 && score1 < score3) {
        status = 1;
        System.out.println("The lowest score was:: " + score1);
    } else if (score2 < score1 && score2 < score3) {
        status = 2;
        System.out.println("The lowest score was:: " + score2);
    } else {
        status = 3;
        System.out.println("The lowest score was:: " + score3);
    }

    if (status == 1) {
        average_no_lowest = (score2 + score3) / 2;
        System.out.println("The average without the lowest score is: "
                + average_no_lowest);
    } else if (status == 2) {
        average_no_lowest = (score1 + score3) / 2;
        System.out.println("The average without the lowest score is: "
                + average_no_lowest);
    } else {
        average_no_lowest = (score1 + score2) / 2;
        System.out.println("The average without the lowest score is: "
                + average_no_lowest);
    }

}