私は自分のプログラムを思い通りに動作させようとしています。switch ステートメントに入るときに、ユーザーに値を入力させ、対応するユーザー作成メソッドに移動させたいと考えています。ユーザーが作成したメソッドのいずれかで、ランダムなブール値がメイン メソッドに返されます。値が true で、すべてのメソッドではなく、アクセスした 1 つのメソッドに対してのみ、プログラムに println "key is here" のみを表示させたいと考えています。プログラムを実行すると、必要なメソッドだけでなく、if ステートメントのすべてのメソッドが処理されるためです。誰か助けてくれませんか?ユーザーが作成したメソッドが nextBoolean を返すようにし、println "key is here" を引き続きメイン メソッドに配置したいと考えています。これを行う方法はありますか?前もって感謝します。私はこれで完全な初心者です:)コードは次のとおりです。
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();
}
}