题目:
利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
程序分析:
使用(a>b)?a:b条件运算
程序代码:
package com.ljy.tencent;
import java.util.Scanner;
/**
* 题目:利用条件运算符的嵌套来完成此题:
* 学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
* 程序分析:(a>b)?a:b这是条件运算符的基本例子。
* @author liaojianya
* 2016年10月2日
*/
public class ConditionCompute
{
public static void main(String[] args)
{
System.out.print("输入成绩为:");
Scanner input = new Scanner(System.in);
int score = input.nextInt();
grade(score);
input.close();
}
public static void grade(int score)
{
if(score < 0 || score > 100)
{
System.out.println("输入成绩无效!");
}
else
{
String str = (score >= 90) ? "为A等级": ((score < 60) ? "为C等级": "为B等级");
System.out.println(score + str);
}
}
}
结果输出:
输入成绩为:60
60位B等级