Java 5. 语法结构

顺序结构
分支结构
  • 单分支if
  1. 先看一个if的例子
//lib提供好的类库 Scanner引用类型,开发者给我们提供好的一个类文件 Scanner.java
import java.util.Scanner;

public class Test{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in); //Scanner 是引用类型,基本类型才能赋值,引用类型要new
		System.out.println("请输入一个数字:");
		int day = input.nextInt(); //可以读取我们输入的文字,读字符串input.nextLine()
		if(day == 1){
			System.out.println("Monday");
		}else if(day == 2){
			System.out.println("Tuesday");
		}else if(day == 3){
			System.out.println("Wednesday");
		}else if(day == 4){
			System.out.println("Thursday");
		}else if(day == 5){
			System.out.println("Friday");
		}else if(day == 6){
			System.out.println("Saturday");
		}else if(day == 7){
			System.out.println("Sunday");
		}else{
			System.out.println("请输入1-7的整数!");
		}
	}
}
  • cmd下运行报错:【错误: 编码GBK的不可映射字符】

    • 解决办法:在notepad++打开,点击菜单栏的编码->使用ANSI编码
  • java.util.InputMismatchException

    • 输入的类型与接收的数据类型不一致
  1. /利用if语句实现,判断给定月份对应的季节
import java.util.Scanner; //rt.jar

public class Jijie{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个数字:");
		int day = input.nextInt();
		System.out.println();
		if(day == 12 | day == 1 | day == 2){
			System.out.println("现在是冬天");
		}else if(day >= 3 && day <= 5){
			System.out.println("现在是春天");
		}else if(day >= 6 | && day <= 8){
			System.out.println("现在是夏天");
		}else if(day >= 9 && day <= 11){
			System.out.println("现在是秋天");
		}else{
			System.out.println("请输入1-12的整数!");
		}			
	}
}
  1. 利用if实现一个随机摇骰子的小游戏

    1. 随机产生一个骰子点数 1-6

    2. 让玩家猜测大小 大 小

    3. 比较点数与猜测结果

      ​ - 点数1 2 3同时小 或者 点数4 5 6 同时大

//import java.lang.Math;  //lang包可以省略不写
import java.util.Scanner;

public class DiceGame{
	public static void main(String[] args){
		double value = Math.random(); //[0.0,1.0)  -- 0.0-0.9999
		int num = (int)(value*6+1);
		System.out.println(num);
		Scanner input = new Scanner(System.in);
		System.out.println("买大买小,买定离手!");
		String choose = input.nextLine();
		System.out.println("本次摇出的点数为:"+num);
		if((num >= 1 && num <=3 && choose.equals("小")) || (num >3 && num<=6 && choose.equals("大"))){
			System.out.println("猜对了!");
		}else{
			System.out.println("猜错了!");
		}
	}
}
字符串之间的比较不能"a"=="b" ,需要这样写:"a".equals("b")
  • 多分支switch
    1. examp1:
import java.util.Scanner;

public class TestSwitch{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.println("请输入:");
		int day = input.nextInt();
        // switch(值) byte,short,int,char emnum,String		
        switch(day){
			case 1:
				System.out.println("Monday");
				break;
			case 2:
				System.out.println("Tuesday");
				break;
			default:
				System.out.println("error");
				break;
		}				
	}
}
  1. examp2: 利switch实现 判断学生成绩对应的区间

    import java.util.Scanner;
    
    public class Switch123{
    	public static void main(String[] args){
    		Scanner input = new Scanner(System.in);
    		System.out.println("请输入你的分数:");
    		int score = input.nextInt();
    		switch(score/10){
    			case 0:
    			case 1:
    			case 2:
    			case 3:
    			case 4:
    			case 5:
    				System.out.println("挂啦!");
    				break;
    			case 6:
    				System.out.println("欧耶 过啦!");
    				break;
    			case 7:
    				System.out.println("一般般吧!");
    				break;
    			case 8:
    				System.out.println("比较好啦!");
    				break;
    			case 9:
    				System.out.println("优秀啊!");
    				break;
    			case 10:
    				if(score == 100){
    					System.out.println("学霸本霸!");
    					break;
    				}		
    			default:
    				System.out.println("输入错误!");
    		}
    	}
    }
    1. examp3:

      /*
      利用Scanner输入一个值(值代表一个星期几)
      ​	为小明同学制定一个学习计划
      ​	1 3 5 学校语文
      ​	2 4 6 学习数学
      ​	7        出去玩耍
      */
      import java.util.Scanner;
      
      public class Switch456{
      	public static void main(String[] args){
      		Scanner input = new Scanner(System.in);
      		System.out.println("请输入一个1-7的数字:");
      		int day = input.nextInt();  //输入非int类型就会报错java.util.InputMismatchException
      		switch(day){
      			case 1:
      			case 3:
      			case 5:
      				System.out.println("学习java!");
      				break;
      			case 2:
      			case 4:
      			case 6:
      				System.out.println("学习python!");
      				break;
      			case 7:
      				System.out.println("玩? 想得美");
      				break;
      			default:
      				System.out.println("你逃不掉1-7天的");
      		}
      	}
      }
  • if和switch的优缺点
好处缺点
if可以写复杂的逻辑执行比较慢
switch判断的过程效率更高只能做恒等比较(==),固定值
循环结构
  • for

    1. 栗子1
    操场有一百多人,让他们排队,
    三个人一组多一个,四个人一组多两个,五个人一组多两
    求操场上的人数是多少? 设为n
    */
    public class TestFor{
    	public static void main(String[] args){
    		for(int n=100;n<200;n++){
    			if(n%3==1 && n%4==2 && n%5==2){
    				System.out.println("操场上的人数为:"+n);
    			}			
    		}
    	}
    }
    1. 栗子2
    /*
    甲乙丙丁四个人加工零件  加工的总零件数为370个
    如果甲加工的零件数多10   
    如果乙加工的零件数少20
    如果丙加工的零件数乘以2
    如果丁加工的零件数除以2
    则四个人加工的零件数就相等啦
    求 四个人加工的零件个数分别是多少?
    设甲乙丙丁相等时的数是x
    那么甲x-10 + 乙x+20 + 丙x/2 + 丁x*2 ==370
            x       x       0       2x
    优化:甲乙可以理解为都在x附近浮动,丁为2x,丙是最小的,
    我们先缩小下上限,假设丙为0,4x=370,x大概95这样,丙是最小的,95除以2大概45这样,所以for循环范围可以定位45-95就好了
    */
    public class Lianxiti1{
    	public static void main(String[] args){
    		for(int x=45;x<95;x++){
    			if((x-10)+(x+20)+(x/2)+(x*2) == 370){
    				System.out.println("甲加工的零件个数是:"+(x-10));
    				System.out.println("乙工的零件个数是:"+(x+20));
    				System.out.println("丙加工的零件个数是:"+(x/2));
    				System.out.println("丁加工的零件个数是:"+(x*2));
    			}			
    		}
    	}
    }
    1. 栗子3
    /*
    鸡兔同笼问题
    小鸡 小兔子 关在同一个笼  小鸡两  小兔子四只脚
    小鸡+小兔子  总数50只  脚的总数160只
    求小鸡 和 小兔子 各多少只?
    小鸡x, 小兔子50-x
    2*x+(50-x)*4 == 160
    
    */
    public class Jitutonglong{
    	public static void main(String[] args){
    		for(int x=1;x<50;x++){
    			if(2*x+(50-x)*4 == 160){
    				System.out.println("小鸡是:" + x + "只");
    				System.out.println("小兔子是:" + (50-x) + "只");
    			}			
    		}
    	}
    }
    1. 栗子4
    /*
    通过循环找寻三位数字的水仙花数
    153  ---> 1  5  3
              1 + 125 + 27 == 153
    		  x*x*x
    100-999之间挨个尝试,满足上述规则的数字成为水仙花数   153  370 371 407
    
    */
    public class Daffodils{
    	public static void main(String[] args){
    		for(int n=100;n<999;n++){
    			if(Math.pow(n/100,3)+Math.pow(n/10%10,3)+Math.pow(n%10,3) == n){
    				System.out.println("三位数字的水仙花数:"+n);
    			}			
    		}
    	}
    }
    1. 栗子5

      /*
      *******   画星星,换行                    i==1, 7
      ***#***   画星星,画空格,画星星,换行    i==2, 3 1 3
      **###**   画星星,画空格,画星星,换行    i==3, 2 3 2
      *#####*   画星星,画空格,画星星,换行    i==4, 1 5 1
      如果是第一行,很简单就是画星星
      第二行往后,规则不同
      不管哪种情况,都需要换行
      */
      import java.util.Scanner;
      
      public class DrawStar1{
      	public static void main(String[] args){
      		System.out.println("画几个:");
      		Scanner input = new Scanner(System.in);
      		int line = input.nextInt();
      		for(int i=1;i<=line;i++){  //控制行数
      			if(i==1){//第一行规则
      				//画星星
      				for(int j=1;j<=2*line-1;j++){
      					System.out.print("*");
      				}
      			}else{//后三行规则
      				//画星星
      				for(int j=1;j<=line+1-i;j++){
      					System.out.print("*");
      				}
      				//画空格
      				for(int j=1;j<=2*i-3;j++){
      					System.out.print(" ");
      				}
      				//画星星
      				for(int j=1;j<=line+1-i;j++){
      					System.out.print("*");
      				}
      			}
      			//换行
      			System.out.println();
      		}
      	}
      }
    2. 栗子6

      /*
         1           
        121          
       12321         
      1234321        
      
      空格3,     1,     空格3  换行     i==1  --1   
      空格2,   1 2 1,   空格2  换行     i==2  --3
      空格1, 1 2 3 2 1 ,空格1  换行     i==3  --5
            1 2 3 4 3 2 1,                i==4  --7	
      	1 2 3 4 5 4 3 2 1,             i==5  --9
      	  
      	  i==1,j==1,  1,
      	  i==1,j==2,  12,
      */
      
      import java.util.Scanner;
      
      public class Draw1{
      	public static void main(String[] args){
      		System.out.print("请输入图形的高度(行数):");
      		Scanner input = new Scanner(System.in);
      		int line = input.nextInt();
      		for(int i=1;i<=line;i++){//控制行数
      			if(false){
      				System.out.print("1234321");
      			}else{
      				for(int j=1;j<=line-i;j++){//空格
      					System.out.print(" ");
      				}
      				for(int j=1;j<=i;j++){//左   
      					System.out.print(j); 
      				}
      				for(int j=i;j>1;j--){ //右
      					System.out.print(j-1);
      				}
      			}
      				//空格
      				System.out.print(" ");
      				System.out.println();								
      		}
      	}
      }
    3. 栗子7 乘法口诀

      /*
      1*1=1
      1*2=2 2*2=4
      1*3=3 2*3=6 3*3=9
      1*4=4 2*4=8 3*4=12
      
      i==1 1 * 1 = 1             
      i==2 1 * 2 = 2 2 * 2 = 4
      
      for(int i=1;i<=line;i++){}//控制行数
      	第一行 1 * 1 = 1
      	第二行规则
      	for(int j=1;j<=i;j++){j+"*"+i+"="+j*i 空格}  j<i每次输出完一个等式都要空格,当j==i,输完等式,要换行
      	
      */
      
      import java.util.Scanner;
      
      public class DrawMultiplicationFormulas{
      	public static void main(String[] args){
      		System.out.println("请输入一个数,打印该数以内的乘法口诀:");
      		Scanner input = new Scanner(System.in);
      		int line = input.nextInt();
      		for(int i=1;i<=line;i++){
      			if(i==1){
      				System.out.println("1*1=1");
      			}else{
      				for(int j=1;j<=i;j++){
      					if(j<i){
      						System.out.print(j+"*"+i+"="+j*i+" ");
      					}else{
      						System.out.print(j+"*"+i+"="+j*i);
      						System.out.println();
      					}					
      				}
      			}
      		}	
      	}
      }
    4. 栗子 找质数和非质数

      /*
      找出2-100之间的素数(质数) 只能被 1和本身整除的数字  2 3 5 7 11 13
      2是素数
      3是素数
      3不是素数
      5是素数
      6不是素数
      7是素数
      
      for(int i=1;i<num;i++){} //控制遍历num以内的所有数
      	for(int j=2;j<=i/2;j++){} //计算i的素数,j从2开始到i/2来遍历
      		if(i%j){} //
      */
      
      import java.util.Scanner;
      
      public class Prime{
      	public static void main(String[] args){
      		System.out.print("输入一个数,我会输出该数以内的所有素数:");
      		Scanner input = new Scanner(System.in);
      		int num = input.nextInt();
      		for(int i=1;i<=num;i++){
      			boolean flag = true;
      			//System.out.print("i=: "+i+" ");
      			//System.out.println("Math.sqrt(i): "+(int)Math.sqrt(i));
      			for(int j=2;j<=Math.sqrt(i);j++){
      				if(i%j==0){			
      					System.out.println(i+"不是素数");
      					flag = false;
      					break;
      				}
      			}
      			//System.out.println("flag: "+flag);
      			if(flag == true){ //如果j循环完,上面的if没有进去过,那就是素数	
      				System.out.println(i+"是素数");
      			}
      		}
      			
      	}
      }
      • jdk1.5以后 for新特性
      import java.util.Arrays;
      
      public class Arr{
      	public static void main(String[] args){
      		int[] array = new int[]{1,2,3,4,5};
      		//jdk1.5以后 for新特性
      		for(int value:array){ //只能取值,没有index,找不到元素到底哪一个
      			System.out.println(value);
      		}
      	}
      }

      break
      continue
      循环标记 给循环起名字,指定中断标记的循环

  • while

    ​ 1. 栗子1 一层循环

    /*
    有一个水池 已经盛满了120立方米的水
    有一个排水管 每小时进水18立方米
    有一个排水管 每小时排水30立方米
    两水管一起开
    经过多少小时  水池的水排放干净?
    */
    public class TestWhileOne{
    	public static void main(String[] args){
    		int volume = 120;
    		int hour = 0;
    		while(volume>0){
    			volume += 18;
    			volume -= 30;
    			hour++;
    		}
    		System.out.print("水池的水经过"+hour+"小时排放干净");
    	}
    }
    1. 栗子2 while嵌套

      /*
      画星星
         *
        ***
       *****
      *******
      
      空格  星星  空格  换行
      3 1 3 换行
      2 3 2 换行
      1 5 1 换行
      0 7 0 
      */
      import java.util.Scanner;
      
      public class WhileDrawStar{
      	public static void main(String[] args){
      		System.out.print("输入画几行:");
      		Scanner input = new Scanner(System.in);
      		int line = input.nextInt();
      		int i = 1;
      		while(i<=line){ //控制line
      			//画空格
      			int j = 1;
      			while(j<=line-i){
      				System.out.print(" ");
      				j++;
      			}
      			//画星星
      			int k = 1;
      			while(k<=2*i-1){
      				System.out.print("*");
      				k++;
      			}
      			//换行
      			System.out.println();
      			i++;
      		}
      	}
      }
      1. 栗子3

        /*
        小明同学从A点以7km/h的速度向B点出发
        小雨同学从B点以18km/h的速度向A点出发
        A和B之间的距离是1000km
        求解 小明同学和小雨同学经过多少小时相遇?
        
        设x小时相遇
        7x+18x=1000
        1 25  975
        2 25  950
        3 25  925
        每小时25km,走完1000km  需要40小时
        */
        import java.util.Scanner;
        
        public class WhileTwo{
        	public static void main(String[] args){
        		//System.out.print("输入画几行:");
        		//Scanner input = new Scanner(System.in);
        		//int line = input.nextInt();
        		/*int time = 1;
        		//使用循环标记
        		ok:while(true){
        			while(7*time+18*time == 1000){
        				System.out.print("小明同学和小雨同学经过"+time+"小时相遇");
        				break ok;
        			}
        			time++;
        		}
        		*/
        		int distance = 1000;
        		int time = 0;
        		while(distance>0){
        			distance -= 7;
        			distance -= 18;
        			System.out.println("经过"+time+"小时,还剩余距离: "+distance);
        			time++;
        		}
        		System.out.print("小明同学和小雨同学经过"+time+"小时相遇");
        	}
        }
        运行结果:
        D:\myproject\JavaProject\learn\demo
        $ java WhileTwo
        经过0小时,还剩余距离: 975
        经过1小时,还剩余距离: 950
        经过2小时,还剩余距离: 925
        经过3小时,还剩余距离: 900
        经过4小时,还剩余距离: 875
        经过5小时,还剩余距离: 850
        经过6小时,还剩余距离: 825
        经过7小时,还剩余距离: 800
        经过8小时,还剩余距离: 775
        经过9小时,还剩余距离: 750
        经过10小时,还剩余距离: 725
        经过11小时,还剩余距离: 700
        经过12小时,还剩余距离: 675
        经过13小时,还剩余距离: 650
        经过14小时,还剩余距离: 625
        经过15小时,还剩余距离: 600
        经过16小时,还剩余距离: 575
        经过17小时,还剩余距离: 550
        经过18小时,还剩余距离: 525
        经过19小时,还剩余距离: 500
        经过20小时,还剩余距离: 475
        经过21小时,还剩余距离: 450
        经过22小时,还剩余距离: 425
        经过23小时,还剩余距离: 400
        经过24小时,还剩余距离: 375
        经过25小时,还剩余距离: 350
        经过26小时,还剩余距离: 325
        经过27小时,还剩余距离: 300
        经过28小时,还剩余距离: 275
        经过29小时,还剩余距离: 250
        经过30小时,还剩余距离: 225
        经过31小时,还剩余距离: 200
        经过32小时,还剩余距离: 175
        经过33小时,还剩余距离: 150
        经过34小时,还剩余距离: 125
        经过35小时,还剩余距离: 100
        经过36小时,还剩余距离: 75
        经过37小时,还剩余距离: 50
        经过38小时,还剩余距离: 25
        经过39小时,还剩余距离: 0
        小明同学和小雨同学经过40小时相遇
      2. 栗子4

        /*
        卖西瓜
        一车西瓜共1020个
        每天卖掉总数的一半多两个
        问:几天卖完所有的瓜
        
        设x天卖完
        第一天卖出 1020/2 + 2 = 512  剩余1020-512=508
        2           508/2 + 2 = 256  剩余508-256=252    
        3           252/2 + 2 = 128  剩余252-128=124
        4           124/2 + 2 = 64   剩余124-64=60
        5           60/2  + 2 = 32   剩余60-32=28
        6           28/2  + 2 = 16   剩余28-16=12
        7           12/2  + 2 = 8    剩余12-8=4
        8            4/2  +2  = 4    剩余4-4=0
        */
        import java.util.Scanner;
        
        public class WhileThree{
        	public static void main(String[] args){
        		int total = 1020;
        		int day = 0;
        		while(total>0){
        			total -= total/2;
        			total -= 2;
        			day++;
        			System.out.println("第"+day+"天剩余"+total+"瓜");
        		}
        		System.out.println(day+"天卖完所有的瓜");
        	}
        }
        运行结果:
        D:\myproject\JavaProject\learn\demo
        $ java WhileThree
        第1天剩余508瓜
        第2天剩余252瓜
        第3天剩余124瓜
        第4天剩余60瓜
        第5天剩余28瓜
        第6天剩余12瓜
        第7天剩余4瓜
        第8天剩余0瓜
        8天卖完所有的瓜
  • do ... while

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是一言

让我看看你的头像

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值