java编程中的 if语句的嵌套问题

qq_18811621 2017-03-26 10:04:10
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示

题目说明:这是 一位老师给的一道题目
要求:1,使用三元运算符 写出此题
2,使用 if的嵌套格式 写出此题


请各路大神码字使用java语言解析,谢谢!
...全文
799 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
老大佬王 2017-07-21
  • 打赏
  • 举报
回复
作为一个新手,看到这个问题,我就当练手了。。。 rating是三元运算符,score是if嵌套。

import java.util.Scanner;

public class Test{
	
	public static void rating(int s){
		System.out.print("Your rating is ");
		String t = s>=90 ? "A" : s>=60?"B" : "C";
		System.out.println(t+". ");
	}
	
	public static void score(int a){
		System.out.print("Your lvl is ");
		if(a>=0){
            if(a>=60){
            	if(a>=90){
            		System.out.println("A. ");
            		return;
            	}
            	System.out.println("B. ");
            	return;
            }
        	System.out.println("C. ");
        }
	}
	

    public static void main(String[] args){
    	Scanner s = new Scanner(System.in);
    	System.out.print("Plz enter your score: ");
        int b = s.nextInt();
        
        rating(b);	//三元运算符
        score(b);	//if嵌套
    }
}
hang199309 2017-03-29
  • 打赏
  • 举报
回复
先自己思考写下,实在不会查下百度怎么写的,代码别人帮你写,怎么有进步?
小徐师傅 2017-03-29
  • 打赏
  • 举报
回复
先自己思考,实在写不出来,再问会比较好。
qq609737607 2017-03-28
  • 打赏
  • 举报
回复
刚才的有误 class Text3{ public static void main(String[] args) { System.out.println("请输入成绩"); Scanner sc=new Scanner(System.in); int count=sc.nextInt(); if(count>=90&&count<=100){ System.out.println("A"); System.out.println("B"); }else if(count<60&&count>0){ System.out.println("C"); } }
qq609737607 2017-03-28
  • 打赏
  • 举报
回复
if嵌套语句 class Text3{ public static void main(String[] args) { System.out.println("请输入成绩"); Scanner sc=new Scanner(System.in); int count=sc.nextInt(); if(count>=90&&count<=100){ System.out.println("A"); }else if(count>=60){ System.out.println("B"); }else if(count<60&&count>0){ System.out.println("C"); } }
qq609737607 2017-03-28
  • 打赏
  • 举报
回复
三目运算 class Text2{ public static void main(String[] args) { int count=0; System.out.println("请输入成绩"); Scanner sc=new Scanner(System.in); count=sc.nextInt(); String counts=count>=90?"A":count>=60?"B":"C"; System.out.println(counts); }
mayiaihuangluo 2017-03-27
  • 打赏
  • 举报
回复
public static void main(String[] args) {
int count=0;
Scanner sc=new Scanner(System.in);
System.out.println("请输入成绩");
count=sc.nextInt();

/* if(count<60){
System.out.println("c等级");
}else if(count<90&&count>=60){
System.out.println("b等级");
}else if(count<101&&count>=90){
System.out.println("A等级");
}else{System.out.println("输入有误,成绩为0-100,请重新输入");}
//
以下是三元运算.................................................................
变量 = 条件表达式?表达式1:表达式2;
*/
String m;
String a="a等级",b="b等级",c="c等级",d="输入有误" ;
System.out.println(m=(count>=60)?((count>90)?((count<=100)?a:d):b):((count>=0)?c:d));
}
}
qq_18811621 2017-03-27
  • 打赏
  • 举报
回复
哥们:谢谢你 辛苦 码代码,而且一个报错都没有。 我知道你 是 写了俩个 方法,然后在调用;三元的那个是这样写的,没错 但是 if的嵌套 是那样的麽???
sparta1029 2017-03-26
  • 打赏
  • 举报
回复
public class Test { public static void useIf(double score) { if (score >= 90) System.out.println("A"); else if (score >= 60) System.out.println("B"); else System.out.println("C"); } static void useTO(double score) { String result = score >= 90 ? "A" : score >= 60 ? "B" : "C"; System.out.println(result); } public static void main(String[] args) { double score; score = 95; useIf(score); useTO(score); } }

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧