美文网首页
语法结构

语法结构

作者: 公子请留步LookMe | 来源:发表于2020-06-22 21:52 被阅读0次

顺序结构

分支结构

1.单分支(if..else)
  if(值--boolean){
  }else{
  }; 
  
  if(){
  }else if(){
  }
2.多分支(switch)

课堂任务


image.png
单分支例题:
例题一:

int month;
if(month == 3 || month == 4 || month == 5){
  System.out.prinltn("春天");
}else if(month == 6 || month == 7 || month == 8){
   System.out.prinltn("夏天");
}else if(month == 9 || month == 10 || month == 11){
   System.out.prinltn("秋天");
}else{
  System.out.prinltn("冬天")
}

例题二:
float score;
if(score<60){
  System.out.prinltn("不及格");
}else if(60=<score && score<70){
  System.out.prinltn("及格");
}else if(70=<score && score<80){
  System.out.prinltn("中");
}else if(80=<score && score<90){
  System.out.prinltn("良好")
}else if(90=<score && score<100){
  System.out.prinltn("优秀")
}else if(score==100){
  System.out.prinltn("满分");
}else{
  System.out.prinltn("分数有误");
}

例题三:
随机数
1.随机一个骰子 点数为1到六的数 
double value =Math.random();
int number = (int)(value*6+1);
2.玩家去猜测大小 请玩家输入大小
Scanner input = new Scanner(System.in);
System.out.prinltn("请玩家下注,买定离手,二十块钱一盘");
String pro = input.nextLine();
3.比较玩家是否猜对
if((number <= 3 && pro.equals("小"))||(number >3)&&pro.equals("大")){
      System.out.prinltn("恭喜你压对啦,得四十块");
}else{
       System.out.prinltn("下一句再接再厉,损失二十块");
}
多分支例题
用switch语句实现一个判断学生成绩对应的区间
public static viod main(String[] args){
           Sanncer input = new Sanncer(System.in);
            int score = input.nextInt();
            switch(score/10){
                case 6:
                          System.out.prinltn("及格");
                case 7:
                          System.out.prinltn("还不错");
                case 8:
                          System.out.prinltn("进步打的很");
                case 9:
                          System.out.prinltn("哇);
                case 10:
                          System.out.prinltn("我惊呆了");
                defulet:
                         System.out.prinltn("滚吧死废物");
            }
}

循环结构

for while do...while

相关文章

网友评论

      本文标题:语法结构

      本文链接:https://www.haomeiwen.com/subject/zpwughtx.html