package com.mhm;
import java.util.Scanner;
public class StudentGrade {
public static void main(String[] args) {
System.out.println("Enter your grade: ");
Scanner in = new Scanner(System.in);
String grade = in.nextLine();
//a program that prompts the user to enter the grade for student and show up a massege for him
1-如果他寫了 A 優秀 2-如果他寫 B 優秀 3-如果他寫 C 很好 4-如果他寫 D 可以做得更好 5-如果他寫 f 失敗!如果用戶輸入了另一個等級,則寫入無效等級
switch (grade) {
case "A":
System.out.println("Excellent.");
break;
case "B":
System.out.println("OutStanding.");
break;
case "C":
System.out.println("Good");
break;
case "D":
System.out.println("Can Do Better ");
break;
case "F":
System.out.println("Failed !");
break;
default:
System.out.println("invalid grade ");
}
}
}
uj5u.com熱心網友回復:
一般來說,有幾種方法可以解決這個問題,這取決于您喜歡遞回的程度。但是這里有一個迭代選項,它通過設定一個關于您是否應該繼續回圈的標志來作業。
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
boolean flag = true; //We will set this variable to false in order to indicate that we should exit the loop.
while (flag) {
System.out.print("Enter your grade: ");
String grade = in.nextLine();
switch (grade) {
//As DevilsHnd notes, you might also want to use 'switch (grade.toUpperCase()) {' so that your program is case-insensitive.
case "A":
System.out.println("Excellent!");
flag = false; //This indicates we should exit the loop.
break;
case "B":
System.out.println("Outstanding!");
flag = false; //This also indicates we should exit the loop.
break;
/**
* More cases go here
*/
default:
System.out.println("Sorry, I didn't understand that. Could you try again?");
//Note that we're not setting flag to false this time. This is because we are not exiting the loop.
}
}
in.close(); //Don't forget to close your resources!
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/347908.html
