Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class L8LogicalOperators1{
- public static void main(String[] args){
- //And (&&) Example
- boolean hasCleanedRoom = true, hasDoneHomework = true;
- if( hasCleanedRoom && hasDoneHomework ){
- System.out.println("You can go out");
- }
- else{
- System.out.println("No you can't go out");
- }
- //AND Truth Table
- System.out.println("F&F: " + ( false && false ));
- System.out.println("F&T: " + ( false && true ));
- System.out.println("T&F: " + ( true && false ));
- System.out.println("T&T: " + ( true && true ));
- //OR (||) Example
- boolean isRaining = true, isSuperSunny = false;
- if( isRaining || isSuperSunny ){
- System.out.println("Bring an umbrella");
- }
- else{
- System.out.println("Don't bring an umbrella");
- }
- //Or Truth Table
- System.out.println("F|F: " + ( false || false ));
- System.out.println("F|T: " + ( false || true ));
- System.out.println("T|F: " + ( true || false ));
- System.out.println("T|T: " + ( true || true ));
- //.equals example
- Scanner kb = new Scanner(System.in);
- System.out.print("Please enter your password: ");
- String userPw = kb.next();
- if( userPw.equals("abc123") ){
- System.out.println("Logged in");
- }
- else{
- System.out.println("Incorrect password. 0 attempts remaining");
- }
- //WRONG WAY
- if( userPw == "abc123" ) System.out.println("WRONG WAY logged in");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement