如果是剛接觸或者剛學習java,練習一些基礎的算法還是必須的,可以提升思維和語法的使用。

1、輸出兩個int數中的最大值
import java.util.Scanner;
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“請依次輸入兩個整數:a,b(以空格隔開)”);
/*比較兩個數的大小*/
int a = scanner.nextInt();
int b = scanner.nextInt();
int max;
if(a >= b){
max = a;
}else {
max = b;
}
System.out.println(“最大值為”+max);
}
}
}

2、輸出三個int數中的最大值
package demo;
import java.util.Scanner;
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“請依次輸入兩個整數:a,b(以空格隔開)”);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
scanner.close();
/*方法一*/
int d=(a>b)?a:b;
int e=(d>c)?d:c;
System.out.println(“最大值為”+e);
/*方法二*/
if(a>b && a>c){
System.out.println(“最大值為”+a);
}else if(b>c && b>a){
System.out.println(“最大值為”+b);
}else if(c>b && c>a){
System.out.println(“最大值為”+c);
}else{
System.out.println(“出現異常”);
}
}
}

3、編寫程序判斷某一個年份是否是閏年
package demo;
import java.util.Scanner;
/*判斷閏年
由用戶輸入任意一個年份,能被4整除但不能被100整除,或者能被400整除,是閏年。
要求判斷一個年份是否為閏年。
要求輸出:此年份是否是閏年
*/
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“請輸入年份:”);
int year = scanner.nextInt();
/*方法一*/
if((year % 4 ==0 && year % 100 !=0) || year%400 ==0){
System.out.println(“這個年份是閏年”);
}else{
System.out.println(“這個年份不是閏年”);
}
/*方法二*/
boolean isLeapYear = (year % 4 ==0 && year % 100 !=0) || year%400 ==0;
String string = isLeapYear?year+”是閏年”:year+”不是閏年”;
System.out.println(string);
}
}

4、完成成績等級輸出程序
如果用戶輸入的分數正確(0-100),則根據表-1中的規則計算該分數所對應的的級別,並計算結果。

package demo;
import java.util.Scanner;
/*
* 成績等級劃分表
* >= 90 A
* >=80 B
* >=60 C
* <60 D
*
* 分數範圍:0-100
*
* 需要有2個判斷*/
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“請輸入分數:”);
double score = scanner.nextDouble();
scanner.close();
if(score < 0 || score >100){
System.out.println(“輸入的分數不在0-100之間,不符合要求”);
}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、完成命令解析程序
問題:有一個命令解析程序,該程序提供三個功能選型供用戶選擇,用戶選擇某功能後,程序在界面上輸出用戶所選擇的的功能名稱。程序的交互如圖:

package demo;
import java.util.Scanner;
/*
* 有一個命令解析程序,該程序提供三個功能選型供用戶選擇,
* 用戶選擇某功能後,程序在界面上輸出用戶所選擇的的功能名稱。
*
* */
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“請選擇功能:1.顯示全部記錄 2.查詢登錄記錄 0.退出”);
int command = scanner.nextInt();
scanner.close();
switch (command) {
case 0:
System.out.println(“歡迎使用”);
break;
case 1:
System.out.println(“顯示全部記錄……”);
break;
case 2:
System.out.println(“查詢登錄記錄……”);
break;
default:
System.out.println(“輸入錯誤!”);
}
}
}

6、完成收銀櫃檯收款程序
編寫一個收銀櫃檯收款程序。根據單價、購買數量以及收款進行計算並輸出應收金額和找零;當總價大於或者等於500,享受8折優惠。控制台交互如下:

package demo;
import java.util.Scanner;
/*
* 需求:
* 編寫一個收銀櫃檯收款程序。根據單價、購買數量以及收款進行計算並輸出應收金額和找零;
* 當總價大於或者等於500,享受8折優惠。
*
*/
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“請輸入單價(¥):”);
double price = scanner.nextDouble();
System.out.println(“請輸入數量:”);
double amount = scanner.nextDouble();
System.out.println(“請輸入收款金額:”);
double count = scanner.nextDouble();
double totalMoney = price*amount;
if(totalMoney > 500){
totalMoney = totalMoney*0.8;
}
double change = count – totalMoney;
System.out.println(“應收金額為:”+totalMoney + “找零為:”+change);
}
}

7、java從鍵盤輸入三個整數,實現從小到大排序
package demo;
import java.util.Scanner;
/*
* java從鍵盤輸入三個整數,實現從小到大排序
*
**/
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“請輸入三個整數,以空格隔開:”);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
scanner.close();
System.out.println(“輸入的值為:a = ” + a + “, b = ” + b + “, c = ” + c);
if(a > b){
if ( b > c) {
System.out.println(“排序後的值為:” + c + “,” + b + “,” + a);
}else if( c > a){
System.out.println(“排序後的值為:” + b + “,” + a + “,” + c);
}else{
System.out.println(“排序後的值為:” + b + “,” + a + “,” + c);
}
}else{
if(c < a){
System.out.println(“排序後的值為:” + c + “,” + a + “,” + b);
}else if(c > b){
System.out.println(“排序後的值為:” + a + “,” + b + “,” + c);
}else{
System.out.println(“排序後的值為:”+ a + “,” + c + “,” + b);
}
}
}
}

8、計算個人所得稅
北京地區的個人所得稅計算公式:
納稅額 = (工資薪金所得 – 扣除數)*適用稅率 – 速算扣除數
其中,扣除數為3500,適用稅率以及速算扣除數如下表所示:

package demo;
import java.util.Scanner;
/*
* 北京地區的個人所得稅計算公式:
應納稅額 = (工資薪金所得 – 扣除數)*適用稅率 – 速算扣除數
其中,扣除數為3500
*/
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“請輸入你的稅前工資:”);
int salaryBeforeTax = scanner.nextInt();
scanner.close();
int taxSalary = salaryBeforeTax – 3500;
double tax;
/*方法一*/
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(“個人應繳納稅款為:”+tax);
/*方法二*/
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(“個人應繳納稅款為:”+tax);
}
}

9、輸入年份和月份,輸出天數
package demo;
import java.util.Scanner;
/*
提示:
1.需要判斷是否是閏年,2月份的天數跟是否是閏年有關係;
2.用switch-case判斷每個月的天數
*/
public class demo{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“請輸入年份:”);
int year = scanner.nextInt();
System.out.println(“請輸入月份:”);
int month = scanner.nextInt();
int dayNum = theDayNum(month); //先根據月份得出天數,如果是閏年,對2月份的天數重新獲取
if(isLeapYear(year)){
if(month == 2){
dayNum ++; //如果是閏年,2月份增加一天
}
System.out.print(year + “是閏年,”);
}else{
System.out.print(year + “不是閏年,”);
}
System.out.println(year + “年” + month + “月份共有” + dayNum + “天”);
}
/*判斷是否是閏年
* 能被4整除但不能被100整除,或者能被400整除,是閏年
*/
public static boolean isLeapYear(int year) {
if((year % 4 ==0 && year % 100 !=0) || year%400 ==0){
return true;
}else{
return false;
}
}
/*判斷天數*/
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(“對不起,您輸入的月份有誤!”);
return 0;
}
}
}

10、輸出九九乘法表

package demo;
/* author:wendy
* 問題:
* 直接輸出九九乘法表
* */
public class demo {
public static void main(String[] args) {
//i變量用於控制行數
for(int i = 0; i <= 9; i++) {
//j變量用於控制每行中參與計算的數值
for(int j = 1; j <= i; j++) {
System.out.print(j + “*” + i + “=” + i*j + “t”);
}
//每行輸出之後需要換行
System.out.println();
}
}
}

11、隨機產生一個從0-100之間的整數,判斷是否是質數
質數又稱素數,是指在一個大於1的自然數中,除了1和此整數自身外,不能被其他自然數整除的數 。
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(“隨機產生的數為:” + 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、查找數組最小值,並將數組擴容成新數組
package demo;
import java.util.Arrays;
import java.util.Random;
/*
* author:wendy
* 問題:隨機產生10個從0-100之間的整數,並查找最小值;
* 將該數組擴容成新數組,把最小值存在新數組的第一個位置。
* 步驟:
* 1.構造一個長度為10的數組,利用Random隨機產生10個0-100之間的整數;
* 2.尋找最小值,利用for循環
* 3.擴容 利用Arrays.coprOf()構造新數組,將其長度設置為11
* 4.遍歷新數組,從後往前遍歷,以此賦值,然後將2中找到的最小值存在數組的第一個
* */
public class copyOf {
public static void main(String[] args) {
int [] arr = new int[10];
//隨機產生10個 0-100之間的整數
Random random = new Random();
for(int i = 0; i < 10; i ++) {
arr[i] = random.nextInt(100);
}
//打印數組的內容
System.out.println(“隨機產生的數組為:” + Arrays.toString(arr));
//查找最小的值
int min = arr[0];
for(int j = 1; j < 10; j ++) {
if(min > arr[j]) {
min = arr[j];
}
}
System.out.println(“該數組最小的值為:” + min);
//擴容,將最小值存在擴容之後的第一個
int [] newArr = Arrays.copyOf(arr, 11);
//從後往前遍歷,將前面的值賦給後面的值,然後將第一個的值賦為最小值min
for(int k = newArr.length-1; k >=1; k –) {
newArr[k] = newArr[k-1];
}
//將第一個的值賦為最小值min
newArr[0] = min;
//打印數組的內容
System.out.println(“擴容之後的數組為:”+ Arrays.toString(newArr));
}
}
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/274990.html
微信掃一掃
支付寶掃一掃