Java 基础(顺序、循环)

该博客主要介绍Java流程控制相关内容,包括用户交互Scanner的使用,顺序、选择、循环三种结构,其中选择结构有if单选、双选、多选、嵌套及switch;循环结构有while、do while、for循环,还提及break和continue的作用以及goto关键字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java流程控制

用户交互 Scanner

java.util.Scanner
next接收输入

package com.lqy.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();
        }
    }
}

nextline接收输入

package com.lqy.scanner;

import java.awt.*;
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.hasNext()){
            String str = scanner.nextLine();
            System.out.println("输出为:"+str);
        }
        scanner.close();

    }
}

本质代码

package com.lqy.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);
    }
}
package com.lqy.scanner;

import java.util.Scanner;

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

        //定义和
        double sum =0;
        int m = 0;

        //通过循环判断是否还有输入
        while (scanner.hasNextDouble()){
            double x = scanner.nextDouble();
             m = m +1;
             sum = sum +x;
            System.out.println("你输入了第"+m+"个数据"+"当前结果为:"+sum);
        }
        System.out.println(m+"个数的和为:"+sum);
        System.out.println(m+"个数的平均值为:"+(sum/m));

        scanner.close();
    }
}

顺序结构

Java的基本结构

package com.lqy.structure;

public class Demo01 {
    public static void main(String[] args) {
        System.out.println("hello1");
        System.out.println("hello2");
        System.out.println("hello3");
    }
}
//输出
hello1
hello2
hello3
选择结构
if单选
package com.lqy.structure;

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

        if(s.equals("Hello")){
            System.out.println("Hello");
        }
        System.out.println("end");
        scanner.close();
    }
}

if双选
import java.util.Scanner;

public class IfDemo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入成绩:");
        double score = scanner.nextDouble();

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

if多选
import java.util.Scanner;

public class IfDemo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入成绩:");

        Double score = scanner.nextDouble();
        if(score>=90){
            System.out.println("优秀");
        } else if (score>=60 && score<90) {
            System.out.println("优良");
        }else {
            System.out.println("不及格");
        }
        scanner.close();
    }
}
if嵌套
switch

switch case 判断一个变量与一系列值中某个值是否相等
Java SE 7开始支持字符串

public class SwitchDemo01 {
    public static void main(String[] args) {
        //case穿透     //switch匹配具体值
        char grade = 'B';
        switch (grade){
            case 'A':
                System.out.println("优秀");
                break;//可选,没有会把后面的都输出
            case 'B':
                System.out.println("良好");
            case 'C':
                System.out.println("及格");
            case 'D':
                System.out.println("再接再厉");
            case 'E':
                System.out.println("挂科");
            default:
                System.out.println("未知等级");
        }
//输出
良好
及格
再接再厉
挂科
未知等级
package com.lqy.structure;

public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "小草";
        switch (name){
            case "花朵" :
                System.out.println("错误");
                break;
            case "水果" :
                System.out.println("错误");
                break;
            case "小草" :
                System.out.println("正确");
                break;
            case "树木":
                System.out.println("错误");
                break;
        }
    }
}

循环结构

while先判断后循环
do while先循环后判断

while循环

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

public class whileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            System.out.println(i);
        }
    }
}

死循环:
while(true){
//循环内容
}

public class WhileDemo03 {
    public static void main(String[] args) {
        int i = 0 ;
        int sum =0;
        while (i<101){
            sum = sum +i;
            i = i+1;
        }
        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  = sum + i;
            i++;
        }
        while (i < 101);
            System.out.println(sum);
    }
}
public class DowhileDemo02 {
    public static void main(String[] args) {
        int a=0;
        a++;
        while(a<0){
            System.out.println(a);
        }
        System.out.println("--------------------------");
        do {
            System.out.println(a);
            a++;
        }while (a<0);
    }
    //--------------------------
   //1
for 循环

快捷键:100.for
for(初始条件; 循环条件;更新){//代码语句}

public class ForWhileDemo02 {
    //输出9*9乘法表
    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= 9; j++) {
                System.out.print(i*j+" ");
                if (j==9){
                    System.out.println();
                }
            }
        }
    }
}

输出9*9乘法表

public class ForWhileDemo02 {
    //输出9*9乘法表
    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i+"*"+j+"="+i*j+" ");
                if (j == i){
                    System.out.println();
                }
            }
        }
    }
}
//输出
1*1=1 
2*1=2 2*2=4 
3*1=3 3*2=6 3*3=9 
4*1=4 4*2=8 4*3=12 4*4=16 
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 

增强for循环

public class ForDemoe03 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};//定义了一组数组
        //遍历数组的元素
        for (int x:numbers){
            System.out.println(x);
        }
    }
}

break and continute

break 用于强制退出循环

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

continute 退出某次循环(一般用于跳过某些东西)

public class ContinuteDemo {
    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);
        }
    }
}
//输出
123456789
111213141516171819
212223242526272829
313233343536373839
414243444546474849
515253545556575859
616263646566676869
717273747576777879
818283848586878889
919293949596979899
goto关键字
public class LableDemo {
    public static void main(String[] args) {
        //打印101-150之间的质数,质数:只能被1和它本身整除
        int count = 0;
        outer:for (int i = 100; 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 TextDemo01 {
    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 =1 ; j<i; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
     *
    ***
   *****
  *******
 *********
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值