Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在androidstudio中调用get方法时,它返回一个零值,即使之前它是一个int_Java_Android - Fatal编程技术网

Java 在androidstudio中调用get方法时,它返回一个零值,即使之前它是一个int

Java 在androidstudio中调用get方法时,它返回一个零值,即使之前它是一个int,java,android,Java,Android,这是我的一个方法的代码,该方法选择一个随机值并将其分配给question1select。在我尝试使用get方法之前,这似乎工作得很好,它总是返回零,即使它不应该返回零,因为println说它有一个值 public void selectQuestions() { System.out.println("selectQuestions is running"); question1select = randomNumber(); question2select = rand

这是我的一个方法的代码,该方法选择一个随机值并将其分配给question1select。在我尝试使用get方法之前,这似乎工作得很好,它总是返回零,即使它不应该返回零,因为println说它有一个值

public void selectQuestions()
{
    System.out.println("selectQuestions is running");
    question1select = randomNumber();
    question2select = randomNumber();
    question3select = randomNumber();
    question4select = randomNumber();
    question5select = randomNumber();
    System.out.println("it has selected numbers and is now testing them");
    if (question1select == question2select)
    {
        selectQuestions();
    }
    else if (question1select == question3select)
    {
        selectQuestions();
    }
    else if (question1select == question4select)
    {
        selectQuestions();
    }
    else if (question1select == question5select)
    {
        selectQuestions();
    }
    else if (question2select == question3select)
    {
        selectQuestions();
    }
    else if (question2select == question4select)
    {
        selectQuestions();
    }
    else if (question2select == question5select)
    {
        selectQuestions();
    }
    else if (question3select == question4select)
    {
        selectQuestions();
    }
    else if (question3select == question5select)
    {
        selectQuestions();
    }
    else if (question4select == question5select)
    {
        selectQuestions();
    }
    else
    {
        System.out.println(" the value of question1select is " +question1select);
        System.out.println(" the value of question2select is " +question2select);
        System.out.println(" the value of question3select is " +question3select);
        System.out.println(" the value of question4select is " +question4select);
        System.out.println(" the value of question5select is " +question5select);
        page1questionaccessor access = new page1questionaccessor();
        access.questionAnswer();
    }
}


public int getNumber1()
{
    System.out.println(" the value of question1select in getNumber1 is " +question1select);
    return question1select;
}
public int getNumber2()
{
    return question2select;
}
public int getNumber3()
{
    return question3select;
}
public int getNumber4()
{
    return question4select;
}
public int getNumber5()
{
    return question5select;
}
这就是所谓的getNumber1方法,quicknote——所有if语句都是复制/粘贴的,我不认为它们是问题所在

public void questionAnswer()
{
    System.out.println("it should be testing number values");
    questionchanger change;
    System.out.println("1");
    change = new questionchanger();
    System.out.println("2");
    int x = change.getNumber1();
    System.out.println("4");
    System.out.println("x = " +x);

    if (x == 1)
    {
        System.out.println("a question should be accsing");
        question_answers question;
        question = new question_answers();
        question.question1();

    }
    else if (x == 2)
    {
        System.out.println("a question should be accsing");
        question_answers question;
        question = new question_answers();
        question.question2();
    }
    else if (x == 3)
    {
        System.out.println("a question should be accsing");
        question_answers question;
        question = new question_answers();
        question.question3();
    }
    else if (x == 4)
    {
        System.out.println("a question should be accsing");
        question_answers question;
        question = new question_answers();
        question.question4();
    }
    else if (x == 5)
    {
        System.out.println("a question should be accsing");
        question_answers question;
        question = new question_answers();
        question.question5();
    }
    else if (x == 6)
    {
        System.out.println("a question should be accsing");
        question_answers question;
        question = new question_answers();
        question.question6();
    }
    else if (x == 7)
    {
        System.out.println("a question should be accsing");
        question_answers question;
        question = new question_answers();
        question.question7();
    }
    else if (x == 8)
    {
        System.out.println("a question should be accsing");
        question_answers question;
        question = new question_answers();
        question.question8();
    }
    else if (x == 9)
    {
        System.out.println("a question should be accsing");
        question_answers question;
        question = new question_answers();
        question.question9();
    }
    else if (x == 10)
    {
        System.out.println("a question should be accsing");
        question_answers question;
        question = new question_answers();
        question.question10();
    }
    else if (x == 11)
    {
        System.out.println("a question should be accsing");
        question_answers question;
        question = new question_answers();
        question.question11();
    }
    else if (x == 12)
    {
        System.out.println("a question should be accsing");
        question_answers question;
        question = new question_answers();
        question.question12();
    }
    else if (x == 13)
    {
        System.out.println("a question should be accsing");
        question_answers question;
        question = new question_answers();
        question.question13();
    }

    else if (x == 14)
    {
        System.out.println("a question should be accsing");
        question_answers question;
        question = new question_answers();
        question.question14();
    }
    else if (x == 15)
    {
        System.out.println("a question should be accsing");
        question_answers question;
        question = new question_answers();
        question.question15();
    }
    else
    {
        System.out.println("an imposible number was selected");
    }

}
}

这是航海日志

10-04 15:46:43.196    9686-9686/com.example.michael.trivia I/System.out﹕ 1371012it has selected numbers and is now testing them

10-04 15:46:43.196    9686-9686/com.example.michael.trivia I/System.out﹕ the value of question1select is 1

10-04 15:46:43.196    9686-9686/com.example.michael.trivia I/System.out﹕ the value of question2select is 3

10-04 15:46:43.196    9686-9686/com.example.michael.trivia I/System.out﹕ the value of question3select is 7

10-04 15:46:43.196    9686-9686/com.example.michael.trivia I/System.out﹕ the value of question4select is 10

10-04 15:46:43.196    9686-9686/com.example.michael.trivia I/System.out﹕ the value of question5select is 12

10-04 15:46:43.196    9686-9686/com.example.michael.trivia I/System.out﹕ it should be testing number values

10-04 15:46:43.196    9686-9686/com.example.michael.trivia I/System.out﹕ 1

10-04 15:46:43.196    9686-9686/com.example.michael.trivia I/System.out﹕ 2

10-04 15:46:43.196    9686-9686/com.example.michael.trivia I/System.out﹕ the value of question1select in getNumber1 is 0

10-04 15:46:43.196    9686-9686/com.example.michael.trivia I/System.out﹕ 4

10-04 15:46:43.196    9686-9686/com.example.michael.trivia I/System.out﹕ x = 0

10-04 15:46:43.196    9686-9686/com.example.michael.trivia I/System.out﹕ an imposible number was selected

如果我可以发表评论,这会更简单,但我只有一个代表,所以。。。嗯

简短回答

根据您显示的代码,似乎没有任何错误。我最好的猜测是变量范围的问题,甚至可能是继承的问题

长答案

这是你所有的代码吗?您应该知道,如果您还不知道,当一个整数被初始化但未设置为值时,它默认为零,如下所示:

private static int question1select; //this would equal 0
现在,我可以看到您通过以下方式将其设置为一个数字:

question1select = randomNumber();
在相同的方法中,您继续将question1select的值打印到logcat。它起作用了。但是,当您稍后使用另一种方法打印它时,它显示它已重置回0

因为这不是你的全部代码,我不能确定。但是,我确实认为您可能多次初始化同一变量question1select,只是在不同的范围内。您可以在类的开头全局初始化它,如下所示:

public class yourClassName{
    private static int question1select;
    ...
然后再次在局部范围内,如selectQuestions方法:

public void selectQuestions(){
    private static int question1select;
    ...
然后,当您使用randomNumber()方法更改question1select的值时,它可能会更改局部值,但不会更改全局值,将全局值保留为零。最后,当您尝试使用getNumber1()方法从另一个类打印新值时,该方法可能被设置为检索全局值,而全局值仍然为0


正如我所说,这只是根据我所看到的,我最好的猜测。

在你的代码中,change=newquestionchanger();系统输出打印项次(“2”);int x=change.getNumber1();您正在创建一个新的
questionchanger
对象,并立即对其调用
getNumber1()
,它返回
question1select
的初始值,即0。可能你打算使用现有的对象?这正是我打算做的。我怎么做呢,我试着扩展第二个类,但仍然返回零。那么我如何调用现有对象的方法呢。非常感谢ashoke。只在这里发表评论是因为我没有足够的代表评论这篇文章。然而,ashoke是对的。您应该在questionchanger类中定义question1select、question2select等。。。变量如“publicstaticintquestion1select;”等等。然后,由于它们是公共的,您不必调用任何get方法。只需做questionchanger.Question1选择引用它即可。同样,请确保将其定义为静态的,以便它可以是类的一部分,而不仅仅是类实例的一部分。