Advertisement
fosterbl

L8LogicalOperators1.java

Oct 1st, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class L8LogicalOperators1{
  4. public static void main(String[] args){
  5. //And (&&) Example
  6. boolean hasCleanedRoom = true, hasDoneHomework = true;
  7. if( hasCleanedRoom && hasDoneHomework ){
  8. System.out.println("You can go out");
  9. }
  10. else{
  11. System.out.println("No you can't go out");
  12. }
  13.  
  14. //AND Truth Table
  15. System.out.println("F&F: " + ( false && false ));
  16. System.out.println("F&T: " + ( false && true ));
  17. System.out.println("T&F: " + ( true && false ));
  18. System.out.println("T&T: " + ( true && true ));
  19.  
  20. //OR (||) Example
  21. boolean isRaining = true, isSuperSunny = false;
  22. if( isRaining || isSuperSunny ){
  23. System.out.println("Bring an umbrella");
  24. }
  25. else{
  26. System.out.println("Don't bring an umbrella");
  27. }
  28.  
  29. //Or Truth Table
  30. System.out.println("F|F: " + ( false || false ));
  31. System.out.println("F|T: " + ( false || true ));
  32. System.out.println("T|F: " + ( true || false ));
  33. System.out.println("T|T: " + ( true || true ));
  34.  
  35. //.equals example
  36. Scanner kb = new Scanner(System.in);
  37. System.out.print("Please enter your password: ");
  38. String userPw = kb.next();
  39. if( userPw.equals("abc123") ){
  40. System.out.println("Logged in");
  41. }
  42. else{
  43. System.out.println("Incorrect password. 0 attempts remaining");
  44. }
  45.  
  46. //WRONG WAY
  47. if( userPw == "abc123" ) System.out.println("WRONG WAY logged in");
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement