0

私は自分のプログラムを思い通りに動作させようとしています。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();
    }

}
4

3 に答える 3

1

スイッチでメソッドを実行し、最後に if ステートメントを削除するだけです。

           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);
            }
于 2013-06-16T20:22:47.310 に答える
0

アクセスされるメソッドifは、ステートメントではなく値を出力する必要があります。

   public static boolean kitchenMethod()
    {
        Boolean key = keyVal.nextBoolean();
        if (key)
          System.out.println("Key is here1");
        return key;
    }

if他のメソッドに対してそれを行い、ステートメントを削除します。

于 2013-06-16T20:21:24.323 に答える