如何在同一语句中输入字母和浮点输入?

时间:2016-07-21 02:19:28

标签: java if-statement netbeans

如果用户想要接受评估分数或教师评估信,则创建程序段。打印教员的姓名和获得的评论。备注(rem)基于以下标准:(remarks.java)

SCORE/LETTER        REMARKS
4.50-5.00 or A          Outstanding
4.00-4.49 or B          Very Satisfactory
3.50-3.99 or C          Satisfactory
3.00-3.49 or D          Needs Improvement
2.99    below or E          Poor

请帮助我如何输入字母选项以添加if else语句。

截至目前,这是我的计划:

Scanner in = new Scanner (System.in);

String n;
float score;


System.out.println("Enter Faculty's Name:");
n=in.nextLine(); //asking the user to incode his/her name

System.out.println("Enter score:");
score=in.nextFloat(); //asking the user to incode score


if ( score >=4.50  )//  4.50 to 5.00
    System.out.println("Remark: Outstanding");

else if(score>=4.00)// 4.00 to 4.49
    System.out.println("Remark: Very Satisfactory");

else if(score>=3.50)// 3.50 to 3.99
    System.out.println("Remark: Satisfactory");

else if(score>=3.00)//3.00 to 3.49
    System.out.println("Remark: Needs Improvement");

else
    System.out.println("Remark: Poor");

1 个答案:

答案 0 :(得分:0)

试试这个:

    Scanner in = new Scanner (System.in);
    String n, score;

    System.out.println("Enter Faculty's Name:");
    n=in.nextLine(); //asking the user to incode his/her name

    System.out.println("Enter score:");
    score=in.nextLine(); //asking the user to incode score

    if ("ABCDE".contains(score.toUpperCase())) {
        if (score.equalsIgnoreCase("a"))//  4.50 to 5.00
            System.out.println("Remark: Outstanding");

        else if(score.equalsIgnoreCase("b"))// 4.00 to 4.49
            System.out.println("Remark: Very Satisfactory");

        else if(score.equalsIgnoreCase("c"))// 3.50 to 3.99
            System.out.println("Remark: Satisfactory");

        else if(score.equalsIgnoreCase("d"))//3.00 to 3.49
            System.out.println("Remark: Needs Improvement");

        else
            System.out.println("Remark: Poor");

    } else {
        if (Float.valueOf(score) >=4.50)//  4.50 to 5.00
        System.out.println("Remark: Outstanding");

        else if(Float.parseFloat(score) >=4.00 )// 4.00 to 4.49
            System.out.println("Remark: Very Satisfactory");

        else if(Float.parseFloat(score) >=3.50 )// 3.50 to 3.99
            System.out.println("Remark: Satisfactory");

        else if(Float.parseFloat(score) >=3.00 )//3.00 to 3.49
            System.out.println("Remark: Needs Improvement");

        else
            System.out.println("Remark: Poor");            
    }