JAVA中的if語句的使用
在這里我教一下大家JAVA語言中IF語句的使用方法,在JAVA,if語句使用方法同其他語言相似卻又不同,在下面我給大家分析一下。
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' ");
}
}
【JAVA中的if語句的使用】相關文章:
JAVA中If語句的使用02-22
Java for循環(huán)語句使用03-31
Java中shuffle算法的使用03-05