怎樣弄java計算成績的平均數呢?

怎樣寫出 讓使用者輸入3科成績 程式就可以算出 平均數(要整數)呢?

算出來以後 90~100 對應出來顯視為A

80~89 對應出來顯視為B

70~79 對應出來顯視為c

60~69 對應出來顯視為D

不及格為E

不可以用 if else 要用switch 不可以超過 50行

那一位大大可以教我呢^^ 拜託了 感激不盡

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    Scanner scanner = new Scanner(System.in);

    int grade1 = 0 , grade2 = 0, grade3 = 0;

    System.out.println("請輸入成績:");

    System.out.print("成績1:");

    grade1 = scanner.nextInt();

    System.out.print("成績2:");

    grade2 = scanner.nextInt();

    System.out.print("成績3:");

    grade3 = scanner.nextInt();

    int avg = (grade1 + grade2 + grade3)/3;

    System.out.println("平均為" + avg);

    int shift = avg/10;

    String judgement = "";

    switch(shift){

    case 10:

    case 9:

    judgement = "A";

    break;

    case 8:

    judgement = "B";

    break;

    case 7:

    judgement = "C";

    break;

    case 6:

    judgement = "D";

    break;

    default:

    judgement = "E";

    break;

    }

    System.out.println(judgement);

    2009-03-20 01:10:50 補充:

    只要將平均值除10,

    剩下各的部份就可以當case判斷依據

    2009-03-20 01:12:38 補充:

    若是C++的寫法

    我會用

    int condition1Result = ( 100 >= avg && 90 <= avg ) *1;

    int condition2Result = ( 89 >= avg && 89 <= avg ) *2;

    true為1, false為0,

    我可以用case的1, 2, 3, 4, 5當case判斷依據

Still have questions? Get your answers by asking now.