java流程控制
1.scanner对象(JDK5)
1.1 hasNext()与next()
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("s输入的内容为:"+str);
}
//凡是属于IO流的类如果不关闭会一直占用资源
scanner.close();
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hS2NhDDo-1615202052014)(C:\Users\23236\AppData\Roaming\Typora\typora-user-images\image-20200905104438880.png)]
1.2hasNextLine()与nextLine()
public static void main(String[] args) {
//创建一个扫描器对象,用于接受键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接受:");
//判断是否还有输入数据
if (scanner.hasNextLine()){
//使用nextLine方式接受
String str = scanner.nextLine();
System.out.println("s输入的内容为:"+str);
}
//凡是属于IO流的类如果不关闭会一直占用资源
scanner.close();
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sYcWbXV8-1615202052018)(C:\Users\23236\AppData\Roaming\Typora\typora-user-images\image-20200905104618941.png)]
**
上述方法总结
**hasnext()方法以空白符为结束标志,hasNextLine()方法以enter为结束标志
1.3 其他方法:
Scanner还有hasNextInt()+nextInt(),hasNextDouble()+nextDouble()等方法
while (scanner.hasNextDouble()){
double v = scanner.nextDouble();
sum += v;
m++;
}
System.out.println("输入数据的和:"+sum);
System.out.println("输入数据的平均数:"+sum/m);
2.顺序结构
从上到下执行:若干个依次执行处理步骤组成,基本算法结构 3.选择结构
3.1 if语句
if(){}
if(){}else{}
if(){}else if(){}else if(){}else{}
while (scanner.hasNextDouble()) {
if (scanner.nextDouble() < 60.0) {
System.out.println("不及格");
System.out.println(scanner.nextDouble());//此处还是本次输入的数据吗?不是的,这里他会等待下一个输入。
}
}
//if-else if-else
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入成绩0-100:");
while (scanner.hasNextDouble()) {
double score = scanner.nextDouble();
if (score < 60) {
System.out.println("不及格");
} else if (score < 80) {
System.out.println("一般");
} else if (score < 90) {
} else {
System.out.println("优秀");
}
System.out.print("输入成绩0-100:");
}
scanner.close();
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MISceREj-1615202052020)(C:\Users\23236\AppData\Roaming\Typora\typora-user-images\image-20200905123301944.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-T5sYttGk-1615202052024)(C:\Users\23236\AppData\Roaming\Typora\typora-user-images\image-20200905123637475.png)]
3.2 swtich语句
//swtich 匹配一个具体的值
//case穿透 break语句终止穿透
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
switch (input) {
case "A":
System.out.println("A等级");
break;
case "B":
System.out.println("B等级");
break;
case "C":
System.out.println("C等级");
break;
case "D":
System.out.println("D等级");
break;
default:
System.out.println("未知");
}
scanner.close();
}
//jdk7后switch支持String
//jave-class(字节码文件)->反编译->java(IDEA)
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
byte var4 = -1;
switch(input.hashCode()) {
case 65:
if (input.equals("A")) {
var4 = 0;
}
break;
case 66:
if (input.equals("B")) {
var4 = 1;
}
break;
case 67:
if (input.equals("C")) {
var4 = 2;
}
4.循环结构
4.1 while循环
public static void main(String[] args) {
int i = 1;
int sum = 0;
while(i<=100){
sum += i;
i++;
}
System.out.println(sum);
}
// 先判断在执行
4.2 do while循环
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);
}
//==========================
//0
//先执行后判断,至少执行一次。
4.3 for循环
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int m =1;m <= i;m++){
System.out.print(m+"x"+i+"="+i*m+"\t");
}
System.out.println();
}
}
4.4 增强for循环
//jdk5引入,主要用来循环数组,集合
int[] numbers = {1,2,3,4,5};
for (int num:numbers){
System.out.println(num);
}
4.5 break和continue
//break终止循环,直接退出循环
//continue立即退出本次循环,执行下一次循环
int i = 1;
while(i<10){
if (i == 6) break;
System.out.println(i);//1,2,3,4,5
i++;
}
System.out.println("==================");
//continue
int m = 1;
while(m<10){
if (m == 6) {
m++;
continue;
}
System.out.println(m);//1,2,3,4,5,7,8,9
m++;
}
//注意看m++区别
for (int m = 0; m < 10; m++) {
if (m == 6) continue;
System.out.println(m);//1,2,3,4,5,7,8,9
}
//打印三角形 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 = 1; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
+) {
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();
}