Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. public class Main {
  2.  
  3. // comments out everything so other things can run while this code is broken
  4. // /*
  5.  
  6.  
  7.  
  8. int firstSwitch = 0;
  9. int secondSwitch = 0;
  10. int lamp = 0;
  11.  
  12. public static void main(String[] args) {
  13.  
  14. //ask for input
  15. System.out.println("You are in a hallway. \n" +
  16. "The hallway is dark but alas, there are two switches!\n" +
  17. "Enter (1) or (2) to chose which switch to flip.\n" +
  18. "Enter -1 to exit program.");
  19.  
  20. Scanner userInput = new Scanner(System.in);
  21. int input = userInput.nextInt();
  22.  
  23. //loops through simulation of hallway
  24. while (input /= -1){
  25. if (input == 1){
  26. toggleFirstSwitch();
  27. getFirstSwitchState();
  28. getLampState();
  29. int input = userInput.nextInt();
  30. }else if (input == 2){
  31. toggleSecondSwitch();
  32. getSecondSwitchState();
  33. getLampState();
  34. int input = userInput.nextInt();
  35. }else{
  36. System.out.println("What are you doing? That's not a switch!");
  37. }
  38. }
  39. System.out.println("You get tired of flipping switches and go on with your day.");
  40. }
  41.  
  42.  
  43. //gets the FirstSwitchState
  44. public static int getFirstSwitchState() {
  45.  
  46. if (firstSwitch == 0){
  47. System.out.println("Switch One is in the off position.");
  48. }else{
  49. System.out.println("Switch One is in the on position.");
  50. }
  51.  
  52. return firstSwitch;
  53. }
  54.  
  55. //gets the second switch state
  56. public static int getSecondSwitchState() {
  57. if (secondSwitch == 0){
  58. System.out.println("Switch Two is in the off position.");
  59. }else{
  60. System.out.println("Switch Two is in the on position.");
  61. }
  62. return secondSwitch;
  63. }
  64.  
  65. //flips the value of the First Switch
  66. public static void toggleFirstSwitch() {
  67. if (firstSwitch == 0) {
  68. firstSwitch += 1;
  69. } else {
  70. firstSwitch -= 1;
  71. }
  72. }
  73.  
  74. //flips the value of the Second Switch
  75. public static void toggleSecondSwitch() {
  76. if (secondSwitch == 0) {
  77. secondSwitch += 1;
  78. } else {
  79. secondSwitch -= 1;
  80. }
  81. }
  82.  
  83. //gives you the state of the lamp
  84. public static int getLampState() {
  85. if(firstSwitch == 1|| secondSwitch == 1){
  86. System.out.println("The lamp is on.");
  87. }else{
  88. System.out.println("The lamp is off.");
  89. }
  90. return lamp;
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement