我的Int()总是返回0?

时间:2011-11-14 22:32:14

标签: java android

我有这个问题...... 请注意,tip.setText告诉我它高于0。 我还将在另一个类中调用getResults方法。 (Int'正确'已经高于0)

我想知道为什么getResults中的正确值始终为0,我该如何解决?

     public int correct = 0;

    private String[] questions =
   {"Question 1", 
    "Question 2",
    "Question 3", 
    "Question 4",
    "Question 5"
    };

public void onClick(View view) {
        if (alive == false) {
        //  startActivity(new Intent("com.aleksei.etb.END"));
            return;
        }
        try {
        button_click = MediaPlayer.create(this, R.raw.button_click);
        button_click.start();
        switch(view.getId()){
        case R.id.button5: //main
            break;
        case R.id.button1: //answer_1
            if(correct(1))
                correct++;


            break;
        case R.id.button2: //answer_2
            if(correct(2))
                correct++;

            break;
        case R.id.button3: //answer_3
            if(correct(3))
                correct++;

            break;
        case R.id.button4: //answer_3
            if(correct(4))
                correct++;


            break;

        default:
            break;


        }
        Game(i1);
        tip.setText("Correct answers "+correct); //this thing tells me that it's not 0. (Well, in the beggining it's 0, but after counter goes up! (correct++)
        } catch (Exception ex){}
    }

    public int getResults(){
        return (int)(correct*5)/(questions.length+1);
    }

编辑:

public class ETBetaActivity extends Activity implements View.OnClickListener {

    Button answer_1,
    answer_2,answer_3,
    answer_4,main;

    TextView q_textview,
    tip;

    private String a1,a2,a3,a4 = "";

    private int i1 = 0;
    public float correct = 0;

    private boolean alive = true;

    MediaPlayer button_click;

    private String[] questions =
   {"Question 1", 
    "Question 2",
    "Question 3", 
    "Question 4",
    "Question 5"
    };
    private String[] answers_correct =
   {"Correct answer 1",
    "Correct answer 2",
    "Correct answer 3", 
    "Correct answer 4",
    "Correct answer 5"
    };

    private String[][] answers_wrong = 
    { {"Incorrect answer 1-1", "Incorrect answer 1-2" , "Incorrect answer 1-3"},
      {"Incorrect answer 2-1", "Incorrect answer 2-2" , "Incorrect answer 2-3"},
      {"Incorrect answer 3-1", "Incorrect answer 3-2" , "Incorrect answer 3-3"},
      {"Incorrect answer 4-1", "Incorrect answer 4-2" , "Incorrect answer 4-3"},
      {"Incorrect answer 5-1", "Incorrect answer 5-2" , "Incorrect answer 5-3"},


    };


    List<String> question_list = new ArrayList<String>();
    List<String> answer_list_correct = new ArrayList<String>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getData();
    }


    @Override
    public void onClick(View view) {
        if (alive == false) {
        //  startActivity(new Intent("com.aleksei.etb.END"));
            return;
        }
        try {
        button_click = MediaPlayer.create(this, R.raw.button_click);
        button_click.start();
        switch(view.getId()){
        case R.id.button5: //main
            break;
        case R.id.button1: //answer_1
            if(correct(1))
                correct++;


            break;
        case R.id.button2: //answer_2
            if(correct(2))
                correct++;

            break;
        case R.id.button3: //answer_3
            if(correct(3))
                correct++;

            break;
        case R.id.button4: //answer_3
            if(correct(4))
                correct++;


            break;

        default:
            break;


        }
        Game(i1);
        correct++;
        tip.setText("Correct answers "+correct);
        } catch (Exception ex){}
    }


    public float getResults(){
        return (Float)(correct*5)/(questions.length);
    }

    private boolean correct(int button){
        for (int i = 0; i < answers_correct.length; i++){
        if(button == 1 && a1 == answers_correct[i]
            || button == 2 && a2 == answers_correct[i]
            || button == 3 && a3 == answers_correct[i]
            || button == 4 && a4 == answers_correct[i])
            return true;
        }
        return false;
    }

    private void Game(int q){
        try {
        if(i1 >= questions.length) { //no more questions
            startActivity(new Intent("com.aleksei.etb.END"));
            alive = false;
            return;
        }

        main.setText("Next");
        String answer_list[][] = {
                {answers_correct[i1], answers_wrong[q][0] , answers_wrong[q][1] , answers_wrong[q][2]},
                {answers_correct[i1], answers_wrong[q][0] , answers_wrong[q][1] , answers_wrong[q][2]},
                {answers_correct[i1], answers_wrong[q][0] , answers_wrong[q][1] , answers_wrong[q][2]},
                {answers_correct[i1], answers_wrong[q][0] , answers_wrong[q][1] , answers_wrong[q][2]},
                {answers_correct[i1], answers_wrong[q][0] , answers_wrong[q][1] , answers_wrong[q][2]}
        };

        Collections.shuffle(Arrays.asList(answer_list[q]));
        answer_1.setText(answer_list[q][0]);
        answer_2.setText(answer_list[q][1]);
        answer_3.setText(answer_list[q][2]);
        answer_4.setText(answer_list[q][3]);
        a1 = answer_list[q][0];
        a2 = answer_list[q][1];
        a3 = answer_list[q][2];
        a4 = answer_list[q][3];
        q_textview.setText(questions[q]);
        /*questions = question_list.toArray(new String[question_list.size()]);
        answers_correct = answer_list_correct.toArray(new String[answer_list_correct.size()]);
        question.setText(questions[i1]);        

        answer_list_correct.remove(questions[i1]);
        question_list.remove(questions[i1]);*/

        } catch(Exception e){
            System.out.println(e);
        }
        i1++;
    }
    private void getData(){
        //Getting the data
        main = (Button) findViewById(R.id.button5);
        answer_1 = (Button) findViewById(R.id.button1);
        answer_2 = (Button) findViewById(R.id.button2);
        answer_3 = (Button) findViewById(R.id.button3);
        answer_4 = (Button) findViewById(R.id.button4);
        q_textview = (TextView) findViewById(R.id.question);
        tip = (TextView) findViewById(R.id.answ1);

        main.setOnClickListener(this);
        answer_1.setOnClickListener(this);
        answer_2.setOnClickListener(this);
        answer_3.setOnClickListener(this);
        answer_4.setOnClickListener(this);

        //Resets the text
        //Note to self: Replace with another ContectView
        main.setText("Begin!");
        answer_4.setText("");
        answer_3.setText("");
        answer_2.setText("");
        answer_1.setText("");
        tip.setText("");

    /*  for(String x : questions) {
            for(String y : answers_correct){

            answer_list_correct.add(y);
            question_list.add(x);

            Collections.shuffle(answer_list_correct);
            Collections.shuffle(question_list);

            }
        } */



    }


    }

另一个班级

       public class End extends Activity implements RatingBar.OnRatingBarChangeListener {
    ETBetaActivity classy = new ETBetaActivity();

    TextView score;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.end);
        score = (TextView) findViewById(R.id.score);
        results();
        final RatingBar yourRating = (RatingBar) findViewById(R.id.ratingBar1);
        yourRating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener(){

            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating,
              boolean fromUser) {
             // TODO Auto-generated method stub
              //  yourRating.setRating(rating);
                yourRating.setRating(classy.getResults());
              /*  Toast.makeText(End.this, "M:"+String.valueOf(rating),
               Toast.LENGTH_LONG).show(); */
                Toast.makeText(End.this, "M "+classy.getResults(), Toast.LENGTH_LONG).show();
            }});
     //movieImage.setImageResource(R.drawable.icon);       
 }


    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating,
            boolean fromUser) {
        // TODO Auto-generated method stub

    }

    public void results(){
        score.setText("Your mark  "+classy.getResults());
    }

    }

4 个答案:

答案 0 :(得分:1)

开始结束活动时,你应该这样做:

Intent end = new Intent("com.aleksei.etb.END");
end.putExtra("results", getResults());
startActivity(end);

然后从End-activity中删除classy-field,而不是在onCreate()方法中读取结果,如:

int results = getIntExtra("results");

这将正确地将结果从一个活动转移到下一个活动。

答案 1 :(得分:0)

我会确保你在switch语句中实际捕获这些情况。你可能实际上没有输入这些案件。

因此,如果您未输入这些案例correct将保持0

答案 2 :(得分:0)

我认为你可能正在进行整数除法,所以如果问题的大小很大,那么浮点除法就是0.something因此整数除法是0.尝试将正确的方法转换为浮点数而你将获得一个十进制数

答案 3 :(得分:0)

以下是如何快速找出问题的答案:

打开

    public int correct = 0;

进入财产

    private int _correct = 0;
    public int correct
    {
        get { return _correct; }
        set { _correct = value; }
    }

并在setter上设置断点。然后你会看到是否有东西将它设置回零。如果在增加之后没有任何东西将其设置为零,则可能在某个地方你实例化对象的新实例而你没有意识到你没有使用默认值的新实例。 / p>

我不认为这是一个整数除法问题,这里有一个注释:

你乘以* 5,然后除以5(因为有5个答案)。如果答案的数量在某个时刻会发生变化,但5的乘数将保持不变,那么这是有意义的。但是否则我不知道你为什么要这样做,因为5/5 = 1,将某些东西乘以一点毫无意义。