From scratch learn Java-5. Use conditional test for judgment, learn java-5
1. Use the if statement to test the most basic conditions;
2. test whether a value is greater than or less than another value;
3. test whether the two values are equal;
4. Use the else statement corresponding to the if statement;
5. Combine multiple conditions for testing;
6. Use the switch statement to perform complex condition tests;
7. Create a test using the ternary operator;
Program Game: Preliminary use of if Statements
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
Output:
You score a touchdown!
Total score: 7
Program Commodity: Use the switch statement to buy or sell things
1 package com. jsample; 2 3 public class Commmodity {4 public static void main (String [] args) {5 String command = "BUY "; // The command is set to "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 "Limit": 15 quantity-= 5; 16 balance + = 15; 17} 18 System. out. println ("Balance:" + balance + "\ n" 19 + "Quantity:" + quantity); 20} 21}
View Code
Output:
Balance: 530
Quantity: 47
Program Clock: uses the built-in timing function of Java to track the current date and time, and displays the information in one sentence
1 package com. jsample; 2 3 import java. time. *; // enable the program to use the java class. time. localDateTime, which is used to track the current date and time 4 import java. time. temporal. *; // enables the program to use java. time. temporalfield. chronoField 5 6 public class Clock {// start the Clock program and its main () Statement Block 7 public static void main (String [] args) {8 // get current time and date 9 LocalDateTime now = LocalDateTime. now (); // create a LocalDateTime object named now, which contains the current date and time of the system 10 int hour = now. get (Chron OField. HOUR_OF_DAY); // create the variables hour, month, day, year. The values of these variables come from the LocalDateTime object 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 greeting17 if (hour <12) {// display one of the three greetings. The displayed content depends on the value of the hour variable 18 System. out. println ("Good morning. \ n "); 19} else if (hour <17) {20 then E M. out. println ("Good afternoon. \ n "); 21} else {22 System. out. println ("Good evening"); 23} 24 25 // begin time message by showing the minutes26 System. out. print ("It's"); // display the specific number of minutes based on the value of the minute variable 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 hour34 System. out. print (""); // display the hour value in the 12-hour System 35. out. print (hour> 12 )? (Hour-12): hour); 36 System. out. print ("o 'clock on"); 37 38 // display the name of the month39 switch (month) {// display different month names based on the value of the variable 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 ("CMDL"); 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 year55 System. out. println ("" + day + "," + year + ". "); // display the current date and year 56} // end main () Statement block 57} // end the entire clock Program
View Code
Output:
Good morning.
It's 15 minutes past 9 o 'clock on March 16,2018.
Program GradeGame: stores user input scores (0-100), automatically grades and outputs comments (Implemented Using if and switch statements respectively)
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 else29 {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
Input:
65
Output:
B Good
B Good