Java顺序、选择、循环


Scanner

Scanner s=new Scanner(System.in);

创建一个对象,实现人机交互

  1. next()和nextLine()

    next() :一定是读取了有效字符之后才结束输入,对于输入有效字符前的空白会将其去掉,对输入有效字符后的空白作为结束符号,所以这个方法不能得到带有空格的字符串。

    nextLine() :以enter作为结束键,也就是说回车之前的字符都可以得到,因此这个方法可以得到空白。

  2. hasNext()和hasNextLine()

    判断是否还有输入数据

顺序结构

除非特别指明,否则按照顺序一句一句的执行,是最简单的算法结构

选择结构

  1. if (布尔表达式){

    //表达式为true执行的语句

    }

    String str=s.nextLine();
    		if(str.equals("Hello")) {
    			System.out.println(str);
    		}
    		System.out.println("oh");
    
  2. if (布尔表达式){

    //表达式为true执行的语句

    }else{

    //表达式为false执行的语句

    }

    int score=s.nextInt();
    		if(score>60) {
    			System.out.println("及格");
    		}else {
    			System.out.println("不及格");
    		}
    		
    
  3. if 多条件选择中只要有一个执行就会自动跳出算法

    int score=s.nextInt();
    		if(score>=90&&score<=100) {
    			System.out.println("A");
    		}
    		else if(score>=80&&score<90) {
    			System.out.println("B");
    		}
    		else if(score>=70&&score<80) {
    			System.out.println("C");
    		}
    		else if(score>=60&&score<70) {	
    			System.out.println("D");
            }
    		else if(score>=0&&score<60) {
    			System.out.println("E");
    		}else {
    			System.out.println("非法成绩!");
    		}
    
  4. if嵌套算法

    int i=s.nextInt();
    		if(i<100) {
    			if(i<50) {
    				System.out.println(i);
    			}
    		}
    
  5. switch语句

    char grade='C';
    		switch(grade) {
    		case'A':System.out.println("优秀");break;
    		case'B':System.out.println("一般");break;
    		case'C':System.out.println("菜鸡");break;
    		default:System.out.println("不明物体");
    		}
    

    注意加上break,不然都会输出,case的穿透性。

    可以比较byte,int,short,字符串,字符串是jdk7之后支持的。

循环语句

  1. while(表达式){

    //执行语句

    }

  2. do{

    //执行语句

    }while(表达式)

不同:while先判断再执行,do-while先执行再判断

  1. for循环

    for循环是支持迭代的一种通用结构,是最有效、最灵活的循环结构。

    //九九乘法表
    int sum=1;
    		
    	   for (int i = 1; i < 10; i++) {
    		for(int j=1;j<=i;j++) {
    			sum=i*j;
    			System.out.print(j+"x"+i+"="+sum+"\t");
    			
    			}
    		System.out.println();
    		}
    
  2. 增强for循环

    int[] num= {1,2,3,4};
    		for(int x:num) {
    			System.out.println(x);
    		}
    

    将数组中的变量遍历赋值给for循环中定义的变量,这样不用算数组长度,也比原来的for循环简单。

break,continue

  1. break直接跳出循环,不执行循环中的剩余语句

  2. continue终止本次循环但是接着执行下面的循环

    int i=0;
    		while(i<100) {
    			i++;
    			if(i%10==0) {
    				System.out.println();
    				continue;
    			}
    			System.out.print(i+" ");
    		}
    

    此次的输出结果为

    1 2 3 4 5 6 7 8 9 
    11 12 13 14 15 16 17 18 19 
    21 22 23 24 25 26 27 28 29 
    31 32 33 34 35 36 37 38 39 
    41 42 43 44 45 46 47 48 49 
    51 52 53 54 55 56 57 58 59 
    61 62 63 64 65 66 67 68 69 
    71 72 73 74 75 76 77 78 79 
    81 82 83 84 85 86 87 88 89 
    91 92 93 94 95 96 97 98 99 
    

    break直接辞职,continue就是请假

拓展:求101--150之间的质数

for(int i=101;i<150;i++) {
			int flag=0;
			for(int j=2;j<=i/2;j++) {
				if(i%j==0) {
					flag=1;
					continue;
				}
			}
			if(flag==0) {
				System.out.print(i+" ");
			}
		}