JAVA中的if語(yǔ)句的使用
在這里我教一下大家JAVA語(yǔ)言中IF語(yǔ)句的使用方法,在JAVA,if語(yǔ)句使用方法同其他語(yǔ)言相似卻又不同,在下面我給大家分析一下。
1、頭文件、定義函數(shù)、輸出字符串、對(duì)變量賦值;定義輸入的分?jǐn)?shù)為“mark”,且分?jǐn)?shù)會(huì)有小數(shù)
import java.util.Scanner;//頭文件
class Mark{
public static void main(String[] args){
System.out.println("請(qǐng)輸入一個(gè)分?jǐn)?shù)");double mark;
Scanner scanner = new Scanner(System.in);
mark = scanner.nextDouble();
2、判斷是否有輸入錯(cuò)誤。
if(mark<0||mark>100){
System.out.println("輸入有誤! ");
System.exit(0);
}
3、判斷分?jǐn)?shù)的等級(jí)
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("請(qǐng)輸入一個(gè)分?jǐn)?shù)");
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' ");
}
}
【JAVA中的if語(yǔ)句的使用】相關(guān)文章:
JAVA中If語(yǔ)句的使用02-22
Java中synchronized的使用實(shí)例05-31
Java中shuffle算法的使用03-05
講解Java編程中finally語(yǔ)句的使用方法08-11
Java for循環(huán)語(yǔ)句使用實(shí)例01-13
關(guān)于Java for循環(huán)語(yǔ)句使用07-19