一、用户交互Scanner
-
通过Scanner类获取用户的输入
-
基本语法:
Scanner scanner = new Scanner(System.in);//创建对象
-
通过Scanner类的scanner对象分别调用next()、nextLine()、nextBoolean()、 nextByte()、 nextShort()、 nextInt()、nextLong()、nextFloat()、nextDouble()等方法,从终端读入一个字符串(遇到空格或回车符结束)、一行字符串(遇到回车符才结束)、一个boolean型数据、一个byte型数据、一个short型数据、一个int型数据、一个long型数据、一个float型数据、一个**double()**型数据
-
为了正确的调用nextXXX()方法,可以先调用hasNextXXX()方法判断将要读入的数据是否为期望的数据类型,然后再调用相应的**nextXXX()**方法读取数据
public class Demo01{//类 public static void main(String[] args){//方法 //创建一个扫描器对象,用于接收键盘数据 Scanner scanner = new Scanner(System.in); System.out.println("使用next方式接收:"); //判断用户有没有输入字符串 if(scanner.hasNext()){ String str = scanner.next();//使用next方式接收 System.out.println("输出的内容为:" + str); } //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉 scanner.close(); /* 结果: 使用next方式接收: Hello World! 输出的内容为:Hello */ } }
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();//使用next方式接收 System.out.println("输出的内容为:" + str); } //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉 scanner.close(); /* 结果: 使用next方式接收: Hello World! 输出的内容为: Hello World! */ } }
public class Demo03{//类 public static void main(String[] args){//方法 //创建一个扫描器对象,用于接收键盘数据 Scanner scanner = new Scanner(System.in); System.out.println("请输入整数:"); if(scanner.hasNextInt()){ int i=scanner.nextInt(); System.out.println("整数数据:" + i); }else{ System.out.println("输入的不是整数!"); } System.out.println("请输入小数:"); if(scanner.hasNextFloat()){ float f=scanner.nextFloat(); System.out.println("小数数据:" + f); }else{ System.out.println("输入的不是小数数据!"); } scanner.close(); /* 结果1: 请输入整数: 5 整数数据:5 请输入小数: 6.9 小数数据:6.9 结果2: 请输入整数: 11.9 输入的不是整数! 请输入小数: 小数数据:11.9 */ } }
public class Demo04 { public static void main(String[] args) { //我们可以输入多个数字,并求其总和与平均数, // 每输入一个数字用回车键确认,通过输入非数字来结束 double sum = 0;//和 int m = 0;//计数 Scanner scanner = new Scanner(System.in); while (scanner.hasNextDouble()){ double x = scanner.nextDouble(); m+=1;//m++ sum+=x; System.out.println("你输入了第" + m + "个数据,然后当前结果sum=" + sum); } System.out.println(m + "个数的和为" + sum); System.out.println(m + "个数的平均值为" + (sum/m)); scanner.close(); /* 结果: 1 你输入了第1个数据,然后当前结果sum=1.0 2 你输入了第2个数据,然后当前结果sum=3.0 3 你输入了第3个数据,然后当前结果sum=6.0 a 3个数的和为6.0 3个数的平均值为2.0 */ } }
二、顺序结构
-
JAVA的基本结构,除非特别指明,否则就按顺序一句一句执行
-
语句与语句之间,框与框之间是按从上到下的顺序进行的
它是由若干个依次执行的处理步骤组成的
三、选择结构
-
if选择结构
-
if单选结构
if(关系表达式){ //关系表达式为ture执行的语句 }
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(); /* 结果1: 请输入内容: hello 输出内容:hello end 结果2: 请输入内容: word end */ } }
-
if双选结构
if(关系表达式) { //如果关系表达式的值为true }else{ //如果关系表达式的值为false }
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(); /* 结果: 请输入成绩: 69 及格 */ } }
-
if多选择结构
if(关系表达式1) { ... } else if(关系表达式2) { ... } ... [else{ ... }] 注: if语句至多有1个else语句,else语句在所有的else if语句之后; if语句可以有若干个else if语句,它们必须在else语句之前; 一旦其中一个else if语句检测为true,其他的else if以及else语句都将跳过执行。
public class IfDemo03 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); 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 && score>=0){ System.out.println("不及格"); }else { System.out.println("成绩不合法"); } scanner.close(); /* 结果1: 请输入成绩: 102 成绩不合法 结果2: 请输入成绩: 78 C级 结果3: 请输入成绩: 40 不及格 */ } }
-
-
Switch结构
switch(表达式) { case 目标值1: 若干语句 break; case 目标值2: 若干语句 break; ...... default: 若干语句 break; }
public class SwitchDemo01 { public static void main(String[] args) { //case穿透 //switch匹配一个具体的值 char grade = 'C'; 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; case 'E': System.out.println("挂科"); break; default: System.out.println("未知等级"); } } }
四、循环结构
-
while循环
while(关系表达式){ ...... }
public class WhileDemo02 { public static void main(String[] args) { //计算1+2+3+...+100=? int i = 0; int sum = 0; while (i<=100){ sum = sum + i; i++; } System.out.println(sum);//结果为5050 } }
-
do…while循环
do{ ...... }while(关系表达式);
public class DoWhileDemo01 { public static void main(String[] args) { int i = 0; int sum = 0; do{ sum+=i; i++; }while (i<=100); System.out.println(sum);//结果为5050 } }
注:
While和do-while的区别:
while先判断后执行。do…while先执行后判断
do…while总是保证循环体会被至少执行一次
-
for循环
for(初始化表达式; 条件表达式; 条件改变表达式){ ...... } 注: for语句头部的三个表达式都是可选项,都可以没有。
public class ForDemo01 { public static void main(String[] args) { int sum=0; for (int i=1;i<=100;i++){ sum = sum + i; } System.out.println(sum);//结果为5050 } }
public class ForDemo02 { public static void main(String[] args) { //练习1:计算0到100之间的奇数和偶数的和 int oddSum = 0; int evenSum = 0; for (int i = 0; i <= 100; i++){ if (i%2!=0){ oddSum+=i; }else { evenSum+=i; } } System.out.println("奇数的和:"+oddSum);//奇数的和:2500 System.out.println("偶数的和:"+evenSum);//偶数的和:2450 } }
public class ForDemo03 { public static void main(String[] args) { //练习2:用while或for循环输出1-100之间能被5整除的数 //并且每行输出3个 for (int i = 0; i <=100; i++){ if (i%5==0){ System.out.print(i+"\t"); } if (i%(5*3)==0){ System.out.println(); } } } }
public class ForDemo04 { public static void main(String[] args) { //1.先打印一列/行 //2.把固定的1再用循环包起来 //3.去掉重复项,j<=i //4.调整样式 for (int i = 1; i <=9; i++){//外层循环控制行数 for (int j = 1; j <= i; j++){//内层循环控制每行表达式个数 System.out.print(j+"*"+i+"="+j*i+"\t"); } System.out.println();//换行 } /* 结果: 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 */ } }
-
for each循环
for(变量声明:数组){ ..... } 注: “变量声明”用于声明一个临时变量,其类型与数组元素的类型相同。for each循环自动的从前往后依次访问数组的每个元素。每访问一个元素,就把它的值存入临时变量中,这样就可以通过临时变量得到数组每个元素的值。
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.print(numbers[i]+"\t"); } System.out.println("\n"+"=================="); for (int x:numbers){//遍历数组 System.out.print(x+"\t"); } /* 结果: 10 20 30 40 50 ================== 10 20 30 40 50 */ } }
-
跳转语句
-
break: 跳出switch语句或当前的循环结构。
public class BreakDemo { public static void main(String[] args) { int i = 0; while (i<10){ i++; System.out.println(i); if (i == 2){ break; } } System.out.println(123); } /* 结果: 1 2 123 */ }
-
continue:跳过循环体中的剩余语句,继续往下执行。
public class ContinueDemo { public static void main(String[] args) { int i = 0; while (i<50){ i++; if (i%10==0){ System.out.println(); continue; } System.out.print(i+"\t"); } /* 结果: 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 */ } }
-
标签
public class LabelDemo { public static void main(String[] args) { //打印101-150之间所有的质数 //质数是指在大于1的自然数中, //除了1和它本身以外不再有其他因数的自然数 int count = 0; //不建议使用 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+" "); } /* 结果: 101 103 107 109 113 127 131 137 139 149 */ } }
-
五、练习
public class TestDemo {
public static void main(String[] args) {
//打印三角形5行
for (int i=1;i<=5;i++){
for (int j=5;j>=i;j--){
System.out.print(" ");
}
for (int j=1;j<=i;j++){
System.out.print("*");
}
for (int j=2;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
/*
for (int i=1;i<=5;i++){
for (int j=1;j<=5;j++){
if (j<i){
System.out.print(" ");
}else {
System.out.print("*");
}
}
for (int j=4;j>=1;j--){
if (j<i){
System.out.print(" ");
}else {
System.out.print("*");
}
}
System.out.println();
}*/
/*
结果1:
*
***
*****
*******
*********
结果2:
*********
*******
*****
***
*
*/
}
}