×

javaswitch语句ppt

javaswitch语句ppt(java中使用switch语句怎么实现下图)

admin admin 发表于2024-02-20 13:34:33 浏览33 评论0

抢沙发发表评论

大家好,今天小编来为大家解答以下的问题,关于javaswitch语句ppt,java中使用switch语句怎么实现下图这个很多人还不知道,现在让我们一起来看看吧!

本文目录

java中使用switch语句怎么实现下图

public static void main(string args){//编写内容for(int i=0;i《5;i++){switch(i){ case 0:system.out.println("student"+i+"’s result is A");break;case 1:system.out.println("student"+i+"’s result is B");break;case 2:system.out.println("student"+i+"’s result is C");break;case 3:system.out.println("student"+i+"’s result is D");break;case 4:system.out.println("student"+i+"’s result is F");break;default:break;}}}

java编程 switch语句

switch后面的括号里面只能放int类型的值,注意是只能放int类型,但是放byte,short,char类型的也可以是因为byte,short,shar可以自动提升(自动类型转换)为int,不是说就可以放它们,说白了,你放的byte,short,shar类型,然后他们会自动转换为int类型(宽化,自动转换并且安全),其实最后放的还是int类型!long不行,是因为long类型不能自动转换为int类型,注意,我说的是自动转换,或者叫隐式转换,或者有些时候叫自动提升,都是一个意思!给你解释一下原理:Java中8种基本数据类型,boolean类型不参与转换,任何类型不能转换为boolean型,boolean也不能转换为其他类型,所以剩下7种,按照他们的表数范围,(也就是能表示的最大的数的大小,比如char是0到65535,byte是-128到正127)从小到大,排序,依次为:byte、short、char、int、long、float、double规则:  1.小的往大的转换(宽化),自动转换,有些时候就会自动提升为大的类型,比如switch中  2.大的往小的转换(窄化)必须强制类型转换所以long不行,要想行就得强转如(int)long  switch后面括号中只能探测到int类型数,同理,float、double也是不行的,要想行就强转。如果你要用switch来写程序.我写了一段你参考下package Test;import java.util.Scanner;public class Test{ public static void main(String args){ int level=0;//分数等级 System.out.print("请输入考试分数:"); Scanner input=new Scanner(System.in); int score=input.nextInt();//得到输入分数 //根据分数划分等级 if(score》90){ level=1; }else if(score《=90&&score》=80){ level=2; }else if(score》=70&&score《80){ level=3; }else if(score》=60&&score《70){ level=4; }else { level=5; } String message="考试分数:"+score+" 等级:"; //用switch来打印 switch(level){case 1:System.out.println(message+"优秀");break;case 2:System.out.println(message+"良好");break;case 3:System.out.println(message+"中等");break;case 4:System.out.println(message+"及格");break;case 5:System.out.println(message+"不及格");break; }}}

OK,关于javaswitch语句ppt和java中使用switch语句怎么实现下图的内容到此结束了,希望对大家有所帮助。