1.用户交互Scanner
package com.song.scanner; import java.util.Scanner; public class Demo1 { public static void main(String[] args){ //创建一个扫描器对象,用于接收键盘数据,输入hello world Scanner scanner = new Scanner(System.in); System.out.println("使用next的接收方式:"); //判断用户有无字符串输入 if (scanner.hasNext()){ //使用next方式接收 String str =scanner.next(); System.out.println("输出的内容为:"+str); //hello } //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉 scanner.close(); } }
package com.song.scanner; import java.util.Scanner; public class Demo2 { public static void main(String[] args){ //从键盘接收数据,输入hello world Scanner scanner = new Scanner(System.in); System.out.println("使用nextLine的接收方式:"); //判断是否还有输入 if (scanner.hasNextLine()){ String str =scanner.nextLine(); System.out.println("输出的内容为:"+str); //输出hello world } scanner.close(); } }
2.Scanner进阶使用
package com.song.scanner; import java.util.Scanner; public class Demo4 { public static void main(String[] args){ //输入多个数字,并求和以及平均数,每输入一个数字用回车确认,输入非数字来结束输入并执行结果 Scanner scanner=new Scanner(System.in); //求和 double sum=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(); } }
3.顺序结构
语句与语句之间,框与框之间按从上到下的顺序进行。
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"); } }
4.选择结构
if单选择结构
public class IfDemo1 { 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双选择结构
public class IfDemo2 { public static void main(String []args){ 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多选择结构
public class IfDemo3 { public static void main(String[] args){ Scanner scanner =new Scanner(System.in); System.out.println("请输入成绩:"); int score =scanner.nextInt(); if (score<60&&score>=0){ System.out.println("不及格"); } else if (score<=70) { System.out.println("中等"); } else if (score<=80) { System.out.println("良好"); }else if(score<=100){ System.out.println("优秀"); }else{ System.out.println("成绩不合法,满分为一百分!"); } scanner.close(); } }
嵌套if结构
Switch多选择结构
public class SwitchDemo1 { public static void main(String[] args){ 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; default: System.out.println("未知等级"); break; } } }
public class SwitchDemo2 { public static void main(String[] args){ String name ="艾瑞利亚"; //表达式结果可以是字符串 switch (name){ case "艾瑞利亚": System.out.println("我是艾瑞利亚"); break; case "亚索": System.out.println("我是亚索"); break; default: System.out.println("出错了"); } } }
do...while循环
public class DowhileDemo2 { public static void main(String[] args){ int a=0; while(a<0){ a++; System.out.println(a); } System.out.println("=============="); do { a++; System.out.println(a); }while(a<0); } }
-
对于while语句而言,如果不满足条件,则不能进入循环;
-
do...while循环和while循环相似,不同的是,do...while循环至少会执行一次;
-
while先判断后执行,do...while循环先执行后判断
-
do...while循环总是保证循环体被至少执行一次!
public class DowhileDemo2 {
public static void main(String[] args){
int a=0;
while(a<0){
a++;
System.out.println(a);
}
System.out.println("==============");
do {
a++;
System.out.println(a);
}while(a<0);
}
}
For循环
格式:for(初始值;条件判断;迭代){
循环体...........
}
public class ForDemo2 { public static void main(String[] args){ //计算0到100之间奇数和偶数的和 int oddsum=0; //奇数和 int evensum=0; //偶数和 for (int i = 0; i <= 100; i++) { if(i%2==0) { evensum = evensum + i; }else{ oddsum+=i; } } System.out.println("偶数的和"+evensum); System.out.println("奇数的和"+oddsum); } }
public class ForDemo3 { public static void main(String[] args){ //for循环输出1--1000之间能被5整除的数,且每行输出三个 for (int i = 0; i <= 1000; i++) { if (i%5==0){ System.out.print(i+"\t"); } if (i%(5*3)==0){ System.out.println(); //System.out.print("\n"); } } //println输出会换行 //print输出不会换行 } }
public class WhileDemo1 { public static void main(String[] args){ //while循环输出1--1000之间能被5整除的数,且每行输出三个 int i=0; while(i<=1000){ i++; if (i%5==0){ System.out.print(i+"\t"); } if (i%(5*3)==0){ System.out.println(); } } } }
打印九九乘法表
public class ForDemo4 { public static void main(String[] args){ //打印九九乘法表,使用For循环的嵌套 for (int j = 1; j <= 9; j++) { for (int i = 1; i <= j; i++) { System.out.print(i+"*"+j+"="+(j*i)+"\t"); } System.out.println(); } } }
增强For循环
public class ForDemo5 { 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语句
break:结束循环体,继续执行下面的语句
continue:结束当次循环,继续执行下次循环(满足循环判定条件下)
public class BreakDemo1 { public static void main(String[] args) { int i=0; while (i<=100){ i++; System.out.println(i); if (i==10){ break; } } System.out.println("123"); } }
public class ContinueDemo1 { public static void main(String[] args) { int i=0; while(i<20) { i++; if (i % 10 == 0) { continue; } System.out.print(i+"\t"); } } }
练习:打印三角形
public class TextDemo { public static void main(String[] args) { //打印三角形 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=1;j<i;j++){ System.out.print("*"); } System.out.println(); } } }