学习目录

01 用户交互Scanner

package com.kuang.scanner;

import java.util.Scanner;

public class Demo01 {
   public static void main(String[] args) {
       //创建一个扫描器对象,用于接收键盘数据
       Scanner scanner = new Scanner(System.in);
       System.out.println("使用next方式接收:");

       //判断用户有没有输入字符串
       if (scanner.hasNext()){
           //使用next方式接收:
           String str = scanner.next(); //程序会等待用户输入完毕
           System.out.println("输入的内容为:"+str);
      }
       //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
       scanner.close();
  }
}
package com.kuang.scanner;

import java.util.Scanner;

public class Demo02 {
   public static void main(String[] args) {
       //从键盘接收数据
       Scanner scanner = new Scanner(System.in);
       System.out.println("使用nextLine方式接收:");
       //判断是否还有输入
       if (scanner.hasNextLine()){
           String str = scanner.nextLine();
           System.out.println("输入的内容为:"+str);
      }
       scanner.close();
  }
}
package com.kuang.scanner;

import java.util.Scanner;

public class Demo03 {
   public static void main(String[] args) {
       //从键盘接收数据
       Scanner scanner = new Scanner(System.in);
       System.out.println("请输入数据:");

       String str = scanner.nextLine();
       System.out.println("输入的数据为:"+str);
  }
}
02 Scanner 进阶使用
package com.kuang.scanner;

import java.util.Scanner;

public class Demo04 {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       //从键盘接收数据
       int i = 0;
       float f = 0.0f;
       System.out.println("请输入整数:");
       //如果...那么...
       if (scanner.hasNextInt()){
          i = scanner.nextInt();
           System.out.println("整数数据为:"+i);
      }else {
           System.out.println("输入的不是整数数据!");
      }
       System.out.println("请输入小数:");
       //如果...那么...
       if (scanner.hasNextFloat()){
           f = scanner.nextFloat();
           System.out.println("小数数据为:"+f);
      }else {
           System.out.println("输入的不是小数数据!");
      }
       scanner.close();
  }
}
package com.kuang.scanner;

import java.util.Scanner;

public class Demo05 {
   public static void main(String[] args) {
       //我们可以输入多个数字,并求其总和与平均值,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果
       Scanner scanner = new Scanner(System.in);

       //和
       double sum = 0.0;
       //计算输入了多少个数字
       int m = 0;
       System.out.println("请输入数据:");
       //通过循环判断是否还有输入,并在里面对每一次进行求和和统计
       while (scanner.hasNextDouble()){
           double x = scanner.nextDouble();
           m = m + 1;
           sum = sum + x;
           System.out.println("你输入了第" +m+"个数据,当前结果sum ="+ sum);
      }
       System.out.println(m + "个数的和为" + sum);
       System.out.println(m + "个数的平均值为:"+ (sum/m));

       scanner.close();
  }
}
03 顺序结构

package com.kuang.struct;

public class ShunXuDemo {
   public static void main(String[] args) {
       System.out.println("hello1");
       System.out.println("hello2");
       System.out.println("hello3");
       System.out.println("hello4");
       System.out.println("hello5");
  }
}
04 选择结构

if单选择结构

package com.kuang.struct;

import java.util.Scanner;

public class ifDemo01 {
   public static void main(String[] args) {

       Scanner scanner = new Scanner(System.in);
       System.out.println("请输入内容:");
       String s = scanner.nextLine();

       //equals:判断字符串是否相等
       if (s.equals("Hello")){
           System.out.println(s);
      }
       System.out.println("End");
       scanner.close();
  }
}

if双选择结构

package com.kuang.struct;

import java.util.Scanner;

public class IfDemo02 {
   public static void main(String[] args) {
       //考试分数大于60分就是及格,小于60分就是不及格
       Scanner scanner = new Scanner(System.in);
       System.out.println("请输入成绩:");

       int score = scanner.nextInt();
       if (score>60){
           System.out.println("及格");
      }else {
           System.out.println("不及格");
      }
       scanner.close();
  }
}

if多选择结构

package com.kuang.struct;

import java.util.Scanner;

public class IfDemo03 {
   public static void main(String[] args) {
       //考试分数大于60分就是及格,小于60分就是不及格
       Scanner scanner = new Scanner(System.in);
       /*
       if 语句至多有一个 else 语句,else 语句在所有的 else if 语句之后。
       if 语句可以有若干个 else if 语句,它们必须在 else 语句之前。
       一旦其中一个 else if 语句检测为 true,其它 else if 以及 else 语句都将跳过执行。
        */
       System.out.println("请输入成绩:");
       int score = scanner.nextInt();

       if (score==100){
           System.out.println("恭喜满分!");
      }else if (score<100&&score>=90){
           System.out.println("A级");
      }else if (score<90&&score>=80){
           System.out.println("B级");
      }else if (score<80&&score>=70){
           System.out.println("C级");
      }else if (score<70&&score>=60){
           System.out.println("D级");
      }else if (score<60){
           System.out.println("不及格");
      }else {
           System.out.println("你输入的是非法数据!");
      }



       scanner.close();
  }
}

嵌套的if结构

switch多选择结构

package com.kuang.struct;

public class SwitchDemo01 {
public static void main(String[] args) {
//case 穿透 //switch匹配一个具体的值
char grade = '0';

switch (grade){
case 'A':
System.out.println("优秀");
break;//可选
case 'B':
System.out.println("良好");
break;//可选
case 'C':
System.out.println("及格");
break;//可选
case 'D':
System.out.println("再接再厉");
break;//可选
default:
System.out.println("未知等级");
}
}
}
package com.kuang.struct;

public class SwitchDemo02 {
   public static void main(String[] args) {
       String name = "小";
       //JDK7的新特性,表达式结果可以是字符串!!!
       //字符的本质还是数字

       //反编译   java---class(字节码文件)---反编译(IDEA)


       switch (name){
           case "小明":
               System.out.println("小明");
               break;
           case "小亮":
               System.out.println("小亮");
               break;
           default:
               System.out.println("找不到!");
      }
  }
}

 

 

 

 

05 循环结构

while循环

package com.kuang.struct;

public class WhileDemo01 {
public static void main(String[] args) {
//输出1~100
int i = 0;

while (i<100){
i++;
System.out.println(i);
}
}
}
package com.kuang.struct;

public class WhileDemo02 {
public static void main(String[] args) {
//死循环
while (true){
//等待客户端连接
//定时检查
//......
}
}
}
package com.kuang.struct;

public class WhileDemo03 {
public static void main(String[] args) {
//计算 1+2+3+...+100=?
//高斯的故事
int i = 0;
int sum = 0;
while (i<=100){
sum +=i; //sum = sum + i
i++;
}
System.out.println(sum);
}
}

do...while循环

package com.kuang.struct;

public class DoWhileDemo01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
sum = sum+i;
i++;
}while (i<=100);
System.out.println(sum);
}
}
package com.kuang.struct;

public class DoWhileDemo02 {
   public static void main(String[] args) {
       int a = 0;

       while (a<0){
           System.out.println(a);
           a++;
      }
       System.out.println("=====================");

       do {
           System.out.println(a);
           a++;
      }while (a<0);
  }
}

For循环

package com.kuang.struct;

public class ForDemo01 {
   public static void main(String[] args) {
       int a = 1; //初始化条件

       while (a<=100){ //条件判断
           System.out.println(a); //循环体
           a+=2;//迭代
      }
       System.out.println("While循环结束!");
           //初始化//条件判断//迭代
       for (int i= 0;i<=100;i++){
           System.out.println(i);
      }
       System.out.println("For循环结束!");

       /*
        快捷操作:100.for + 回车 可产生下面的循环
        for (int i = 0; i < 100; i++) {
       }

        */
       /*
       关于 for 循环有以下几点说明:
       最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
       然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
       执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
       再次检测布尔表达式,循环执行上面的过程。
        */
       /*死循环
       for ( ; ;){

       } */
  }
}
package com.kuang.struct;

public class ForDemo02 {
   public static void main(String[] args) {
       //练习1:计算0到100之间的奇数和偶数的和
    //法一:
       int sum1 = 0;
       int sum2 = 0;

       for (int x=1;x<=100;x=x+2){
           sum1+=x;
      }
       for (int s=0;s<=100;s=s+2){
           sum2+=s;
      }
       System.out.println("奇数和为:"+sum1);
       System.out.println("偶数和为:"+sum2);
    //法二:
       int oddsum = 0;
       int evensum = 0;

       for (int i = 0; i <= 100; i++) {
           if (i%2!=0){  //奇数
               oddsum+=i;  //oddsum = oddsum + i
          }else {       //偶数
               evensum+=i;
          }
      }
       System.out.println("奇数的和:"+oddsum);
       System.out.println("偶数的和:"+evensum);
  }
}
package com.kuang.struct;

public class ForDemo03 {
   public static void main(String[] args) {
       //练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
      //法一
       for (int i = 0; i <= 1000; i++) {
           if (i%5==0){
               System.out.print(i+"\t");
          }
           if (i%(3*5)==0){ //每行输出
               System.out.println(); //换行写法一
               System.out.print("\n"); //换行写法二
          }
      }
       //println 输出完会换行
       //print   输出完不会换行
       System.out.print("\n");
       System.out.println("=============================");
      //法二
       int x = 1;
       while (x<=1000){
           x++;
           if (x%5==0){
               System.out.print(x+"\t");
          }
           if (x%(3*5)==0){
               System.out.println();
          }
      }
  }
}

打印九九乘法表

package com.kuang.struct;
/*
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 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
*/
public class ForDemo04 {
   public static void main(String[] args) {
       //1.我们先打印第一列
       //2.我们把固定的 “1” 再用一个循环包起来
       //3.去掉重复项 i <= j
       //4.调整样式
       for (int j = 1; j <= 9; j++) {
           for (int i = 1; i <= j; i++) {
               System.out.print(i+"*"+j+"="+(i*j)+ "\t");
               //System.out.print(j+"*"+i+"="+(i*j)+ "\t");
          }
           System.out.println();
      }
  }
}

增强for循环

package com.kuang.struct;

public class ForDemo05 {
   public static void main(String[] args) {
       int[] numbers ={10,20,30,40,50}; //定义了一个数组

       for (int i=0;i <5;i++){
           System.out.println(numbers[i]);
      }
       System.out.println("================");
       //遍历数组的元素
       for (int x :numbers){
           System.out.println(x);
      }
  }
}
break&continue

package com.kuang.struct;

public class BreakDemo {
   public static void main(String[] args) {
       int i = 0;
       while (i<100){
           i++;
           System.out.println(i);
           if (i==30){
               break;
          }
      }
       System.out.println("123");
  }
}
package com.kuang.struct;

public class ContinueDemo {
   public static void main(String[] args) {
       int i = 0;
       while (i<100){
           i++;
           if (i%10==0){
               System.out.println();
               continue;
          }
           System.out.print(i);
      }


       //break:在任何循环语句的主体部分,均可用break控制循环的流程。
       //break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
       //continue语句用于循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定
  }
}
package com.kuang.struct;

public class LabelDemo {
   public static void main(String[] args) {
       //打印101-150之间所有的质数
       //质数是指大于1的自然数中,除了1和它本身以外不再有其他因数的自然数

      outer: for (int i = 101;i < 150;i++){
           for (int j = 2;j < i/2;j++){
               if (i % j ==0){
                   continue outer ;
              }
          }
          System.out.print(i+" ");
      }
  }
}
流程控制练习

打印 5 层三角形:

package com.kuang.struct;

public class TestDemo {
   public static void main(String[] args) {
       //打印三角形   5行
       for (int i = 1; i <= 5; i++) {
           for (int j = 5;j >= i;j--){  //打印区域 1 的空白三角
               System.out.print(" ");
          }
           for (int j = 1;j <= i;j++){  //打印区域 2 的空白三角
               System.out.print("*");
          }
           for (int j = 1;j < i;j++){   //打印区域 3 的空白三角
               System.out.print("*");
          }
           System.out.println();
      }
  }
}