Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 314|回复: 0

[默认分类] 从零自学Java-5.使用条件测试进行判断

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-3-17 11:34:54 | 显示全部楼层 |阅读模式

    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("Aerfect");
    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


    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-16 11:49 , Processed in 0.392493 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表