【问题标题】:Program that asks user to input quiz scores and gives back a letter grade要求用户输入测验分数并返回字母等级的程序
【发布时间】:2020-03-06 03:56:30
【问题描述】:

我需要帮助编写一个程序,该程序要求用户输入一个满分 10 分的成绩,然后它会返回相应的字母成绩“A、B、C、D、F”我需要使用 for使用 switch 语句循环。我该如何开始?

【问题讨论】:

  • 像任何程序一样开始 - 你的主要功能!然后开始添加每一块,然后从那里开始。
  • 请分享您到目前为止所尝试的内容。它将帮助我们解决问题。

标签: java loops for-loop switch-statement


【解决方案1】:

你可以用更多 case/system.out.print 填充 switch 来完成这个 它会阻止它你输入退出

public static void main(String[] args) {
        while (true) {
            Scanner scan = new Scanner(System.in);
            String f = scan.nextLine(); //Scans user input for name
            if (f.equalsIgnoreCase("quit")){
                return;
            }
            try {
                int num = Integer.parseInt(f);
                if (num <= 10){
                    switch(num){
                        case 1:
                            System.out.println("F");
                    }
                }
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }

【讨论】:

    【解决方案2】:

    我不确定 for 循环的用途是什么。将输入存储到整数变量后,您可以对每个可用情况使用 switch 语句。为此,您将有案例 0-10。示例:

    switch (int name here){
        case 1: System.out.println("F");
        break;
        case 2: System.out.println("F");
        break;
    //Continue for each available number and the grade corresponding to it. 
    

    【讨论】:

    • 您还可以使用 if/else if 语句,这将为您节省一些编码案例 0-5 ("F")。 if (intName
    猜你喜欢
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 2017-03-25
    相关资源
    最近更新 更多