java中判断⼩于0_从零⾃学Java-5.使⽤条件测试进⾏判断1.使⽤if语句进⾏最基本的条件测试;
2.测试⼀个值⼤于还是⼩于另⼀个值;
3.测试两个值是否相等;
4.使⽤与if语句对应的else语句;
5.组合多个条件测试;
6.使⽤switch语句进⾏复杂的条件测试;
7.使⽤三元运算符创建测试;
程序Game:if语句的初步使⽤
1 packagecom.jsample;2
3 public classGame {
4 public static voidmain(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 }
自学java从哪里开始View Code
输出:
You score a touchdown!
Total score: 7
程序Commodity:使⽤switch语句来购买或销售东西
1 packagecom.jsample;2
3 public classCommmodity {
4 public static voidmain(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 packagecom.jsample;2
3 import java.time.*; //让程序能够使⽤类java.time.LocalDateTime,它⽤于跟踪当前的⽇期和时间
4 import poral.*;//让程序能够使⽤poralfield.ChronoField
5
6 public class Clock {//开始Clock程序及其main()语句块
7 public static voidmain(String[] args){8 //get current time and date
9 LocalDateTime now = w();//创建⼀个名为now的LocalDateTime对象,该对象包含系统的当前⽇期和时间
10 int hour = (ChronoField.HOUR_OF_DAY);//创建变量hour,month,day,year,这些变量的值来⾃LocalDateTime对象
11 int minute =(ChronoField.MINUTE_OF_HOUR);12 int month =(ChronoField.MONTH_OF_YEAR);13 int day =(ChronoField.DAY_OF_MONTH);14 int year =(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 packagecom.jsample;2
3 public classGradeGame {
4 public static voidmain(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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论