有程序在记录你的键盘输入_12个用Java编写基础小程序amp;经典案例(收藏)
點擊上方“潭州教育EDU”關(guān)注我們
如果是剛接觸或者剛學(xué)習(xí)java,練習(xí)一些基礎(chǔ)的算法還是必須的,可以提升思維和語法的使用。
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、完成成績等級輸出程序
如果用戶輸入的分?jǐn)?shù)正確(0-100),則根據(jù)表-1中的規(guī)則計算該分?jǐn)?shù)所對應(yīng)的的級別,并計算結(jié)果。
package demo;
import java.util.Scanner;?
/*
?* 成績等級劃分表
?* >= 90? A
?* >=80? ?B
?* >=60? ?C
?*<60? ? D
?*?
?* 分?jǐn)?shù)范圍:0-100
?*?
?* 需要有2個判斷*/
public class demo {
public static void main(String[] args) {
?Scanner scanner = new Scanner(System.in);
?System.out.println("請輸入分?jǐn)?shù):");
?double score = scanner.nextDouble();
?scanner.close();
?if(score < 0 || score >100){
?System.out.println("輸入的分?jǐn)?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、完成命令解析程序
問題:有一個命令解析程序,該程序提供三個功能選型供用戶選擇,用戶選擇某功能后,程序在界面上輸出用戶所選擇的的功能名稱。程序的交互如圖:
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ù)量以及收款進(jìn)行計算并輸出應(yīng)收金額和找零;當(dāng)總價大于或者等于500,享受8折優(yōu)惠。控制臺交互如下:
package demo;?
import java.util.Scanner;
/*
?* 需求:
?* 編寫一個收銀柜臺收款程序。根據(jù)單價、購買數(shù)量以及收款進(jìn)行計算并輸出應(yīng)收金額和找零;
?* 當(dāng)總價大于或者等于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("應(yīng)收金額為:"+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ù)如下表所示:
package demo;?
import java.util.Scanner;
/*
?* 北京地區(qū)的個人所得稅計算公式:
應(yīng)納稅額 = (工資薪金所得 - 扣除數(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("個人應(yīng)繳納稅款為:"+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("個人應(yīng)繳納稅款為:"+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、輸出九九乘法表
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();
}
}
}
11、隨機(jī)產(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("隨機(jī)產(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ù)組擴(kuò)容成新數(shù)組
package demo;
import java.util.Arrays;
import java.util.Random;
/*
?* author:wendy
?* 問題:隨機(jī)產(chǎn)生10個從0-100之間的整數(shù),并查找最小值;
?*? ? ?將該數(shù)組擴(kuò)容成新數(shù)組,把最小值存在新數(shù)組的第一個位置。
?* 步驟:
?* 1.構(gòu)造一個長度為10的數(shù)組,利用Random隨機(jī)產(chǎn)生10個0-100之間的整數(shù);
?* 2.尋找最小值,利用for循環(huán)
?* 3.擴(kuò)容? 利用Arrays.coprOf()構(gòu)造新數(shù)組,將其長度設(shè)置為11
?* 4.遍歷新數(shù)組,從后往前遍歷,以此賦值,然后將2中找到的最小值存在數(shù)組的第一個
?* */
public class copyOf {
public static void main(String[] args) {
int [] arr = new int[10];
//隨機(jī)產(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("隨機(jī)產(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);
//擴(kuò)容,將最小值存在擴(kuò)容之后的第一個
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("擴(kuò)容之后的數(shù)組為:"+ Arrays.toString(newArr));
}?
}
然后今天就講到這里啦,大家記得點贊收藏,關(guān)注小姐姐哦!
文章內(nèi)容轉(zhuǎn)載自:https://blog.csdn.net/u010297791/article/details/77367589,如有涉及侵權(quán)請聯(lián)系作者刪除。
END
加下面這個雙魚小姐姐微信
領(lǐng)取上課地址更多JAVA開發(fā)資料
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的有程序在记录你的键盘输入_12个用Java编写基础小程序amp;经典案例(收藏)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pythontcp服务器教程_tcp服务
- 下一篇: java反射的优化_请问Java反射的性