題目:利用條件運算符的嵌套來完成此題:學習成績>=90分的同學用A表示,60-89分之間的用B表示,60分以下的用C表示。
程式分析:(a>b)?a:b這是條件運算符的基本例子。
1 package com.li.FiftyAlgorthm;
2
3 import java.util.Scanner;
4
5 /**
6 * 題目:利用條件運算符的嵌套來完成此題:學習成績>=90分的同學用A表示,60-89分 之間的用B表示,60分以下的用C表示。
7 * 程式分析:(a>b)?a:b這是條件運算符的基本例子。
8 *
9 * @author yejin
10 */
11 public class Condition {
12 // public static final int S1 = 90;
13 // public static final int S2 = 60;
14 static int grade;
15
16 public static void main(String[] args) {
17 Scanner str = new Scanner(System.in);
18 int s = str.nextInt();
19 Condition c = new Condition();
20 grade = c.compare(s);
21 if (grade == 1) {
22 System.out.print('A');
23 } else if (grade == 2) {
24 System.out.print('B');
25 } else {
26 System.out.println('C');
27 }
28 }
29
30 public int compare(int s) {
31 return s > 90 ? 1 : s > 60 ? 2 : 3;
32 }
33