逻辑控制(实例讲解)

输入输出

import java.util.Scanner;
public class TestDemo1{
public static void main(String[] args){
    Scanner scan =new Scanner (System.in);
	int a=scan.nextInt();//输入一个整型
	System.out.println(a);
	String str=scan.next();//输入一个字符串
	System.out.println(str);
	}
}

要是使用

String str=scan.nextLine();
System.out.println(str);

打印出数字之后就会结束程序,因为str把回车键录了进去,所以读入int和String 类型时,应该使用String str=scan.next();

使用scan.next读到空格就结束了,而使用scan.nextLine可以正常读入带有空格的字符串

image-20220329211131593

import java.util.Scanner;
public class TestDemo2{
public static void main(String[] args){
	Scanner scan =new Scanner (System.in);
	int a=scan.nextInt();
	System.out.println(a);
	//String str=scan.nextLine();//会读入空格,无法录入字符串
	//System.out.println(str);
	String str=scan.next();//可以接着数字后面输入字符串
	System.out.println(str);
}
}
//但是单独输入字符串,String str=scan.nextLine()就可以打印出带空格的完整的字符串,而
//String str=scan.next()只会打印到空格之前的字符串

顺序结构

正常写就行

分支结构

if else

if括号里面的必须是布尔类型的表达式

eg:if(a==10)

image-20220331093136923

判断闰年
import java.util.Scanner;//包
public class TestDemo1{
public static void main(String[] args){
    Scanner scan =new Scanner (System.in);
	while(scan.hasNextInt()){		//多组输入
			int year=scan.nextInt();
			if((year%4==0&&year%100!=0)||(year%400==0))
			{
				System.out.println("闰年");
			}
			else {
			System.out.println("不是闰年");
	        }
	}	
}
}

else会与最近的未匹配的if结合

if语句只会进入一个条件里面

(多组输入结束的方法:按ctrl+C(强制终止)或者 ctrl+D也可以结束程序)

switch语句

import java.util.Scanner;
public class TestDemo1{
public static void main(String[] args){
    Scanner scan =new Scanner (System.in);
	while(scan.hasNextInt()){     //多组输入
		int a=scan.nextInt();
        switch(a){    //写switch语句时,(在没有特殊情况下)不要忘记写break
            case 1:
            System.out.println("1");
            break;
            case 2:
            System.out.println("2");
            break;
            default :
            System.out.println("错误");
            break;
        }
	}
}   
}

哪些类型不能作为switch参数?

long float double boolean

String 和 枚举也是可以作为switch的参数

循环语句

while循环

与if语句一样,while语句后面的参数也要是boolean类型

public class TestDemo2{
public static void main(String[] args){
	int a=10;
	while(a>=0){
		System.out.println("yes");
		a--;
	}
}
}
求5的阶乘
public class TestDemo2{
public static void main(String[] args){
     int i=1;
	int ret=1;
	int num=5;
	int sum=0;
	while(i<=5){
		ret*=i;
		i++;
		sum+=ret;
	}
	System.out.println(sum);
	}
}
break与continue

break是跳出循环,break是在循环和循环中使用

continue是解释本次循环,continue只能在循环中使用

for循环

//求5!+4!+3!+2!+1!
import java.util.Scanner;
public class TestDemo3 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
		int a=scan.nextInt();//5
		int ret=1;
		int sum=0;
        //方法一:
		for(int j=1;j<=a;j++){
			for(int i=1;i<=j;i++){
				ret*=i;
			}
			sum+=ret;
			ret=1;//记得重置
		}
		System.out.println(sum);
        //方法二:
		//for(int i=1;i<=a;i++){
		//	ret*=i;
		//	sum+=ret;
		//	
		//}
		//System.out.print(sum);
   }

}
求n!+(n-1)!+(n-2)!+………+1!(多组输入)
 public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int i = 1;
            int ret = 1;
            int sum=0;
            while (i<=n) {
                ret *= i;
                sum += ret;
                i++;
            }
            System.out.println(sum);
        }

do{
循环语句;
}while(循环条件);
先执行循环语句, 再判定循环条件.

猜数字游戏
import java.util.Random;
import java.util.Scanner;

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

        Random random = new Random();
        int randNum = random.nextInt(100);[0-99]
        while (true) {
            System.out.println("请输入数字:");
            int num = scan.nextInt();
            if (num < randNum)
                System.out.println("猜小了");
            else if (num > randNum) {
                System.out.println("猜大了");
            } else {
                System.out.println("恭喜你,猜对了");
                break;
            }
        }
    }
}


根据年龄, 来打印出当前年龄的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)

import java.util.Scanner;

public class JudgeAge {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n=scanner.nextInt();
            if (n <= 18) {
                System.out.println("少年");
            } else if (n < 28) {
                System.out.println("青年");
            } else if (n <= 55) {
                System.out.println("中年");
            } else {
                System.out.println("老年");
            }
        }

    }
}
打印 1 - 100 之间所有的素数
public class PrintPrimeNum {
   public static void main(String[] args) {
        int count = 0;
        int j = 0;
        for (int i = 1; i <= 100; i += 2) {
            for (j = 2; j <= Math.sqrt(i); j++) {
                if (i % j == 0) {
                    break; //i就不是素数了
                }
            }
            if (j > Math.sqrt(i)) {
                System.out.print(i+"  ");
                count++;
            }
            //每行打印6个数字
            if (count % 6 == 0) {
                System.out.println();
            }
        }
    }
}

最大公约数(辗转相除法)
import java.util.Scanner;

public class GCD {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int a=scanner.nextInt();
            int b=scanner.nextInt();
            while (a % b != 0) {
                int c = a % b;
                a = b;
                b = c;
            }
            System.out.println(b);
        }

    }
}

输入密码
import java.util.Scanner;

public class Password {
    public static void main(String[] args) {
        int n = 3;
        while (n != 0) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入密码:");
            String str=scanner.nextLine();
            if (str.equals("123456")) {
                System.out.println("密码正确");
                break;
            }
            n--;
            if (n == 0) {
                System.out.println("你已经失去机会");
                break;
            }
            System.out.println("你还有"+n+"次机会");
        }

    }
}

image-20220401225624704

image-20220401225643820

do while 循环

先执行在判断

 public static void main(String[] args) {
        int i = 0;
        int sum=0;
        do {
            sum += i;
            i++;
        } while (i<=10);
        System.out.println(sum);
    }
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值