如何使变量可供其他方法访问

时间:2014-04-07 07:54:41

标签: java

我正在努力建立"简单"猜测游戏:D的想法是,如果anwer是正确的话,while循环将每次检查,如果没有,那么用户应该再次尝试猜测。但是我怎样才能使方法suvaline生成的方法随机数可以访问检查和游戏方法。

package AlsoGame;
import java.util.Random;
import java.util.Scanner;
public class AlsoGame {



public static void main(String[] args) {
    System.out.println("Please try to guess a number in range from 1 to 10");
    AlsoGame  mainObject = new AlsoGame ();
    int katse = mainObject.suvaline();
    System.out.println(katse);
    mainObject.check(katse);
    mainObject.gameplay(katse);

}

public int InsertNum(){
    System.out.println("Bitte, bitte enter a number");
    Scanner input = new Scanner(System.in);
    int inputNumber = input.nextInt();  
    return inputNumber;
}

public static int suvaline(){ 
    Random number = new Random();
    int x = 1 + number.nextInt(10);
    return x;   
}

public void gameplay(int katse){
    boolean check = false;
    int inputNumber = InsertNum();

    while(check == false){
        check = check(inputNumber);
        if (check == true){
            System.out.println("You guessed correctly, you win");
            return;
        }
        else{ 
            if (inputNumber >  10 || inputNumber <= 0 ){
                System.out.println("Cant you read, you fool, I said guess a number in range from 1 to 10");

                if (inputNumber > katse){
                    System.out.println("you guessed to high");

                    if (inputNumber < katse){
                        System.out.println("You guessed to low");
                    }
                }
            }
        }
    }
}

public boolean check(int randomNum){
    System.out.println();
    final int thresholdVal = randomNum ;
    System.out.println(thresholdVal);

    if (randomNum  == thresholdVal){
        return true;
    }else{
        return false;
    }
}
}

我编辑它,现在游戏方法中的else语句没有被执行:D

package AlsoGame; 
import java.util.Random;
import java.util.Scanner;
public class AlsoGame {


public final int bla = suvaline();
public static void main(String[] args) {

    System.out.println("Please try to guess a number in range from 1 to 10");
    AlsoGame  mainObject = new AlsoGame ();
    mainObject.gameplay();

}

public int InsertNum(){
    System.out.println("Bitte, bitte enter a number");
    Scanner input = new Scanner(System.in);
    int inputNumber = input.nextInt();  
    return inputNumber;
}

public int suvaline(){ 
    Random number = new Random();
    int x = 1 + number.nextInt(10);
    return x;   
}

public void gameplay(){
    int katse = bla;
    System.out.println(katse);
    boolean check = false;

    while(check == false){
        int inputNumber = InsertNum();
        check = check(inputNumber);

        if (check == true){
            System.out.println("You guessed correctly, you win");
        }
        else{
            if (inputNumber >  10 || inputNumber <= 0 ){
                System.out.println("Cant you read, you fool, I said guess a numbEr in range from 1 to 10");

                if (inputNumber > katse){
                    System.out.println("you guessed to high");

                    if (inputNumber < katse){
                        System.out.println("You guessed to low");
                    }
                }
            }
        }
    }
}

public boolean check(int randomNum){
    int thresholdval = bla;

    if (randomNum  == thresholdval){
        return true;
    }else{
        return false;
    }
}
}

1 个答案:

答案 0 :(得分:0)

我不明白为什么你问这个问题,因为你已经将变量作为参数访问了。但要使其在所有方法中都可访问,您可以这样做:

public class AlsoGame {
    private int katse = suvaline();

    public boolean check(){
        int thresholdval = bla;

        if (this.katse  == thresholdval){
            return true;
        }else{
            return false;
        }
    }

    public void some_other_method(){
        //do something with this.katse
    }
}