1.使用if語句進行最基本的條件測試;2.測試一個值大於還是小於另一個值;3.測試兩個值是否相等;4.使用與if語句對應的else語句;5.組合多個條件測試;6.使用switch語句進行複雜的條件測試;7.使用三元運算符創建測試; 程式Game:if語句的初步使用 1 package com.jsa ...
1.使用if語句進行最基本的條件測試;
2.測試一個值大於還是小於另一個值;
3.測試兩個值是否相等;
4.使用與if語句對應的else語句;
5.組合多個條件測試;
6.使用switch語句進行複雜的條件測試;
7.使用三元運算符創建測試;
程式Game:if語句的初步使用
1 package com.jsample; 2 3 public class Game { 4 public static void main(String[] args){ 5 int total = 0; 6 int score = 7; 7 if(score == 7){ 8 System.out.println("You score a touchdown!"); 9 } 10 if(score == 3){ 11 System.out.println("You kick a field goal!"); 12 } 13 total = total + score; 14 System.out.println("Total score: " + total); 15 } 16 }View Code
輸出:
You score a touchdown!
Total score: 7
程式Commodity:使用switch語句來購買或銷售東西
1 package com.jsample; 2 3 public class Commmodity { 4 public static void main(String[] args){ 5 String command = "BUY";//指令被設定為“BUY” 6 int balance = 550; 7 int quantity = 42; 8 9 switch (command) { 10 case "BUY": 11 quantity += 5; 12 balance -= 20; 13 break; 14 case "SELL": 15 quantity -= 5; 16 balance += 15; 17 } 18 System.out.println("Balance: " + balance + "\n" 19 + "Quantity: " + quantity); 20 } 21 }View Code
輸出:
Balance: 530
Quantity: 47
程式Clock:使用Java內置的計時功能,跟蹤當前的日期和時間,並將信息用一句話顯示出來
1 package com.jsample; 2 3 import java.time.*; //讓程式能夠使用類java.time.LocalDateTime,它用於跟蹤當前的日期和時間 4 import java.time.temporal.*;//讓程式能夠使用java.time.temporalfield.ChronoField 5 6 public class Clock {//開始Clock程式及其main()語句塊 7 public static void main(String[] args){ 8 //get current time and date 9 LocalDateTime now = LocalDateTime.now();//創建一個名為now的LocalDateTime對象,該對象包含系統的當前日期和時間 10 int hour = now.get(ChronoField.HOUR_OF_DAY);//創建變數hour,month,day,year,這些變數的值來自LocalDateTime對象 11 int minute = now.get(ChronoField.MINUTE_OF_HOUR); 12 int month = now.get(ChronoField.MONTH_OF_YEAR); 13 int day = now.get(ChronoField.DAY_OF_MONTH); 14 int year = now.get(ChronoField.YEAR); 15 16 //display greeting 17 if(hour < 12){//顯示三個問候語之一,顯示的內容取決於變數hour的值 18 System.out.println("Good morning.\n"); 19 }else if (hour < 17){ 20 System.out.println("Good afternoon.\n"); 21 }else { 22 System.out.println("Good evening"); 23 } 24 25 //begin time message by showing the minutes 26 System.out.print("It's");//根據變數minute的值來顯示具體的分鐘數 27 if (minute != 0){ 28 System.out.print(" " + minute + " "); 29 System.out.print((minute != 1) ? "minutes" : "minute"); 30 System.out.print(" past"); 31 } 32 33 //display the hour 34 System.out.print(" ");//顯示十二小時制下的hour值 35 System.out.print((hour > 12) ? (hour - 12) : hour); 36 System.out.print(" o'clock on "); 37 38 //display the name of the month 39 switch (month){//根據變數month的值來顯示不同的月份名稱 40 case 1:System.out.print("January");break; 41 case 2:System.out.print("February");break; 42 case 3:System.out.print("March");break; 43 case 4:System.out.print("April");break; 44 case 5:System.out.print("May");break; 45 case 6:System.out.print("June");break; 46 case 7:System.out.print("July");break; 47 case 8:System.out.print("August");break; 48 case 9:System.out.print("September");break; 49 case 10:System.out.print("October");break; 50 case 11:System.out.print("November");break; 51 case 12:System.out.print("December");break; 52 } 53 54 //display the date and year 55 System.out.println(" " + day + "," + year + ".");//顯示當前的日期和年份 56 }//結束main()語句塊 57 }//結束整個clock程式View Code
輸出:
Good morning.
It's 15 minutes past 9 o'clock on March 16,2018.
程式GradeGame:存儲用戶輸入的成績(0-100),自動分等級並輸出評語(分別以if語句和switch語句實現)
1 package com.jsample; 2 3 public class GradeGame { 4 public static void main(String[] args){ 5 int grade = Integer.parseInt(args[0]); 6 char gpa = 'E'; 7 8 if (grade > 80) 9 { 10 System.out.println("A:Perfect"); 11 gpa = 'A'; 12 } 13 else if (grade > 60) 14 { 15 System.out.println("B Good"); 16 gpa = 'B'; 17 } 18 else if (grade > 40) 19 { 20 System.out.println("C Not bad"); 21 gpa = 'C'; 22 } 23 else if (grade > 20) 24 { 25 System.out.println("D You still have lots more to work on"); 26 gpa = 'D'; 27 } 28 else 29 { 30 System.out.println("F Not even wrong"); 31 gpa = 'F'; 32 } 33 34 switch (gpa){ 35 case 'A':System.out.println("A:Perfect");break; 36 case 'B':System.out.println("B Good");break; 37 case 'C':System.out.println("C Not bad");break; 38 case 'D':System.out.println("D You still have lots more to work on");break; 39 case 'F':System.out.println("F Not even wrong");break; 40 default:System.out.println("Who's your daddy");break; 41 } 42 } 43 }View Code
輸入:
65
輸出:
B Good
B Good