Java逻辑控制及实例


输入输出

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

综合练习

水仙花数的变种

求出n以内的所有的特殊数字

public static void find(int n) {
        for (int i = 1; i <=n; i++) {
            int tmp = i;
            int count = 0;
            int sum = 0;//记得将sum放在for循环里面,因为每次if判断完回来都要将sum置空
            while (tmp != 0) {
                count++;
                tmp/=10;
            }
            tmp = i;
            while (tmp != 0) {
                sum+=Math.pow(tmp % 10, count);
                tmp /= 10;
            }
            if (sum == i) {
                System.out.println(i);
            }
        }

    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            find(n);
        }
    }

求一个数的二进制中有多少个1

事实上,只要 &1 就可以得到一个数的最右边的二进制位

方法一:(& >>>)

image-20220422151024998

public static int numOfOne(int n) {
        int count = 0;
        while (n != 0) {

            if ((n & 1) == 1) {   //n&1就是取出最右边的二进制数
                count++;
            }
            n = n >>> 1;//无符号右移是为了在左边补0,要是直接>>就会补符号位
        }
        return count;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            System.out.println(numOfOne(n));
        }
    }

方法二 (&下一个数) :(更加好)

image-20220422152035643

 public static int numOfOne(int n) {
        int count = 0;
        while (n != 0) {
            count++;
            n = n & (n - 1);
        }
        return count;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            System.out.println(numOfOne(n));
        }
    }

取出一个数的奇数位和偶数位

public static void print(int n) {
        for (int i = 30; i>=0 ; i-=2) {
            System.out.print(((n>>i) & 1)+" ");
        }
        System.out.println();
        for (int i = 31; i >=0; i-=2) {
            System.out.print(((n>>i) & 1)+" ");
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            print(n);
        }
    }

全部评论
就喜欢这种有实列的,谢谢分享啊,学到了
点赞 回复 分享
发布于 2022-08-15 21:05

相关推荐

草稿猫编程:查看图片
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客企业服务