简体   繁体   English

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

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

I need help printing multiple lines from if statements to the console.我需要帮助将 if 语句中的多行打印到控制台。 The code I have now is below however it will only print the average test score and what the student got but not the results on their first three test scores.我现在的代码在下面,但是它只会打印平均考试成绩和学生得到的成绩,而不是他们前三个考试成绩的结果。 Does anyone know what the problem is?有谁知道问题是什么?

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!");
        }
    }
}

As @khelwood points out, you are excluding 80, 70, 60 from your if-else blocks.正如@khelwood 指出的那样,您从 if-else 块中排除了 80、70、60。 If you can provide a sample of your test data, we will have a better idea what it is happening.如果您可以提供测试数据的样本,我们将更好地了解正在发生的事情。

On a side note,附带说明一下,

  1. We usually do if-else if-...else.(by convention last branch is else, unless you want to leave out some cases intentionally)我们通常做 if-else if-...else.(按照惯例,最后一个分支是 else,除非你想故意遗漏一些情况)
  2. like @khelwood said, the average formula is wrong.就像@khelwood 所说,平均公式是错误的。 One more thing, you should /3.0 if you want to get decimal since you are doing integer division here by 3.还有一件事,如果你想得到十进制,你应该 /3.0,因为你在这里做整数除以 3。
  3. If you want to improve the readability, think of encapsulating the duplicated logic and make it reusable.如果要提高可读性,可以考虑将重复的逻辑封装起来,使其可重用。

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

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