java有关switch语句运用
2015-03-26 16:17
316 查看
我将以下小例子为大家讲述。
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;
}
}
}
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语句
- Java中的switch语句
- Java和C++中的switch语句
- Java温故知新 - Switch语句
- Java温故知新 - Switch语句
- java中的SWITCH语句的应用
- switch-case 语句的优点和几点注意事项 Java
- C++ 第十二周报告4--《 运用switch语句 求存款利率 》
- 注意:Java中Switch语句的参数类型-_-!...
- switch语句在Java和C#使用中的差异
- .“在JAVA中,异常子类必须在它们任何父类之前使用,因为运用父类的catch语句将捕获该类型及其所有子类类型的异常。这样,后面运行子类的catch语句将永远不会到达。而且,Java中不能到达的代码是一个错误。”
- switch-case 语句的优点和几点注意事项 Java
- java switch语句的参数类型(转帖)
- 毕向东_Java基础视频教程-第02天-switch语句的特点
- java switch语句的参数类型
- Java中的一直不能掌握的switch-case语句
- 论JAVA里switch语句(转)
- java中的switch语句
- java switch语句详解
- switch-case 语句的优点和几点注意事项 Java