复习Java(三)简单的逻辑运算

(根据 Derek Banas的教学视频整理的笔记)


public class LessonThree {

public static void main(String[] args) {

// Relational Operators: >, <, ==, !=, >=, <=

// Logical Operators: !, &, &&, |, ||, ^

int randomNumber = (int)(Math.random()*50);

System.out.println("The random number is: "+ randomNumber);

if(randomNumber > 25)

{

System.out.println("The random number is less than 25");

}

else if (randomNumber == 25)

{

System.out.println("The random number is equal to 25");

}

else if (randomNumber != 15)

{

System.out.println("The random number is not equal to 15");

}

else if (randomNumber >= 25)

{

System.out.println("The random number is greater than 25");

}

else if (randomNumber <= 25)

{

System.out.println("The random number is smaller than 25");

}

else {

System.out.println("The random number is: "+ randomNumber);

}

// &: Returns true if boolean value on the right and left side are 

//    both true (always evaluates both boolean value)

// &&: Returns true if boolean value on the right and left side are

//     both true (Stops evaluating after first true)

// |: Returns true if either boolean value are ture (evaluates both)

// ||: Returns true if either boolean value are ture 

// ^: Returns true if there is 1 true and 1 false

if (!(false))

{

System.out.println("I turned false into true");

}

if ((true) && (true))

{

System.out.println("Both are true");

}

if ((true) || (false))

{

System.out.println("At least one is true");

}

if ((true) ^ (false))

{

System.out.println("One is true and one is false");

}

// Conditional Operator

int valueOne = 1;

int valueTwo = 2;

int biggerValue = (valueOne > valueTwo) ? valueOne:valueTwo;

System.out.println("The bigger value is: "+ biggerValue);

// Switch and case

char theGrade = 'A';

switch (theGrade) {

case 'A':

System.out.println("Great job");

break;

case 'B':

System.out.println("Good job");

break;

case 'C':

System.out.println("OK");

break;

case 'D':

System.out.println("Bad");

break;

default:

System.out.println("Your failed");

break;

}

}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值