• 从零自学Java-5.使用条件测试进行判断


    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 + "
    "
    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.
    ");
    19         }else if (hour < 17){
    20             System.out.println("Good afternoon.
    ");
    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

  • 相关阅读:
    [bbk2342] 第47集 Chapter 09Optimizing Sore Perations(02)
    [bbk2345] 第50集 Chapter 12Optimizing Sore Perations(05)
    [bbk2343] 第48集 Chapter 12Optimizing Sore Perations(03)
    [bbk2863] 第54集 Chapter 14Using Oracle Dasta Storage Structures Efficiently(00)
    [bbk2515] 第51集 Chapter 13Using Oracle Blokcs Efficeintly[00]
    [bbk2341] 第46集 Chapter 09Optimizing Sore Perations(01)
    [bbk2863] 第54集 Chapter 13Using Oracle Blokcs Efficeintly[03]
    [bbk2668] 第53集 Chapter 13Using Oracle Blokcs Efficeintly[02]
    [bbk2344] 第49集 Chapter 12Optimizing Sore Perations(04)
    QT 中的模态和非模态对话框
  • 原文地址:https://www.cnblogs.com/redlogic/p/8586064.html
Copyright © 2020-2023  润新知