繁体   English   中英

在Android Studio中调用get方法时,即使之前是int类型,它也会返回零值

[英]When calling a get method in Android Studio it returns a zero value even though earlier it was an int

这是我的一种方法的代码,该方法选择一个随机值并将其分配给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方法,请快速注意-所有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");
    }

}

}

这是logcat

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

如果可以发表评论,这会比较简单,但是我只有1个代表,所以...是的...

简短答案

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

长答案

这是您的全部代码吗? 您应该知道(如果尚未)在初始化整数但未将其设置为值时,它默认为零,如下所示:

private static int question1select; //this would equal 0

现在,我可以看到您清楚地通过以下方式将其设置为数字:

question1select = randomNumber();

使用相同的方法,继续将问题1选择的值打印到logcat中。 有用。 但是,以后使用另一种方法打印时,它表明它已重置为0。

由于这不是您的全部代码,所以我不确定。 但是,我确实认为您可能会在不同的范围内多次初始化同一个变量,question1select。 您可能像这样在全局的类开始时进行初始化:

public class yourClassName{
    private static int question1select;
    ...

然后再次在本地范围内,例如selectQuestions方法:

public void selectQuestions(){
    private static int question1select;
    ...

然后,当您使用randomNumber()方法更改question1select的值时,它可能会更改局部变量1,而不是全局变量1,从而将全局变量1保留为零。 最后,当您尝试使用getNumber1()方法从另一个类中打印新值时,该方法可能设置为检索全局值,该值仍为0。

就像我说的那样,这只是基于我所见所闻的最佳猜测。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM