If you are just new to Java or just learning Java, it is still necessary to practice some basic algorithms, which can improve the use of thinking and syntax
1. Output the maximum value of two int numbers
import java.util.Scanner; public class demo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter two integers in sequence: a,b (separated by spaces)"); /*Compare the sizes of the two numbers*/ int a = scanner.nextInt(); int b = scanner.nextInt(); int max; if(a >= b){ max = a; }else { max = b; } System.out.println("Maximum value is"+max); } } }2. Output the maximum value of the three int numbers
package demo; import java.util.Scanner; public class demo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter two integers in turn: a,b (separated by spaces)"); int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); scanner.close(); /*Method 1*/ int d=(a>b)?a:b; int e=(d>c)?d:c; System.out.println("maximum value is"+e); /*Method 2*/ if(a>b && a>c){ System.out.println("maximum value is"+a); }else if(b>c && b>a){ System.out.println("maximum value is"+b); }else if(c>b && c>a){ System.out.println("maximum value is"+c); }else{ System.out.println("Exception occurred"); } } }3. Write a program to determine whether a year is a leap year
package demo; import java.util.Scanner; /*Judges that a leap year is input by the user, and can be divisible by 4 but cannot be divisible by 100, or can be divisible by 400, which is a leap year. It is required to determine whether a year is a leap year. Required output: Is this year a leap year*/ public class demo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter the year:"); int year = scanner.nextInt(); /*Method 1*/ if((year % 4 ==0 && year % 100 !=0) || year%400 ==0){ System.out.println("This year is a leap year"); }else{ System.out.println("This year is not a leap year"); } /*Method 2*/ boolean isLeapYear = (year % 4 ==0 && year % 100 !=0) || year%400 ==0; String string = isLeapYear?year+"is leap year":year+"not leap year"; System.out.println(string); } }4. Complete the score level output program: If the score entered by the user is correct (0-100), calculate the level corresponding to the score according to the rules in Table-1 and calculate the result.
package demo; import java.util.Scanner; /* * Score grade classification table* >= 90 A * >=80 B * >=60 C * <60 D * * Score range: 0-100 * * 2 judgments are required*/ public class demo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter score: "); double score = scanner.nextDouble(); scanner.close(); if(score < 0 || score >100){ System.out.println("The input score is not between 0-100, does not meet the requirements"); }else if(score >= 90){ System.out.println("A"); }else if(score >= 80){ System.out.println("B"); }else if(score >= 60){ System.out.println("C"); }else{ System.out.println("D"); } } }5. Complete the command resolution program: There is a command resolution program, which provides three function selections for users to choose. After the user selects a certain function, the program outputs the function name selected by the user on the interface. The interaction of the program is shown in the figure:
package demo; import java.util.Scanner; /* * There is a command parsing program, which provides three function selections for users to choose. * After the user selects a certain function, the program outputs the function name selected by the user on the interface. * * */ public class demo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please select functions: 1. Show all records 2. Query login records 0. Exit"); int command = scanner.nextInt(); scanner.close(); switch (command) { case 0: System.out.println("Welcome"); break; case 1: System.out.println("Show all records..."); break; case 2: System.out.println("Query login record..."); break; default: System.out.println("Input error!"); } } }6. Complete the cash register payment program: Write a cash register payment program, calculate based on the unit price, purchase quantity, and payment collection, and output the receivable amount and change; when the total price is greater than or equal to 500, enjoy a 20% discount. The console interaction is as follows:
package demo; import java.util.Scanner; /* * Requirements: * Write a cashier counter payment program. Calculate and output the receivable amount and change redeemable based on the unit price, purchase quantity and payment collection; * When the total price is greater than or equal to 500, enjoy a 20% discount. * */ public class demo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter unit price (¥):"); double price = scanner.nextDouble(); System.out.println("Please enter quantity:"); double amount = scanner.nextDouble(); System.out.println("Please enter the collection amount:"); double count = scanner.nextDouble(); double totalMoney = price*amount; if(totalMoney > 500){ totalMoney = totalMoney*0.8; } double change = count - totalMoney; System.out.println("The amount receivable is: "+totalMoney + "change is: "+change); } }7. Java enters three integers from the keyboard to realize sorting from small to large.
package demo; import java.util.Scanner; /* * java enters three integers from the keyboard to realize sorting from small to large* **/ public class demo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter three integers, separated by spaces:"); int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); scanner.close(); System.out.println("The input value is: a = " + a + ", b = " + b + ", c = " + c); if(a > b){ if ( b > c) { System.out.println("The sorted value is: " + c + "," + b + "," + a); }else if( c > a){ System.out.println("The sorted value is: " + b + "," + a + "," + c); }else{ System.out.println("The sorted value is: " + b + "," + a + "," + c); } }else{ if(c < a){ System.out.println("The sorted value is: " + c + "," + a + "," + b); }else if(c > b){ System.out.println("The sorted value is: " + a + "," + b + "," + c); }else{ System.out.println("The sorted value is: "+ a + "," + c + "," + b); } } } } }8. Calculate personal income tax. The formula for calculating personal income tax in Beijing: Taxable amount = (Wage and Salary Income - Deduction) *Applicable tax rate - Quick deduction number Among them, the deduction number is 3500. The applicable tax rate and quick deduction number are shown in the following table:
package demo; import java.util.Scanner; /* * Formula to calculate personal income tax in Beijing: Taxable amount = (Wages and Salary Income - Deductions) *Applicable tax rate - Quick calculation of deductions Among them, the deduction is 3500 */ public class demo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter your pre-tax salary:"); int salaryBeforeTax = scanner.nextInt(); scanner.close(); int taxSalary = salaryBeforeTax - 3500; double tax; /* Method 1*/ tax = taxSalary<0?0.0: taxSalary<=1500?taxSalary*0.03: taxSalary<=4500?taxSalary*0.1-105: taxSalary<=9000?taxSalary*0.2-555: taxSalary<=35000?taxSalary*0.25-1005: taxSalary<=55000?taxSalary*0.3-2755: taxSalary<=80000?taxSalary*0.35-5505: taxSalary*0.45-13505; System.out.println("The tax should be paid by individuals is: "+tax); /*Method 2*/ if( taxSalary < 0 ){ tax = 0; }else if( taxSalary <= 1500){ tax = taxSalary*0.03; }else if( taxSalary <= 4500){ tax = taxSalary*0.1-105; }else if( taxSalary <= 9000){ tax = taxSalary*0.2-555; }else if( taxSalary <= 35000){ tax = taxSalary*0.25-1005; }else if( taxSalary <= 55000){ tax = taxSalary*0.3-2755; }else if( taxSalary <= 80000){ tax = taxSalary*0.35-5505; }else{ tax = taxSalary*0.45-13505; } System.out.println("The tax should be paid by the individual is: "+tax); } }9. Enter year and month, and output days.
package demo; import java.util.Scanner; /* Tip: 1. You need to determine whether it is a leap year. The number of days in February is related to whether it is a leap year; 2. Use switch-case to determine the number of days in each month*/ public class demo{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter year: "); int year = scanner.nextInt(); System.out.println("Please enter month: "); int month = scanner.nextInt(); int dayNum = theDayNum(month); //First get the number of days based on the month. If it is a leap year, re-get the number of days in February if(isLeapYear(year)){ if(month == 2){ dayNum ++; //If it is a leap year, add one day in February} System.out.print(year + "is a leap year,"); }else{ System.out.print(year + "not a leap year,"); } System.out.println(year + "year" + month + "month total" + dayNum + "day"); } /*Judge whether it is a leap year* Can be divided by 4 but cannot be divided by 100, or can be divided by 400, it is a leap year*/ public static boolean isLeapYear(int year) { if((year % 4 ==0 && year % 100 !=0) || year%400 ==0){ return true; }else{ return false; } } /*Judge the number of days*/ public static int theDayNum(int month) { switch (month) { case 1: return 31; case 2: return 28; case 3: return 31; case 4: return 30; case 5: return 31; case 6: return 30; case 7: return 31; case 8: return 31; case 9: return 30; case 10: return 31; case 11: return 30; case 12: return 31; default: System.out.println("Sorry, the month you entered was incorrect!"); return 0; } } }10. Output the nine-nine multiplication table.
package demo; /* author:wendy * Problem: * Direct output of nine-nine multiplication table* */ public class demo { public static void main(String[] args) { //i variable is used to control the number of rows for(int i = 0; i <= 9; i++) { //j variable is used to control the numerical values involved in each row for(int j = 1; j <= i; j++) { System.out.print(j + "*" + i + "=" + i*j + "/t"); } //After output of each row, you need to have a new line System.out.println(); } } } }<strong> </strong>11. Randomly generate an integer from 0-100 to determine whether it is a prime number. A prime number is also called a prime number. It refers to a number that cannot be divided by other natural numbers except 1 and the integer itself.
package demo; import java.util.Random; public class primeNum { public static void main(String[] args) { int num; Random random = new Random(); num = random.nextInt(100); System.out.println("The random generated number is: " + num); System.out.println(isPrime(num)); } public static boolean isPrime(int num) { if(num < 2) { return false; } if(num == 2) { return true; } if(num % 2 == 0) { return false; } for(int i = 3; i <= Math.sqrt(num); i += 2) { if(num % i == 0) { return false; } } return true; } }12. Find the minimum value of the array and expand the array into a new array.
package demo; import java.util.Arrays; import java.util.Random; /* * author:wendy * Problem: Randomly generate 10 integers from 0-100 and find the minimum value; * Expand the array into a new array, and the minimum value is stored in the first position of the new array. * Steps: * 1. Construct an array of length 10 and randomly generate 10 integers between 0-100 using Random; * 2. Find the minimum value, use for loop* 3. Expand the capacity to construct a new array using Arrays.coprOf() and set its length to 11 * 4. Iterate through the new array, traverse from behind to front, assign the value, and then place the minimum value found in 2 in the first of the array* */ public class copyOf { public static void main(String[] args) { int [] arr = new int[10]; // Random generate 10 integers between 0-100 Random random = new Random(); for(int i = 0; i < 10; i ++) { arr[i] = random.nextInt(100); } //Print the content of the array System.out.println("The randomly generated array is: " + Arrays.toString(arr)); //Find the smallest value int min = arr[0]; for(int j = 1; j < 10; j ++) { if(min > arr[j]) { min = arr[j]; } } System.out.println("The smallest value of this array is: " + min); //Expand, and the minimum value is present in the first int after expansion [] newArr = Arrays.copyOf(arr, 11); //Transfer from behind to front, assign the previous value to the subsequent value, and then assign the first value to the minimum value min for(int k = newArr.length-1; k >=1; k --) { newArr[k] = newArr[k-1]; } //Assign the first value to the minimum value min newArr[0] = min; //Print the content of the array System.out.println("The array after expansion is: "+ Arrays.toString(newArr)); } }The main content of this article is of great help to students who are first exposed to Java algorithms and thinking. The editor will provide you with relevant help. Please pay more attention to Wulin.com.