首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >初学者java编程if-else语句效率低下的代码

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

Stack Overflow用户
提问于 2014-01-31 13:35:20
回答 3查看 841关注 0票数 0

我的代码是正确的,但它看起来太大了。我想知道它是否可以更有效,并且使用更少的variables.Do,您有什么提示吗?谢谢

样本输出

输入三个分数: 87 42 94

最低分是: 42分

没有最低分的平均分是: 90.5

等级为:a

代码语言:javascript
复制
<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>
EN

回答 3

Stack Overflow用户

发布于 2014-01-31 13:38:52

您的代码部分如下所示

代码语言:javascript
复制
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次。将其放入一个方法中。

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

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

票数 5
EN

Stack Overflow用户

发布于 2014-01-31 13:46:12

将最低分数存储在变量中,例如lowest_score。再乘以(total - lowest_score) /2,这是最有效的

票数 0
EN

Stack Overflow用户

发布于 2014-01-31 13:48:34

您可以使用以下代码,这些代码具有更少的代码行,并根据您的需要提供相同的输出:

代码语言:javascript
复制
   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);
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21473142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档