首页 > 编程语言 > 详细

java有关switch语句运用

时间:2015-03-26 17:45:01      阅读:337      评论:0      收藏:0      [点我收藏+]

我将以下小例子为大家讲述。

技术分享

import java.util.Scanner;
public class text1{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
double i;
i=sc.nextDouble();
switch((int)(i/10)){
case 10:System.out.println("A");break;
case 9:System.out.println("A");break;
case 8:System.out.println("B");break;
case 7:System.out.println("C");break;
case 6:System.out.println("D");break;
default:System.out.println("E");break;
}
}
}

由于switch之后的括号内只能是整型(byte、short、char、int)或字符型表达式,不能是长整型long或其他类型,所以要把i强制类型转化(int)(i/10)。


技术分享


import java.util.Scanner;
public class text2{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
if(i<0||i>100) System.out.println("该数不符合要求");
else if(i>=65) System.out.println("老年");
else if(i>=41) System.out.println("中年");
else if(i>=18) System.out.println("青年");
else if(i>=7)  System.out.println("少年");
else System.out.println("童年");

}
}


根据等级判断成绩。


public class text3 {
public static void main(String[] args)throws Exception {
     char ch;
     ch=(char)System.in.read();
     switch(ch){
     case ‘A‘:System.out.println(" 90~100"); break;
case ‘B‘:System.out.println("80~89"); break;
case ‘C‘:System.out.println(" 70~79"); break;
case ‘D‘:System.out.println(" 60~69"); break;
case ‘E‘:System.out.println(" <60"); break;
default:System.out.println(" 错误的输入。"); break;
     }
}
}


java有关switch语句运用

原文:http://blog.csdn.net/qq_24928451/article/details/44651503

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!