1. Write a program to count the number of 9 in all integers from 1 to 100
Idea: judge whether the number on the one digit and ten digit is 9. If it is 9, it is count++
public class Datatype { public static void main(String[] args) { int count = 0; for(int i=1;i<=100;i++){ if(i%10==9){ count++; } if(i/10==9){ count++; } } System.out.println(count); } }
Operation results:
2. Print all prime numbers between 1 and 100
Prime number: prime number is also called prime number. A natural number greater than 1, except 1 and itself, which cannot be divided by other natural numbers, is called a prime number; Otherwise, it is called a composite number (rule 1 is neither a prime number nor a composite number).
public class Datatype { public static void main(String[] args) { boolean flog; for(int i=2;i<=100;i++) { flog = true; for (int j = 2; j <i ; j++) { if (0==i % j ) { flog = false; } } if (flog == true) { System.out.print(i+" "); } } } }
Operation results:
3. Number guessing game
After completing the number guessing game, the user enters a number to judge whether the number is greater than, less than, or equal to the randomly generated number (1 ~ 100)
import java.util.Random; import java.util.Scanner; public class Datatype { public static void main(String[] args) { Random rand = new Random(); int num = rand.nextInt(100)+1; Scanner sc = new Scanner(System.in); while(true){ System.out.print("Please enter a shaping data>"); int data = sc.nextInt(); if(data < num){ System.out.println("Small"); }else if (data > num) { System.out.println("Big"); }else { System.out.println("You guessed right"); break; } } sc.close(); } }
Operation results:
4. Calculate the value of 1 / 1-1 / 2 + 1 / 3-1 / 4 + 1 / 5... + 1 / 99 - 1 / 100
public class Datatype { public static void main(String[] args) { double sum = 0; int j=1; for (int i = 1; i <= 100; i++) { sum=sum+(1.0/i)*j; j=-j; } System.out.println(sum); } }
Operation results:
5. Find the maximum common divisor of two positive integers
import java.util.Scanner; public class Datatype {public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Please enter the first integer"); int m = sc.nextInt(); System.out.print("Please enter the second integer"); int n = sc.nextInt(); main(m,n); } public static void main(int m,int n){ if(m<n){ int temp = m; m = n; n = temp; } while(m % n != 0){ int temp = m % n; m = n; n = temp; } System.out.println(m+"and"+n+"The maximum common divisor of is:"+n); } }
Operation results:
6. Find an integer and the number of binary 1 when stored in memory
Idea:
1. Decimal will be automatically converted to binary when bitwise and operation is used
2. Sum it with 1. If 1 is obtained, the last bit of the integer is 1;
3 using the shift right operator > >, shift one bit to the right each time to continuously remove the rightmost bit of the integer, so as to judge how many 1s there are
import java.util.Scanner; public class Datatype { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Please enter an integer"); int m = sc.nextInt(); int count = 0; for(int i = 0;i <32;i++) { if (((m >> i) & 1) == 1) { count++; } } System.out.printf("The binary of this integer has%d One",count); } }
Operation results:
7. Obtain all even and odd bits in a binary sequence and output the binary sequence respectively
import java.util.Scanner; public class Datatype { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Please enter an integer"); int m = sc.nextInt(); int i = 0; System.out.println("The even sequence is:"); for(i = 31;i >= 1;i=i-2 ) { System.out.print((m>>i)&1); } System.out.println("\n The odd sequence is:"); for(i = 30;i >= 0;i=i-2 ) { System.out.print((m>>i)&1); } } }
Operation results:
8. Write code to simulate the scenario of three password input. You can enter the password up to three times. If the password is correct, you will be prompted with "login succeeded". If the password is wrong, you can re-enter it up to three times. If all three times are wrong, you will be prompted to exit the program
import java.util.Scanner; public class Datatype { public static void main(String[] args){ System.out.println("Welcome"); for(int i=0;i<3;i++){ System.out.println("Please enter a 6-digit password:"); Scanner scan = new Scanner(System.in); String a = scan.next(); if(a.equals("257832")){ System.out.println("Login successful"); break; } else{ System.out.println("Wrong password, please re-enter, you still have"+(2-i)+"Input opportunities"); } } } }
Operation results:
9. Output each bit of an integer. For example, each bit of 123 is 1, 2, 3
import java.util.Scanner; public class Datatype { public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.println("Please enter a three digit integer:"); int a = scan.nextInt(); if(a>99 && a<1000){ System.out.println("The hundredth is:"+(a/100)); System.out.println("Ten are:"+(a/10)%10); System.out.println("The bits are:"+(a%100)%10); } else{ System.out.println("Input error, please re-enter!"); } } }
Operation results:
The above is the whole content of this meeting~~