游戏:国家和城市

时间:2019-05-08 12:57:49

标签: java

我正在尝试用Java做游戏。

说明:

  

控制台选择随机字母,玩家必须写下正确的国家和城市。

在我的代码中,我想显示玩家选择后的计算机答案;但是,我的代码无法正常工作。它显示给我一个字母太多的答案。

正确:

START
Enter country in the given letter: C
Croatia
Correct country
Computer answer: Czech Republic
Enter city ....

错误:

START
Entere country in the given letter: C
Croatia
Correct country.
Random letter is: B
Belgium
Random letter is: B
Belgium
Random letter is: C
Croatia
Random letter is: E
Egypt
Random letter is: C
Croatia
...

这是我的代码:

public class MainGame {

    public static int menu() {
        System.out.println();
        System.out.println("     ****************************************");
        System.out.println("     *                 MENU                 *");
        System.out.println("     ****************************************");
        System.out.println("     1. Start");
        System.out.println("     2. Instruction");
        System.out.println("     0. The End");

        Scanner in = new Scanner(System.in);
        int w = in.nextInt();

        return w;
    }

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);

        int choice = menu();


        while (choice != 0) {
            switch (choice) {
                case 1:


                    System.out.println("START");

                    country();


                    break;

                case 2:

                    System.out.println("INSTRUCTION");
                    System.out.println("Bla bla bla");

                    break;
            }

            System.out.println("\nClick Enter, to continue...");
            System.in.read();

            choice = menu();
        }


        System.out.println("     ****************************************");
        System.out.println("\n     The end \n\n");
    }

    public static String answersCountry() {
        char sign = RandomLetter.ranomRandom();

        for (int i = 0; i < 1; i++) {
            System.out.println("Random letter is: " + sign);
            switch (sign) {
                case 'A':
                    System.out.println("Austria");
                    break;
                case 'B':
                    System.out.println("Belgium");
                    break;
                case 'C':
                    System.out.println("Croatia");
                    break;
                case 'D':
                    System.out.println("Denmark");
                    break;
                case 'E':
                    System.out.println("Egypt");
                    break;
            }
        }

        return answersCountry();
    }

    public static char country() {


        Scanner scanner2 = new Scanner(System.in);


        boolean result = false;

        for (int i = 0; i < 1; i++) {
            char randomLetter = RandomLetter.ranomRandom();


            while (result == false) {
                System.out.println("Entere country in the given letter: " + randomLetter);
                String countryName = scanner2.next();
                char firstLetter1 = countryName.charAt(0);
                if (firstLetter1 == randomLetter) {
                    System.out.println("Correct country.");

                    System.out.println("Computer answer: " + answersCountry());
                    result = true;

                } else {
                    System.out.println("Incorrect country");
                    result = false;
                    break;
                }
            }

            boolean result1 = false;

            while (result1 == false) {
                System.out.println("Enter city in the given letter: " + randomLetter);
                String cityName = scanner2.next();
                char firstLetter2 = cityName.charAt(0);
                if (firstLetter2 == randomLetter) {
                    System.out.println("Correct city");
                    result1 = true;
                } else {
                    System.out.println("Incorrect city");
                    result1 = false;
                    break;
                }
                break;
            }


        }

        return country();}}

和带有randomLetter的类:

public class RandomLetter {
    public static char ranomRandom() {
        Random random = new Random();
        char[] abc = {'A', 'B', 'C', 'D', 'E'};
        int index = random.nextInt(abc.length);
        char randomLetter = abc[index];
        return randomLetter;
    }
}

您能告诉我如何仅显示一个计算机答案吗?谢谢。

1 个答案:

答案 0 :(得分:0)

使用return answersCountry();递归调用该函数。此外,您正在该函数内部进行打印,当您只需要返回所需的字符串时,因为稍后将在此处进行打印:System.out.println("Computer answer: " + answersCountry());

因此,我只需要删除return answersCountry();行,并用返回值替换该函数中的每个System.out.println();,例如:

System.out.println("Anglia");替换为return "Anglia";