Reputation:
I am trying to get my program to work the way I want. When I go in the switch statement I want to have the user type in a value and go to that corresponding user createdmethod. When in one of the user created method I have a random boolean value returned to the main method. I want my program to only println "key is here" when the value is true and ONLY for the one method I accessed, not all of the methods. Because when I run the program it will go through all the methods in the if statements and not just the one i want. Can someone please help me? I want to keep the user created methods returning nextBoolean and have the println "key is here" to be in the main method still. Is there a way to do this? Thanks in advance. I'm a total beginner at this :) Here is the code:
import java.util.*;
import java.util.Random;
public class JavaGameSearchHouse
{
public static Scanner console = new Scanner(System.in);
public static boolean keyValue;
public static Random keyVal = new Random();
public static void main(String[] args)
{
String roomValue;
int counter;
int keepGoing = 0;
do{
System.out.println("Lets search this house for the key");
roomValue = console.next();
switch(roomValue.toLowerCase())
{
case "kitchen": kitchenMethod();
break;
case "bedroom": bedroomMethod();
break;
case "familyRoom": familyRoomMethod();
break;
case "livingRoom": livingRoomMethod();
break;
case "bathroom": bathroomMethod();
break;
case "basement": basementMethod();
break;
default: System.exit(0);
}
if(kitchenMethod())
System.out.println("Key is here1");
if(bedroomMethod())
System.out.println("Key is here2");
if(familyRoomMethod())
System.out.println("Key is here3");
if(livingRoomMethod())
System.out.println("Key is here4");
if(bathroomMethod())
System.out.println("Key is here5");
if(basementMethod())
System.out.println("Key is here6");
System.out.println("press 1 to search again");
keepGoing = console.nextInt();
}while(keepGoing == 1);
}
public static boolean kitchenMethod()
{
return keyVal.nextBoolean();
}
public static boolean bedroomMethod()
{
return keyVal.nextBoolean();
}
public static boolean familyRoomMethod()
{
return keyVal.nextBoolean();
}
public static boolean livingRoomMethod()
{
return keyVal.nextBoolean();
}
public static boolean bathroomMethod()
{
return keyVal.nextBoolean();
}
public static boolean basementMethod()
{
return keyVal.nextBoolean();
}
}
Upvotes: 0
Views: 276
Reputation:
The method that is accessed should print the value, not the if
statement.
public static boolean kitchenMethod()
{
Boolean key = keyVal.nextBoolean();
if (key)
System.out.println("Key is here1");
return key;
}
do it for other methods and remove if
statements.
Upvotes: 0
Reputation: 425073
Remove the switch. Your approach calls the room methods every time, even if not required.
Instead, do this:
if(roomValue.equalsIgnoreCase("kitchen") && kitchenMethod()) {
System.out.println("Key is here1");
}
Etc.
This approach will only call the room method if required.
Alternatively, you could embed the if statements inside the switch, but it would make for some ugly code.
Upvotes: 0
Reputation: 93842
You could just run your methods in the switch and remove the if statements at the end :
case "kitchen": if(kitchenMethod())
System.out.println("Key is here1");
break;
case "bedroom": if(bedroomMethod())
System.out.println("Key is here2");
break;
case "familyRoom": if(familyRoomMethod())
System.out.println("Key is here3");
break;
case "livingRoom": if(livingRoomMethod())
System.out.println("Key is here4");
break;
case "bathroom": if(bathroomMethod())
System.out.println("Key is here");
break;
case "basement": if(basementMethod())
System.out.println("Key is here6");
break;
default: System.exit(0);
}
Upvotes: 1