JAVA中的if语句的使用

2025-11-11 17:56:23

1、头文件、定义函数、输出字符串、对变量赋值;定义输入的分数为“mark”,且分数会有小数

import java.util.Scanner;//头文件

class Mark{

public static void main(String[] args){

System.out.println("请输入一个分数");double mark;

Scanner scanner = new Scanner(System.in);

mark = scanner.nextDouble();

2、判断是否有输入错误。

if(mark<0||mark>100){

System.out.println("输入有误! ");

System.exit(0);

}

3、判断分数的等级

90以上A, 80~89B,70~79分C,60~69D,60以下E

if (mark>=90) System.out.println("this mark is grade \'A\' ");

else if (mark>=80) System.out.println("this mark is grade \'B\' ");

else if (mark>=70) System.out.println("this mark is grade \'C\' ");

else if (mark>=60) System.out.println("this mark is grade \'D\' ");

else  System.out.println("this mark is grade \'E\' ");

4、完整代码

import java.util.Scanner;

class Mark{

    public static void main(String[] args){

        System.out.println("请输入一个分数");

        double mark;

        Scanner scanner = new Scanner(System.in);

        mark = scanner.nextDouble();

        if(mark<0||mark>100){

           System.out.println("输入有误! ");

           System.exit(0);

        }

        if (mark>=90) System.out.println("this mark is grade \'A\' ");

        else if (mark>=80) System.out.println("this mark is grade \'B\' ");

        else if (mark>=70) System.out.println("this mark is grade \'C\' ");

        else if (mark>=60) System.out.println("this mark is grade \'D\' ");

        else  System.out.println("this mark is grade \'E\' ");

    }

}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢