繁体   English   中英

从 if 语句打印出多行到控制台

[英]Printing out multiple lines to console from if statements

我需要帮助将 if 语句中的多行打印到控制台。 我现在的代码在下面,但是它只会打印平均考试成绩和学生得到的成绩,而不是他们前三个考试成绩的结果。 有谁知道问题是什么?

import java.util.Scanner;

public class TestScoresAndGrade {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        // Gathering info from user
        System.out.println("Enter your first test score!");
        int firstScore = keyboard.nextInt();

        System.out.println("Enter your second test score!");
        int secondScore = keyboard.nextInt();

        System.out.println("Enter your third test score!");
        int thirdScore = keyboard.nextInt();
        
        int averageTestScore = (firstScore * secondScore * thirdScore)/3;
        
        // Telling the student what grade they got on the first test
        if (firstScore >= 90) {
            System.out.println("Congrats you got an A on your first test!");
        }
        else if (firstScore < 90 && firstScore > 80) {
            System.out.println("You got a B on your first test!");
        }
        else if (firstScore < 80 && firstScore > 70) {
            System.out.println("You got a C on your first test!");
        }
        else if (firstScore < 70 && firstScore > 60) {
            System.out.println("You got a D on your first test!");
        }
        else if (firstScore < 60) {
            System.out.println("You got a F on your first test!");
        }

        // Telling student what grade they got on the second test
        if (secondScore >= 90) {
            System.out.println("Congrats you got an A on your first test!");
        }
        else if (secondScore < 90 && secondScore > 80) {
            System.out.println("You got a B on your first test!");
        }
        else if (secondScore < 80 && secondScore > 70) {
            System.out.println("You got a C on your first test!");
        }
        else if (secondScore < 70 && secondScore > 60) {
            System.out.println("You got a D on your first test!");
        }
        else if (secondScore < 60) {
            System.out.println("You got a F on your first test!");
        }

        // Telling student what they got on third test
        if (thirdScore >= 90) {
            System.out.println("Congrats you got an A on your third test!");
        }
        else if (thirdScore < 90 && thirdScore > 80) {
            System.out.println("You got a B on your third test!");
        }
        else if (thirdScore < 80 && thirdScore > 70) {
            System.out.println("You got a C on your third test!");
        } 
        else if (thirdScore < 70 && thirdScore > 60) {
            System.out.println("You got a D on your third test!");
        }
        else if (thirdScore < 60) {
            System.out.println("You got a F on your third test!");
        }
        
        //Telling a student what there average score was    
        if (averageTestScore >= 90) {
            System.out.println("Congrats your avergage is an A!");
        }
        else if (averageTestScore < 90 && averageTestScore > 80) {
            System.out.println("Your average is a B!");
        }
        else if (averageTestScore < 80 && averageTestScore > 70) {
            System.out.println("Your average is a C!");
        }
        else if (averageTestScore < 70 && averageTestScore > 60) {
            System.out.println("Your average is a D!");
        }
        else if (averageTestScore < 60) {
            System.out.println("Your average is an F!");
        }
    }
}

正如@khelwood 指出的那样,您从 if-else 块中排除了 80、70、60。 如果您可以提供测试数据的样本,我们将更好地了解正在发生的事情。

附带说明一下,

  1. 我们通常做 if-else if-...else.(按照惯例,最后一个分支是 else,除非你想故意遗漏一些情况)
  2. 就像@khelwood 所说,平均公式是错误的。 还有一件事,如果你想得到十进制,你应该 /3.0,因为你在这里做整数除以 3。
  3. 如果要提高可读性,可以考虑将重复的逻辑封装起来,使其可重用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM