如果是剛接觸或者剛學(xué)習(xí)Java请唱,練習(xí)一些基礎(chǔ)的算法還是必須的菩帝,可以提升思維和語法的使用。
1族檬、輸出兩個(gè)int數(shù)中的最大值
import java.util.Scanner;
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請依次輸入兩個(gè)整數(shù):a,b(以空格隔開)");
/*比較兩個(gè)數(shù)的大小*/
int a = scanner.nextInt();
int b = scanner.nextInt();
int max;
if(a >= b){
max = a;
}else {
max = b;
}
System.out.println("最大值為"+max);
}
}
}
2歪赢、輸出三個(gè)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("請依次輸入兩個(gè)整數(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、編寫程序判斷某一個(gè)年份是否是閏年
package demo;
import java.util.Scanner;
/*判斷閏年
由用戶輸入任意一個(gè)年份单料,能被4整除但不能被100整除埋凯,或者能被400整除,是閏年扫尖。
要求判斷一個(gè)年份是否為閏年递鹉。
要求輸出:此年份是否是閏年
*/
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("這個(gè)年份是閏年");
}else{
System.out.println("這個(gè)年份不是閏年");
}
/*方法二*/
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ì)算該分?jǐn)?shù)所對應(yīng)的的級別躏结,并計(jì)算結(jié)果。
package demo;
import java.util.Scanner;
/*
* 成績等級劃分表
* >= 90 A
* >=80 B
* >=60 C
* <60 D
*
* 分?jǐn)?shù)范圍:0-100
*
* 需要有2個(gè)判斷*/
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媳拴、完成命令解析程序 問題:有一個(gè)命令解析程序黄橘,該程序提供三個(gè)功能選型供用戶選擇,用戶選擇某功能后屈溉,程序在界面上輸出用戶所選擇的的功能名稱塞关。程序的交互如圖:
package demo;
import java.util.Scanner;
/*
* 有一個(gè)命令解析程序,該程序提供三個(gè)功能選型供用戶選擇子巾,
* 用戶選擇某功能后帆赢,程序在界面上輸出用戶所選擇的的功能名稱。
*
* */
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("輸入錯(cuò)誤线梗!");
}
}
}
6椰于、完成收銀柜臺收款程序 編寫一個(gè)收銀柜臺收款程序。根據(jù)單價(jià)仪搔、購買數(shù)量以及收款進(jìn)行計(jì)算并輸出應(yīng)收金額和找零瘾婿;當(dāng)總價(jià)大于或者等于500,享受8折優(yōu)惠烤咧∑悖控制臺交互如下:
package demo;
import java.util.Scanner;
/*
* 需求:
* 編寫一個(gè)收銀柜臺收款程序。根據(jù)單價(jià)煮嫌、購買數(shù)量以及收款進(jìn)行計(jì)算并輸出應(yīng)收金額和找零笛谦;
* 當(dāng)總價(jià)大于或者等于500,享受8折優(yōu)惠昌阿。
*
*/
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入單價(jià)(¥):");
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從鍵盤輸入三個(gè)整數(shù),實(shí)現(xiàn)從小到大排序
package demo;
import java.util.Scanner;
/*
* java從鍵盤輸入三個(gè)整數(shù),實(shí)現(xiàn)從小到大排序
*
**/
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入三個(gè)整數(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宝泵、計(jì)算個(gè)人所得稅 北京地區(qū)的個(gè)人所得稅計(jì)算公式: 應(yīng)納稅額 = (工資薪金所得 - 扣除數(shù))*適用稅率 - 速算扣除數(shù) 其中好啰,扣除數(shù)為3500,適用稅率以及速算扣除數(shù)如下表所示:
package demo;
import java.util.Scanner;
/*
* 北京地區(qū)的個(gè)人所得稅計(jì)算公式:
應(yīng)納稅額 = (工資薪金所得 - 扣除數(shù))*適用稅率 - 速算扣除數(shù)
其中儿奶,扣除數(shù)為3500
*/
public class demo9 {
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("個(gè)人應(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("個(gè)人應(yīng)繳納稅款為:"+tax);
}
}
9框往、輸入年份和月份,輸出天數(shù)
package demo;
import java.util.Scanner;
/*
提示:
1.需要判斷是否是閏年闯捎,2月份的天數(shù)跟是否是閏年有關(guān)系椰弊;
2.用switch-case判斷每個(gè)月的天數(shù)
*/
public class demo10 {
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;
}
}
}
作者:Wendy-lxq
原文地址:http://www.wxapp-union.com/portal.php?mod=view&aid=2893