创造一个猜谜游戏

时间:2014-03-01 06:09:21

标签: java

我正在追随一个我必须创造一个猜谜游戏的集团。我有点理解这个问题,但我不知道如何修复它。

问题是对于方法“loop”numberDrawn不是定义的整数。但是我在主要参数体中定义它,其中放置了“循环”,所以“循环”怎么会看不到我已经定义它。

import java.util.Random;
import java.util.Scanner;

public class GuessingNumberGame {

    private static int drawNumber() {
        return new Random().nextInt(101);
    }


    public static void main(String[] args) {

        Scanner reader = new Scanner(System. in );

        System.out.println("Guess a number: ");
        int num = Integer.parseInt(reader.nextLine());
        int numberDrawn = drawNumber();

        if (numberDrawn > num) {
            System.out.println("The number is greater");
            loop();
        } else if (numberDrawn < num) {
            System.out.println("The number is lesser");
            loop();
        } else {
            System.out.println("Congratulations, your guess is correct!");
        }
    }

    public static void loop() {

        Scanner reader = new Scanner(System. in );
        System.out.println("Guess a number: ");
        int num = Integer.parseInt(reader.nextLine());

        if (numberDrawn > num) {
            System.out.println("The number is greater");
            loop();
        } else if (numberDrawn < num) {
            System.out.println("The number is lesser");
            loop();
        } else {
            System.out.println("Congratulations, your guess is correct!");
        }
    }
}

4 个答案:

答案 0 :(得分:1)

问题是numberDrawnmain()的本地,并且loop()无法访问。除非传递,否则变量不能在loop()中使用。

然而,在我看来,第二个loop()函数是不必要的,并将创建递归循环。我修改了main()以正常工作并包含一个循环:

import java.util.Random;
import java.util.Scanner;

public class GuessingNumberGame {

private static int drawNumber() {
    return new Random().nextInt(101);
}


public static void main(String[] args) {

    Scanner reader = new Scanner(System.in);

    while(true) {
        System.out.println("Guess a number: ");        
        int num = Integer.parseInt(reader.nextLine());
        int numberDrawn = drawNumber();


        if (numberDrawn > num) {
            System.out.println("The number is greater");

        }   else if (numberDrawn < num) { 
            System.out.println("The number is lesser");

        }   else {
            break;
        }

    }

    System.out.println("Congratulations, your guess is correct!");
}

答案 1 :(得分:1)

问题是您在方法中定义了“ numberDrawn ”变量,方法中定义的变量是方法的本地变量。只需在变量声明中定义变量,并使其变为静态

import java.util.Random;
import java.util.Scanner;

public class GuessingNumberGame {


     static int numberDrawn=0;
    private static int drawNumber() {
        return new Random().nextInt(101);
    }


    public static void main(String[] args) {

        Scanner reader = new Scanner(System. in );

        System.out.println("Guess a number: ");
        int num = Integer.parseInt(reader.nextLine());
        numberDrawn= drawNumber();

        if (numberDrawn > num) {
            System.out.println("The number is greater");
            loop();
        } else if (numberDrawn < num) {
            System.out.println("The number is lesser");
            loop();
        } else {
            System.out.println("Congratulations, your guess is correct!");
        }
    }

    public static void loop() {

        Scanner reader = new Scanner(System. in );
        System.out.println("Guess a number: ");
        int num = Integer.parseInt(reader.nextLine());

        if (numberDrawn > num) {
            System.out.println("The number is greater");
            loop();
        } else if (numberDrawn < num) {
            System.out.println("The number is lesser");
            loop();
        } else {
            System.out.println("Congratulations, your guess is correct!");
        }
    }
}

答案 2 :(得分:0)

这不起作用,因为main()也被认为是Java中的一个不同方法,而main()中定义的所有变量都不能直接访问外部方法,即。在你的情况下循环()。一种解决方案是将变量numberdrawn作为参数传递给方法。另一个是你可以在main方法之外定义变量numberdrawn。

答案 3 :(得分:0)

试试这个,在类级别声明你的变量:

public class GuessingNumberGame {
    private static int numberDrawn;
在你的主要();

int num = Integer.parseInt(reader.nextLine());//no change here
numberDrawn = drawNumber();//remove int keyword