Classic examples of Java entry (two)

Classic examples of Java entry (two)


This issue still brings you classic examples of Java entry.
1. Write a program to count how many numbers 9 appear in all integers from 1 to 100.
Upload code

import java.util.Scanner;

import java.util.Scanner;

public class TextDemo {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个1~100正整数");
        int num1 = scanner.nextInt();
        int num2 = 0;
        if (num1 < 9){
    
    
            System.out.println("没有出现过9");
        }else {
    
    
            for (int i = 1; i <= num1 ; i++){
    
    
                if (i % 10 == 9){
    
    
                    num2 ++;
                }
                if (i / 10 == 9 && i % 10 != 9){
    
    
                    num2++ ;
                }
            }
            System.out.println("9的个数为:" + num2);
        }
    }
}

The running result is as follows.
When the input number is less than 9,
Insert picture description here
when the input number is greater than 9,
Insert picture description here

2. Find all the "number of daffodils" between 0 and 999 and output.
("Daffodil number" refers to a three-digit number, and the sum of the cubes of the digits is exactly equal to the number itself. For example: 153=1 3 +5 3 +3 3 , then 153 is a "daffodil number".)

public class TextDemo {
    
    
    public static void main(String[] args) {
    
    
        for(int i = 100; i < 1000; i++ ){
    
    
            int a = i / 100;//求百位上的数字
            int b = i / 10 % 10;//求十位上的数字
            int c = i % 10;//求个位上的数字
            if (i == (a * a * a + b * b * b + c * c * c)){
    
    
                System.out.println(i);
            }
        }
    }
}

Insert picture description here

3. Write code to simulate three password input scenarios.
The password can be entered up to three times; if the password is correct, it will prompt "login successful"; if the password is wrong, you can re-enter it up to three times. If all three times are wrong, it prompts to exit the program.
Upload code

import java.util.Scanner;

public class TextDemo {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你设置的数字密码");
        long keyword = scanner.nextLong();
        judge(keyword);
    }
    public static void judge(long keyword) {
    
    
        System.out.println("请输入你的密码");
        Scanner scanner = new Scanner(System.in);
        long num = scanner.nextLong();
        for (int i =0; i < 2;i++){
    
    
            if (num == keyword){
    
    
                System.out.println("登陆成功");
                return;
            }else {
    
    
                System.out.println("密码错误,请重新输入");
                num = scanner.nextLong();
            }
        }
        System.out.println("错误超过三次,程序将退出");
    }
}

The results are as follows. When the
login is successful,
Insert picture description here

When the login fails,
Insert picture description here

4. Write a function to return the number of 1s in the parameter binary.
For example: The binary value of 15 is 0000 1111, and there are 4 ones.

import java.util.Scanner;

public class TextDemo {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个整数");
        int num = scanner.nextInt();
        int count = 0;
        for (int i = 0;i < 32;i++){
    
    
            if (((num >> i) & 1) == 1){
    
    
                count ++ ;
            }
        }
        System.out.println(count);
    }
}

The results are as follows
Insert picture description here

5. Get all the even and odd bits in a binary sequence of numbers, and output the binary sequence respectively.

import java.util.Scanner;

public class TextDemo {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个数字");
        int num = scanner.nextInt();
        for (int i = 0;i < 32 ;i += 2){
    
    
            System.out.print((num >> i) & 1);
        }
        System.out.println();
        for (int i = 1;i < 32 ;i += 2){
    
    
            System.out.print((num >> i) & 1);
        }
        System.out.println();
    }
}

The results are as follows
Insert picture description here

6. Output each bit of an integer
Here, I used the recursive method.

import java.util.Scanner;

public class TextDemo {
    
    
      public static void main(String[] args) {
    
    
      Scanner scanner = new Scanner(System.in);
      System.out.println("请输入一个数字");
      int num = scanner.nextInt();
      Print(num);
  }

  public static void Print(int num) {
    
    
      if (num >9)
          Print(num /10);
      }
      System.out.print(num % 10 + " ");
  }
}

Results are as follows
Insert picture description here
VII. Guessing game

import java.util.Random;
import java.util.Scanner;;

public class TestDemo {
    
    
    public static void main(String[] args) {
    
    
        Random random = new Random(); // 默认随机种子是系统时间
        Scanner sc = new Scanner(System.in);
        int toGuess = random.nextInt(100);
        // System.out.println("toGuess: " + toGuess);
        while (true) {
    
    
            System.out.println("请输入要输入的数字: (1-100)");
            int num = sc.nextInt();
            if (num < toGuess) {
    
    
                System.out.println("小了");
            } else if (num > toGuess) {
    
    
                System.out.println("大了");
            } else {
    
    
                System.out.println("恭喜回答正确");
                break;
            }
        }
        sc.close();
    }
}

The results are as follows
Insert picture description here

8. Print X graphics
When i == j, it will print'x'; when i + j == num + 1 it will print'x'; otherwise, it will print "" ".

import java.util.Scanner;

public class TextDemo {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入打印行数:");
        int num= scanner.nextInt();
        //第一层循环控制行数
        for(int i = 1;i <= num;i++){
    
    
            //第二层循环控制行
            for(int j = 1;j <= num;j++){
    
    
                //当 i == j 时会打印 'x' ;当i + j == num + 1 时会打印'x'
                if(j == i || j + i == num + 1) {
    
    
                    System.out.print("x");
                }
                else
                    System.out.print(" ");
            }
            System.out.print("\n");
        }
    }
}

The results are as follows
Insert picture description here

The classic example of Java entry is almost over, and the next issue will return to Java entry. Thank you Tiezi for browsing, see you next time.

Guess you like

Origin blog.csdn.net/Kaiiiiiiiiiiiiii/article/details/112371587