java有关switch语句运用
来源:互联网 发布:手机数据恢复大师 编辑:程序博客网 时间:2024/05/17 08:06
我将以下小例子为大家讲述。
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语句运用
- switch语句的运用
- switch语句简单运用
- switch语句的运用
- switch语句的初次运用
- 有关switch语句的总结
- JAVA-练习switch的运用
- switch case 语句的初步运用。。
- if语句运用和switch初入
- switch语句有关default位置的问题
- Java中的switch语句
- java中的switch语句
- java switch语句详解
- Java温故知新 - Switch语句
- java中的switch语句
- Java switch语句详解
- Java中switch语句
- JAVA-Switch语句Demo
- Android程序:文字匹配实现(AutoCompleteTextView和MultiAutoCompleteTextView)
- JS中的this变量的使用介绍
- iOS ASIHtttpRequest 缓存
- When should you use a class vs a struct in C++?
- UITableView滚动优化
- java有关switch语句运用
- session php
- แทงบอลสร้างรายได้ให้คุณได้จริง
- (转)C/C++基本数据类型长度
- 交互多模型(
- LeadTools Android 入门教学——运行第一个Android Demo
- android 单指螺旋放缩图片多点触控
- CSS 表单可能跨多行,应该清理div容器
- sun.misc.Unsafe源代码