(根据 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;
}
}
}