計算員工薪水,底薪 : 25000,100000 以上 獎金 5%,300000 以上 獎金 7%,500000 以上 獎金 10%。
執行結果,如下。
程式碼,如下。
import java.util.Scanner;
public class EX2_6 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print("\n請輸入業績 》》》》》 ");
double accomplishment = stdIn.nextDouble();
double salary = 25000;
double prize = 0;
if (accomplishment >= 100000 && accomplishment < 300000) {
prize = accomplishment * 0.05;
} else if (accomplishment >= 300000 && accomplishment < 500000) {
prize = accomplishment * 0.07;
} else if (accomplishment >= 500000) {
prize = accomplishment * 0.1;
}
System.out.println("本月薪資 : " + (salary + prize) + "(底薪:" + salary
+ " 獎金:" + prize + ")");
}
}
2012年4月4日 星期三
練習題 2_5:月份英翻中
執行結果,如下。
程式碼,如下。
import java.util.Scanner;
public class EX2_5 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print(" 請輸入月份 》》》》》 ");
String month = stdIn.next();
String outStr = "輸入錯誤。";
if ("1".equals(month)) {
outStr = "Januery";
} else if ("2".equals(month)) {
outStr = "February";
} else if ("3".equals(month)) {
outStr = "March";
} else if ("4".equals(month)) {
outStr = "April";
} else if ("5".equals(month)) {
outStr = "May";
} else if ("6".equals(month)) {
outStr = "June";
} else if ("7".equals(month)) {
outStr = "July";
} else if ("8".equals(month)) {
outStr = "August";
} else if ("9".equals(month)) {
outStr = "Stptember";
} else if ("10".equals(month)) {
outStr = "October";
} else if ("11".equals(month)) {
outStr = "November";
} else if ("12".equals(month)) {
outStr = "December";
}
System.out.println(" 英文 》》》》》 " + outStr);
}
}
程式碼,如下。
import java.util.Scanner;
public class EX2_5 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print(" 請輸入月份 》》》》》 ");
String month = stdIn.next();
String outStr = "輸入錯誤。";
if ("1".equals(month)) {
outStr = "Januery";
} else if ("2".equals(month)) {
outStr = "February";
} else if ("3".equals(month)) {
outStr = "March";
} else if ("4".equals(month)) {
outStr = "April";
} else if ("5".equals(month)) {
outStr = "May";
} else if ("6".equals(month)) {
outStr = "June";
} else if ("7".equals(month)) {
outStr = "July";
} else if ("8".equals(month)) {
outStr = "August";
} else if ("9".equals(month)) {
outStr = "Stptember";
} else if ("10".equals(month)) {
outStr = "October";
} else if ("11".equals(month)) {
outStr = "November";
} else if ("12".equals(month)) {
outStr = "December";
}
System.out.println(" 英文 》》》》》 " + outStr);
}
}
練習題 2_4:比較兩數大小
執行結果,如下。
程式碼,如下。
import java.util.Scanner;
public class EX2_4 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print("請輸入正整數 A 》》》》》 ");
int a = stdIn.nextInt();
System.out.print("請輸入正整數 B 》》》》》 ");
int b = stdIn.nextInt();
String resultStr = "";
if (a < 0 || b < 0) {
resultStr = "數字不可以為負數。";
} else {
if (a > b) {
resultStr = "A > B";
} else if (a < b) {
resultStr = "A < B";
} else {
resultStr = "A = B";
}
}
System.out.println("\n" + resultStr);
}
}
程式碼,如下。
import java.util.Scanner;
public class EX2_4 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print("請輸入正整數 A 》》》》》 ");
int a = stdIn.nextInt();
System.out.print("請輸入正整數 B 》》》》》 ");
int b = stdIn.nextInt();
String resultStr = "";
if (a < 0 || b < 0) {
resultStr = "數字不可以為負數。";
} else {
if (a > b) {
resultStr = "A > B";
} else if (a < b) {
resultStr = "A < B";
} else {
resultStr = "A = B";
}
}
System.out.println("\n" + resultStr);
}
}
學習範例 2_7:判斷成績(2)
成績 100~90: A, 80~89:B, 70~79:C, 60~69:D, 60以下,不及格。
程式碼,如下。
import java.util.Scanner;
class P2_7{
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
System.out.print("請輸入成績 : ");
int studentGrade = stdIn.nextInt();
if(studentGrade > 100 || studentGrade < 0){
System.out.println("輸入錯誤");
}else if(studentGrade >= 90 && studentGrade <= 100){
System.out.println("A");
}else if(studentGrade >= 80 && studentGrade <= 89){
System.out.println("B");
}else if(studentGrade >= 70 && studentGrade <= 79){
System.out.println("C");
}else if(studentGrade >= 60 && studentGrade <= 69){
System.out.println("D");
}else {
System.out.println("不及格。");
}
}
}
執行結果,如下。
程式碼,如下。
import java.util.Scanner;
class P2_7{
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
System.out.print("請輸入成績 : ");
int studentGrade = stdIn.nextInt();
if(studentGrade > 100 || studentGrade < 0){
System.out.println("輸入錯誤");
}else if(studentGrade >= 90 && studentGrade <= 100){
System.out.println("A");
}else if(studentGrade >= 80 && studentGrade <= 89){
System.out.println("B");
}else if(studentGrade >= 70 && studentGrade <= 79){
System.out.println("C");
}else if(studentGrade >= 60 && studentGrade <= 69){
System.out.println("D");
}else {
System.out.println("不及格。");
}
}
}
執行結果,如下。
訂閱:
文章 (Atom)