溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么使用Java編寫經(jīng)典小程序

發(fā)布時間:2021-07-02 14:30:32 來源:億速云 閱讀:221 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)怎么使用Java編寫經(jīng)典小程序的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

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

1、輸出兩個int數(shù)中的最大值

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

2、輸出三個int數(shù)中的最大值

package demo; 
 
import java.util.Scanner; 
 
public class demo { 
 public static void main(String[] args) { 
 Scanner scanner = new Scanner(System.in); 
 System.out.println("請依次輸入兩個整數(shù):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("出現(xiàn)異常"); 
 } 
 } 
}

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、完成成績等級輸出程序:如果用戶輸入的分數(shù)正確(0-100),則根據(jù)表-1中的規(guī)則計算該分數(shù)所對應的的級別,并計算結(jié)果。

怎么使用Java編寫經(jīng)典小程序

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

怎么使用Java編寫經(jīng)典小程序

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、完成收銀柜臺收款程序:編寫一個收銀柜臺收款程序,根據(jù)單價、購買數(shù)量以及收款進行計算并輸出應收金額和找零;當總價大于或者等于500,享受8折優(yōu)惠??刂婆_交互如下:

怎么使用Java編寫經(jīng)典小程序

package demo; 
 
import java.util.Scanner; 
/* 
 * 需求: 
 * 編寫一個收銀柜臺收款程序。根據(jù)單價、購買數(shù)量以及收款進行計算并輸出應收金額和找零; 
 * 當總價大于或者等于500,享受8折優(yōu)惠。 
 * 
 */ 
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("請輸入數(shù)量:"); 
 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從鍵盤輸入三個整數(shù),實現(xiàn)從小到大排序。

package demo; 
 
import java.util.Scanner; 
 
/* 
 * java從鍵盤輸入三個整數(shù),實現(xiàn)從小到大排序 
 * 
 **/ 
public class demo { 
 public static void main(String[] args) { 
 Scanner scanner = new Scanner(System.in); 
 System.out.println("請輸入三個整數(shù),以空格隔開:"); 
 
 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、計算個人所得稅 北京地區(qū)的個人所得稅計算公式:應納稅額 = (工資薪金所得 - 扣除數(shù))*適用稅率 - 速算扣除數(shù) 其中,扣除數(shù)為3500,適用稅率以及速算扣除數(shù)如下表所示:

怎么使用Java編寫經(jīng)典小程序

package demo; 
 
import java.util.Scanner; 
/* 
 * 北京地區(qū)的個人所得稅計算公式: 
 應納稅額 = (工資薪金所得 - 扣除數(shù))*適用稅率 - 速算扣除數(shù) 
 其中,扣除數(shù)為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、輸入年份和月份,輸出天數(shù)。

package demo; 
 
import java.util.Scanner; 
/* 
 提示: 
 1.需要判斷是否是閏年,2月份的天數(shù)跟是否是閏年有關(guān)系; 
 2.用switch-case判斷每個月的天數(shù) 
 
 */ 
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); //先根據(jù)月份得出天數(shù),如果是閏年,對2月份的天數(shù)重新獲取 
 
 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; 
 } 
 } 
 
 /*判斷天數(shù)*/ 
 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、輸出九九乘法表。

怎么使用Java編寫經(jīng)典小程序

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

11、隨機產(chǎn)生一個從0-100之間的整數(shù),判斷是否是質(zhì)數(shù) 質(zhì)數(shù)又稱素數(shù),是指在一個大于1的自然數(shù)中,除了1和此整數(shù)自身外,不能被其他自然數(shù)整除的數(shù) 。

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("隨機產(chǎn)生的數(shù)為:" + 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、查找數(shù)組最小值,并將數(shù)組擴容成新數(shù)組。

package demo; 
 
import java.util.Arrays; 
import java.util.Random; 
/* 
 * author:wendy 
 * 問題:隨機產(chǎn)生10個從0-100之間的整數(shù),并查找最小值; 
 * 將該數(shù)組擴容成新數(shù)組,把最小值存在新數(shù)組的第一個位置。 
 * 步驟: 
 * 1.構(gòu)造一個長度為10的數(shù)組,利用Random隨機產(chǎn)生10個0-100之間的整數(shù); 
 * 2.尋找最小值,利用for循環(huán) 
 * 3.擴容 利用Arrays.coprOf()構(gòu)造新數(shù)組,將其長度設置為11 
 * 4.遍歷新數(shù)組,從后往前遍歷,以此賦值,然后將2中找到的最小值存在數(shù)組的第一個 
 * */ 
public class copyOf { 
 
 public static void main(String[] args) { 
 int [] arr = new int[10]; 
 
 //隨機產(chǎn)生10個 0-100之間的整數(shù) 
 Random random = new Random(); 
 for(int i = 0; i < 10; i ++) { 
 arr[i] = random.nextInt(100); 
 } 
 //打印數(shù)組的內(nèi)容 
 System.out.println("隨機產(chǎn)生的數(shù)組為:" + Arrays.toString(arr)); 
 
 //查找最小的值 
 int min = arr[0]; 
 for(int j = 1; j < 10; j ++) { 
 if(min > arr[j]) { 
 min = arr[j]; 
 } 
 } 
 System.out.println("該數(shù)組最小的值為:" + 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; 
 //打印數(shù)組的內(nèi)容 
 System.out.println("擴容之后的數(shù)組為:"+ Arrays.toString(newArr)); 
 } 
 
}

感謝各位的閱讀!關(guān)于“怎么使用Java編寫經(jīng)典小程序”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI