生活随笔
收集整理的這篇文章主要介紹了
Java年薪计算器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
要求: 薪水計算器
(1)通過鍵盤輸入用戶月薪,每年是幾個月的月薪
(2)輸出用戶的年薪
(3)輸出一行字:如果年薪超過10萬,輸出“恭喜你超越90%的國人”,如果年薪超過20萬,輸出“恭喜你超越98%的國人”
(4)直到鍵盤鍵盤輸入“exit”,則退出程序(使用break退出循環)
(5)輸入中途,鍵盤輸入next,則這個用戶退出計算不顯示“恭喜…”,算下一個用戶的年薪
- 本文包括四個部分
1、問題版(問題+原因+解決方案)
2、最終版
3、完成效果
4、問題總結 - 問題版
package test;
import java.util.Scanner;public class Test15 {public static void main(String[]args
) {while(true) {Scanner scanner
=new Scanner(System.in
);System.out
.println("請輸入月薪");double monthSalary
=scanner
.nextDouble();System.out
.println("請輸入每年是幾個月的月薪");int months
=scanner
.nextInt();System.out
.println("用戶年薪為:"+(monthSalary
*months
));System.out
.println("是否繼續?(輸入exit退出計算器,輸入next進行下一個用戶的計算,輸入其他字符可繼續顯示當前用戶信息...");String input
=scanner
.nextLine(); if(input
=="next")continue;if(input
=="exit")break;if(monthSalary
*months
>100000) System.out
.println("恭喜你超越90%的國人");if(monthSalary
*months
>200000)System.out
.println("恭喜你超越98%的國人");}}
}
package test;
import java.util.Scanner;public class test
{public static void main(String[]args
) {while(true) {Scanner scanner
=new Scanner(System.in
);System.out
.println("請輸入月薪");double monthSalary
=scanner
.nextDouble();System.out
.println("請輸入每年是幾個月的月薪");int months
=scanner
.nextInt();System.out
.println("用戶年薪為:"+(monthSalary
*months
));System.out
.println("是否繼續?(輸入exit退出計算器,輸入next進行下一個用戶的計算,輸入其他字符可繼續顯示當前用戶信息...");scanner
.nextLine(); String input
=scanner
.nextLine(); if("next".equals(input
)) {System.out
.println("進行下一個用戶年薪計算");continue;}if("exit".equals(input
)) {System.out
.print("程序終止");break;}if(monthSalary
*months
>100000 && monthSalary
*months
<200000) System.out
.println("恭喜你超越90%的國人");if(monthSalary
*months
>200000)System.out
.println("恭喜你超越98%的國人");}}
}
1、在Scanner類中的nextInt()后使用nextLine()無法獲取鍵盤字符:
原因:nextInt()只讀取了數字,留下了回車,回車存于緩沖區,會被下一個nextLine()直接讀取。
解決方案:
①在輸入字符串之前先將緩沖區的換行符都去掉(即在“String input=scanner.nextLine();”前一行添加“scanner.nextLine();”
②用next()取代nextLine()
2、進行String的比較時應使用"字符串".equals(變量名)。
equals()方法 比較的是兩個值(只針對String類型),而==操作對于基本數據類型比較的是兩個變量的值是否相等,對于引用型變量表示的是兩個變量在堆中存儲的地址是否相同。
3、使用if語句判斷時,注意判斷區間的上下限
總結
以上是生活随笔為你收集整理的Java年薪计算器的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。