Java——小程序練習一

編寫程序:由鍵盤輸入給出一個百分制成績,要求輸出成績等級’A’、’B’、’C’和’D’,90分以上爲’A’,75~89爲’B’,60~74爲’C’,60分以下爲’D’。

最開始寫的方法:沒有注意到百分制的限定,缺少條件分析,思考不到位。

public class Test01 {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int score = scanner.nextInt();
    if (score>=90) {
      System.out.println("A");
    }else if (score>=75 && score<=89) {
      System.out.println("B");
    }else if (score>=60 && score<=74) {
      System.out.println("C");
    }else if (score<60) {
      System.out.println("D");
    }
  }
}

改正後:

public class Test01 {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int score = scanner.nextInt();
    if (score>=90&&score<=100) {
      System.out.println("A");
    }else if (score>=75 && score<=89) {
      System.out.println("B");
    }else if (score>=60 && score<=74) {
      System.out.println("C");
    }else if (score<60&&score>=0) {
      System.out.println("D");
    }else{
      System.out.println("很顯然,這成績是假滴!");
    }
  }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章