Java中switch语句如何改写为长尾词?

2026-04-02 12:531阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计489个文字,预计阅读时间需要2分钟。

Java中switch语句如何改写为长尾词?

关于switch语句的使用,有很多有趣的例子可以看看。读懂例子后,switch语句的差别就一目了然了。以下是一个简单的小例子,供大家参考:

javaimport java.util.Scanner;

public class text1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print(请输入一个数字(1-7):); int num=sc.nextInt();

switch (num) { case 1: System.out.println(周一); break; case 2: System.out.println(周二); break; case 3: System.out.println(周三); break; case 4: System.out.println(周四); break; case 5: System.out.println(周五); break; case 6: System.out.println(周六); break; case 7: System.out.println(周日); break; default: System.out.println(输入有误,请输入1-7之间的数字。); } }}

关于switch语句运用,有兴趣的可以进来看看,读懂例子后switch语句差不

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

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(i100) 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语句如何改写为长尾词?

本文共计489个文字,预计阅读时间需要2分钟。

Java中switch语句如何改写为长尾词?

关于switch语句的使用,有很多有趣的例子可以看看。读懂例子后,switch语句的差别就一目了然了。以下是一个简单的小例子,供大家参考:

javaimport java.util.Scanner;

public class text1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print(请输入一个数字(1-7):); int num=sc.nextInt();

switch (num) { case 1: System.out.println(周一); break; case 2: System.out.println(周二); break; case 3: System.out.println(周三); break; case 4: System.out.println(周四); break; case 5: System.out.println(周五); break; case 6: System.out.println(周六); break; case 7: System.out.println(周日); break; default: System.out.println(输入有误,请输入1-7之间的数字。); } }}

关于switch语句运用,有兴趣的可以进来看看,读懂例子后switch语句差不

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

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(i100) 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语句如何改写为长尾词?