Java学习--流程控制

流程控制

用户交互Scanner

  • next()

    next()不能得到带有空格的字符串

    //创建一个扫描器对象,用于接收键盘数据
    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();
    
  • nextLine()

    Scanner scanner = new Scanner(System.in);
    
    System.out.println("使用nextLine方式接收:");
    
    if (scanner.hasNextLine()){
          
          
        String str = scanner.nextLine();
        System.out.println("输出的内容为:"+str);
    }
    scanner.close();
    
    Scanner scanner = new Scanner(System.in);
    
    System.out.println("请输入:");
    
    String str = scanner.nextLine();
    
    System.out.println("输出为:"+str);
    
    scanner.close();
    

顺序结构

从上到下,顺序执行

System.out.println("a");
System.out.println("b");
System.out.println("c");
System.out.println("d");

选择结构

if单选择结构
if (布尔表达式){
    
    

//如果布尔表达式为true将要执行的语句

}
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双选择结构
if (布尔表达式){
    
    
    
}else {
    
    
    
}
Scanner scanner = new Scanner(System.in);

System.out.println("请输入成绩:");
int i = scanner.nextInt();

if (i>=60){
    
    
    System.out.println("及格");
}else {
    
    
    System.out.println("不及格");
}

scanner.close();
if多选择结构
if (score==100){
    
    
    System.out.println("恭喜满分!");
}else if (score>=80 && score<=99){
    
    
    System.out.println("优秀");
}else if (score>=60 && score<=79){
    
    
    System.out.println("良好");
}else if (score>=0 && score<=59){
    
    
    System.out.println("不及格");
}else {
    
    
    System.out.println("成绩不合法 ");
}
switch多选择结构
//switch 匹配一个具体的值
char grade = 'B';

switch (grade){
    
    
    case 'A' :
        System.out.println("优秀");
        break;
    case 'B' :
        System.out.println("良好");
        break;
    case 'C' :
        System.out.println("及格");
        break;
    default:
        System.out.println("未知等级");

循环结构

while循环
	while(布尔表达式){
    
    
			//循环内容
}

只要布尔表达式为true,循环就会一直执行下去

//计算1+2+3...+100
int i = 0;
int sum = 0;

while (i<=100){
    
    
    sum= sum +i;
    i++;
}
System.out.println(sum);
do…while循环

先执行,后判断。最少会执行一次

do{
    
    
	//代码表达式
}while(布尔表达式);
//计算1+2+3...+100
int i = 0;
int sum = 0;

do{
    
    
    sum= sum +i;
    i++;
} while (i<=100);

System.out.println(sum);
for循环
for(初始值;布尔表达式;更新){
    
    
		//代码语句
}
//   初始化;条件判断;迭代
for (int i=1;i<100;i++){
    
    
    System.out.println(i);
}
        //打印九九乘法表
/**
 * 1.先打印第一列
 * 2.我们把固定的1再用一个循环包起来
 * 3.去掉重复项 i<=j
 * 4.调整样式
 */

        for (int j = 1; j <= 9; j++) {
    
    
            for (int i = 1; i <= j; i++) {
    
    
                System.out.print(j+"*"+i+"="+(j*i)+"\t");
            }
            System.out.println();
增强for循环
int[] nums = {
    
    10,20,30,40,50}; //定义了一个数组

//遍历数组的元素
for (int x:nums){
    
    
    System.out.println(x);

break & continue

break用于强制退出循环

int i=0;
while (i<100){
    
    
    i++;
    System.out.println(i);
    if(i==30){
    
    
        break;
    }
}

continue用于终止某次循环过程

即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定

练习

//打印三角形

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();
}

有任何问题,可以关注我的公众号: IT果力成
,一起学习交流~

猜你喜欢

转载自blog.csdn.net/qq_58372242/article/details/123504885