1. 從鍵盤輸入一個四位數,將該數字反轉,與原數相加后輸出。
import java.util.Scanner; public class Test4_05 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.println("請輸入一個四位數:"); int num=scanner.nextInt(); int num1=num/1000%10; //千位上的數
int num2=num/100%10; //百位上的數
int num3=num/10%10; //十位上的數
int num4=num%10; //個位上的數
int a=num4*1000+num3*100+num2*10+num1; System.out.println(a+num); } }
2. 托運行李計算費用:
實驗要求:
(1)貨車在計算托運行李費用時以kg為單位計算費用(12元/kg),忽略重量中的小數部分,即忽略不足1kg的部分。
(2)汽車在計算托運行李費用時以kg為單位計算費用(22元/kg),將重量中的小數部分進行四舍五入,即不足1kg的部分進行四舍五入。(3)飛機在計算托運行李費用時以g為單位計算費用(0.062元/g)(62元/kg),將重量中的小數部分,即不足1g的部分進行四舍五入。
用double型變量weight存放行李重量,用charge存放托運行李費用,程序讓用戶從鍵盤輸入weight的值,該值是行李的重量(kg)為單位,然后程序將分別計算出用貨車、汽車和飛機托運行李的費用。
提示:為了實現四舍五入,只需將浮點數加上0.5,再將結果進行int型轉換即可。類型轉換運算符的級別是2級,因此,(int)15.9+0.5的結果是15.5,即((int)15.9)+0.5,而(int)(15.9+0.5)的結果才是16。
import java.util.Scanner public class Test4_06 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("請輸入weight:"); //(kg)為單位
double weight = sc.nextDouble(); int truck=(int)(weight*12); float bus=(int )(weight+0.5)*22;//四舍五入 double fly=(int)(weight*1000+0.5)*0.062;//四舍五入
System.out.println("貨車的費用是:"+truck); System.out.println("汽車的費用是:"+bus); System.out.println("費用的費用是:"+fly); } }
3. 根據給定的字符(char)變量,輸出是相應信息[大寫字母、小寫字母、數字、特殊字符];
提示:如何從鍵盤獲取一個char字符
Scanner sc=new Scanner(System.in);
char st=(char)sc.next().charAt(0);
import java.util.Scanner; public class Test4_07 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("請輸入一個字符:"); char c=(char)sc.next().charAt(0); if(c>='A'&&c<='Z') { System.out.println("該字符是大寫字母"); } else if(c>='a'&&c<='z') { System.out.println("該字符是小寫字母"); }else if(c>=0&&c<=9) { System.out.println("該字符是數字"); }else { System.out.println("該字符是特特殊字符"); } } }
4. 某電信公司的市內通話費計算標准如下:
三分鍾內0.2元,三分鍾后每增加一分鍾增加0.1元,不足一分鍾的按一分鍾計算。
要求編寫程序,給定一個通話時間(單位:秒),計算出應收費金額。
import java.util.Scanner; public class Test4_08 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("請輸入一個通話時間(秒):"); int second=sc.nextInt(); int min=(second+59)/60; //不足一分鍾的按一分鍾計算
double price=0; if(min<=3) { price = 0.2; }else if(min>3) { price = 0.2; price+=(min-3)*0.1; } System.out.println("市內通話費總金額:"+price); } }
5. 接收命令行參數年、月、日,判斷這一天是當年的第幾天
注:判斷一年是否是閏年的標准:
1)可以被4整除,但不可被100整除
2)可以被400整除
package zuoye;
import java.util.Scanner; public class Test4_10 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("請輸入年:"); int year=sc.nextInt(); System.out.println("請輸入月:"); int month=sc.nextInt(); System.out.println("請輸入日:"); int day=sc.nextInt();
int total=0; //2020.5.2 年份用於判斷該年2月是否是閏年 ,月份用於累加該月前的天數, 日期用於累加天數
switch(month-1) { //case 12: // total+=31;
case 11: total+=30; case 10: total+=31; case 9: total+=30; case 8: total+=31; case 7: total+=31; case 6: total+=30; case 5: total+=31; case 4: total+=30; case 3: total+=31; case 2: if(year%4==0&&year%100!=0||year%400==0) { total+=29; }else { total+=28; } case 1: total+=31; } total+=day; //因為這里加上了天數,所以上面的月份需要-1 System.out.println("該日期是當年第"+total+"天"); } }