三元表达式


// 用三元表达式实现成绩分级(替代 if else )
import java.util.Scanner;

public class Score2{

    public static void main(String [] args){

        Scanner in = new Scanner(System.in);
        System.out.print("Please input your score:");
        double score = in.nextDouble();

        String str = score >= 90 ? "Your Level:  A"
                   : score >= 80 ? "Your Level:  B"
                   : score >= 70 ? "Your Level:  C"
                   : score >= 60 ? "Your Level:  D"
                   : "You are failed";
        System.out.print(str);

    }

}

你可能感兴趣的:(三元表达式,成绩分级)