java程序设计-第三章 选择


教材

1. 显示时间函数:long, 需要显示转换为int
int number1 = (int) (System.currentTimeMillis() % 10);
//System.currentTimeMillis() is long [as well as %10]
//change long to int 显示转换
2. 两个浮点数相等测试:
常数ε=测试类型Math.if
1E-14doubleMath.abs只跟一个语句可以不用加{}
1E-7float返回绝对值

//两个浮点数值的相等测试
final double EPSILON = 1E-14;
double x = 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1;
if (Math.abs(x-0.5) < EPSILON)
  System.out.println(x + " is approximately 0.5");
3. 避免对boolean的冗余赋值
在这里插入代码片
4. Generating Random Numbers 产生随机数

Math.random method generate double floating-point number in [0,1).

//generate two random single-digit integers
Scanner input = new Scanner(System.in);
int number1 = (int)(Math.random()*10);
int number2 = (int)(Math.random()*10);
5. Operators && || ^操作符

&& both true ⇒ true;
|| one true ⇒ true;
^ one of them is true and the other is false ⇒ true;

package com.Chapter_03;
//Listing3-6
import java.util.Scanner;

public class TestBooleanOperators {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("Enter an integer:");
    int number = input.nextInt();
    if (number % 2 == 0 && number % 3 == 0)
      System.out.println(number + " is divisible by 2 and 3.");
    if (number % 2 == 0 || number % 3 == 0)
      System.out.println(number + " is divisible by 2 or 3.");
    if (number % 2 == 0 ^ number % 3 == 0)
      System.out.println(number + " is  divisible by 2 or 3, but not both.");
  }
}
6. De Morgan’s law 德摩根定律–> 简化expression3
!(number %2 == 0 && number % 3) 
//tha same as
number % 2 != 0 || number % 3 != 0
7. Leap year 闰年判断

if a year is divisible by 4 but not 100, or is divisible by 400 ,then the year is leap year.
能被4整除但不能被100整除,或者能被400整除的年份;如1900不是闰年,1904、2000是闰年;

//Listing3-7 | Check the year
//if (year % 100 == 0 && year % 400 == 0) //有冗余!不用写%100 == 0!
if (year % 400 == 0)
  System.out.println(year + " is the leap year.");
else if (year % 100 != 0 && year % 4 == 0)
  System.out.println(year + " is the leap year.");
else
  System.out.println(year + " is not the leap year.");*/
//textbook example
boolean b = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
8. Lottery 彩票

要点:
- generating a random number
- compare the all ele of a number
- and the usage of boolean operators

规则:
如果输入的数字顺序和数字都匹配,a== b,奖金$10000;所有数字匹配,a1== b2&&a2 ==b1奖金$3000;输入的一个数字匹配彩票的一个数字|| $1000

import java.util.Scanner;
//Listing3-8 involves generating a random number compare the all ele of a number and the usage of boolean operators
public class Lottery {
  public static void main(String[] args) {
    //随机产生一个两位数的彩票
    int lottery = (int)(Math.random()*100);//0~99, 100 integers, thus*100
    Scanner input = new Scanner(System.in);
    //prompt the user to enter guess
    System.out.print("Enter your lottery pick (two digits):");
    int guess = input.nextInt();

    //get digits from the lottery
    int lotteryDigit1 = lottery / 10;
    int lotteryDigit2 = lottery % 10;

    //get digits from the guess
    int guessDigit1 = guess / 10;
    int guessDigit2 = guess % 10;

    System.out.println("The lottery is "+ lottery);

    //check the guess
    if (guess == lottery)
      System.out.println("Exact match: you win $10000.");
    else if (guessDigit1 == lotteryDigit2 && guessDigit2 == lotteryDigit1)
      System.out.println("Match all digits: you win $2000");
    else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 ||guessDigit2 == lotteryDigit1 || guessDigit2 == lotteryDigit2)
      System.out.println("Match one digit: you win $1000");
    else
      System.out.println("Sorry, no match");
  }
}
9. 生肖

注意switch语句的格式:
- switch后加(),中间可是int,short,byte,char,String【long?】可计算出的值;
- case后的数值必须为同一类型,不能有形如“1+x”的expression,并且与语句之间有“:”;
- 注意加break【可选】;否则会顺次执行下去;如果故意省去,写个//
- 默认情况default【可选】,也要加“:”;

import java.util.Scanner;
//Listing3-9
public class ChineseZodiac {
  public static void main(String[] args ){
    Scanner input = new Scanner(System.in);

    System.out.print("Enter a year:");
    int year = input.nextInt();
    String zodiac = " ";

    switch(year % 12){
      case 0: zodiac = "Monkey";break;
      case 1: zodiac = "Rooster";break;
      case 2: zodiac = "Dog";break;
      case 3: zodiac = "Pig";break;
      case 4: zodiac = "Rat";break;
      case 5: zodiac = "Ox";break;
      case 6: zodiac = "Tigger";break;
      case 7: zodiac = "Rabbit";break;
      case 8: zodiac = "Dragon";break;
      case 9: zodiac = "Snake";break;
      case 10: zodiac = "Horse";break;
      case 11: zodiac = "Sheep";break;
    }
    System.out.println(zodiac);
  }
}
10. 条件操作

y = (x > 0)? 1: -1;
- 三元操作符、条件操作符
- 语法:boolean-expression ? expression1 : expression2;
- meaning: 如果true,结果为表达式1,否则2

if (x>0)
  y = 1;
else
  y = -1;
  //the same as
y = (x > 0)? 1: -1;

举例1 【赋值】两个数中较大的一个给max

max = (num1 > num2)? num1 : num2;

举例2 【执行】如果num是偶数打印“num is even”

System.out.println((num % 2 == 0)? "num is even" : "num is odd");
11. 操作符的优先级和结合规则
  • 除了赋值操作符,所有二元操作符都是左结合的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值