Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- // comments out everything so other things can run while this code is broken
- // /*
- int firstSwitch = 0;
- int secondSwitch = 0;
- int lamp = 0;
- public static void main(String[] args) {
- //ask for input
- System.out.println("You are in a hallway. \n" +
- "The hallway is dark but alas, there are two switches!\n" +
- "Enter (1) or (2) to chose which switch to flip.\n" +
- "Enter -1 to exit program.");
- Scanner userInput = new Scanner(System.in);
- int input = userInput.nextInt();
- //loops through simulation of hallway
- while (input /= -1){
- if (input == 1){
- toggleFirstSwitch();
- getFirstSwitchState();
- getLampState();
- int input = userInput.nextInt();
- }else if (input == 2){
- toggleSecondSwitch();
- getSecondSwitchState();
- getLampState();
- int input = userInput.nextInt();
- }else{
- System.out.println("What are you doing? That's not a switch!");
- }
- }
- System.out.println("You get tired of flipping switches and go on with your day.");
- }
- //gets the FirstSwitchState
- public static int getFirstSwitchState() {
- if (firstSwitch == 0){
- System.out.println("Switch One is in the off position.");
- }else{
- System.out.println("Switch One is in the on position.");
- }
- return firstSwitch;
- }
- //gets the second switch state
- public static int getSecondSwitchState() {
- if (secondSwitch == 0){
- System.out.println("Switch Two is in the off position.");
- }else{
- System.out.println("Switch Two is in the on position.");
- }
- return secondSwitch;
- }
- //flips the value of the First Switch
- public static void toggleFirstSwitch() {
- if (firstSwitch == 0) {
- firstSwitch += 1;
- } else {
- firstSwitch -= 1;
- }
- }
- //flips the value of the Second Switch
- public static void toggleSecondSwitch() {
- if (secondSwitch == 0) {
- secondSwitch += 1;
- } else {
- secondSwitch -= 1;
- }
- }
- //gives you the state of the lamp
- public static int getLampState() {
- if(firstSwitch == 1|| secondSwitch == 1){
- System.out.println("The lamp is on.");
- }else{
- System.out.println("The lamp is off.");
- }
- return lamp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement