倒数计时器,积分和不正确的乘法

时间:2016-05-16 12:14:30

标签: java android

我将此代码作为方法:

String resultTimeString = ct.toString()
    if(resultTimeString.length() == 2 ){
        resultTime1ASCII = resultTimeString.charAt(0);
        resultTime2ASCII = resultTimeString.charAt(1);
        resultTime1 = (int)resultTime1ASCII - 48;
        resultTime2 = (int)resultTime2ASCII - 48;
        resultTime = resultTime1 + resultTime2;
    }
    else{
        resultTime1ASCII = resultTimeString.charAt(0);
        resultTime1 = (int)resultTime1ASCII - 48;
        resultTime = resultTime1;
    }
    punkty = punkty * resultTime;
    //Globals.setScore(punkty);
    ct.cancel();

问题在于计算。最终得分(“punkty”)不是多重punkty和resultTime,我不知道为什么。变量 punkty 被定义为给出一个好答案的分数。

计时器从60减少到0。

2 个答案:

答案 0 :(得分:0)

你说最终得分总是为0。

唯一可能发生这种情况的方法是将punkty初始化为0。

每次,它乘以0,并保持为0.

您应该将punkty初始化为1。那么你的代码就可以了。

答案 1 :(得分:0)

@Hackerdarshi,也许我会告诉你所有代码:

public class QuestionActivity extends Activity {
    private static final String TAG = "suemar";
    int position = 0;
    Button buttonA;
    Button buttonB;
    Button buttonC;
    Button buttonD;
    TextView textView;
    TextView count;
    Retrofit retrofit;
    QuestionService questionService;
    Call<pytania> QACall;
    pytania questionsAnswers;
    int licz = 0, punkty = 0;
    String id;
    char resultTime1ASCII,resultTime2ASCII;
    int resultTime=0, resultTime1=0, resultTime2=0;
    CountDownTimer ct;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);

        buttonA = (Button) findViewById(R.id.buttonA);
        buttonB = (Button) findViewById(R.id.buttonB);
        buttonC = (Button) findViewById(R.id.buttonC);
        buttonD = (Button) findViewById(R.id.buttonD);
        textView = (TextView) findViewById(R.id.textView_pytanie);
        count = (TextView) findViewById(R.id.countText);

        buttonA.setBackgroundResource(R.drawable.button_game);
        buttonB.setBackgroundResource(R.drawable.button_game);
        buttonC.setBackgroundResource(R.drawable.button_game);
        buttonD.setBackgroundResource(R.drawable.button_game);

        ct =  new CountDownTimer(60000, 1000) {

            public void onTick(long millisUntilFinished) {


                String v = String.format("%02d", millisUntilFinished/60000);
                int va = (int)( (millisUntilFinished%60000)/1000);
                count.setText(String.format("%02d", va));
            }

            public void onFinish() {
                count.setText("0");
                koniec();
            }
        };
        ct.start();


         Intent intent = getIntent();
         position = intent.getExtras().getInt("position");
         position++;
         id = Integer.toString(position);

        //Retrofit magic part
        retrofit = new Retrofit.Builder()
                .baseUrl("http://46.101.128.24/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        questionService = retrofit.create(QuestionService.class);

        QACall = questionService.getQuestionsAnswers(id);

        QACall.enqueue(new Callback<pytania>() {
            @Override
            public void onResponse(Call<pytania> call, Response<pytania> response) {
                if (response.isSuccessful()) {
                    questionsAnswers = response.body();

                    // textView.setText(Integer.toString(questionsAnswers.success));
                    giveQuestions();


                    for (Questions c : questionsAnswers.Questions) {
                        Log.i(TAG, String.format("%s: %s", c.question, c.answer1));


                        Log.i(TAG, "---------");
                    }


                } else {
                    Toast.makeText(QuestionActivity.this, "LOL2", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<pytania> call, Throwable t) {
                Log.d("Coś się zepsuło", t.getMessage());
                Toast.makeText(QuestionActivity.this, "LOL", Toast.LENGTH_SHORT).show();
            }
        });


    }


    public void onAnswer(View view) {
        licz++;

        buttonA.setBackgroundResource(R.drawable.button_game);
        buttonB.setBackgroundResource(R.drawable.button_game);
        buttonC.setBackgroundResource(R.drawable.button_game);
        buttonD.setBackgroundResource(R.drawable.button_game);


        switch (view.getId()) {
            case R.id.buttonA:
                buttonA.setBackgroundResource(R.drawable.button_game_click);

                if (buttonA.getText() == questionsAnswers.Questions.get(licz - 1).answer1) {
                    Toast.makeText(QuestionActivity.this, "Pan to umie ale tego nie rozumie :D", Toast.LENGTH_SHORT).show();
                    punkty++;
                } else {
                    Toast.makeText(QuestionActivity.this, "...Bania.", Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.buttonB:
                buttonB.setBackgroundResource(R.drawable.button_game_click);
                if (buttonB.getText() == questionsAnswers.Questions.get(licz - 1).answer1) {
                    Toast.makeText(QuestionActivity.this, "Pan to umie ale tego nie rozumie :D", Toast.LENGTH_SHORT).show();
                    punkty++;
                } else {
                    Toast.makeText(QuestionActivity.this, "...Bania.", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.buttonC:
                buttonC.setBackgroundResource(R.drawable.button_game_click);
                if (buttonC.getText() == questionsAnswers.Questions.get(licz - 1).answer1) {
                    Toast.makeText(QuestionActivity.this, "Pan to umie ale tego nie rozumie :D", Toast.LENGTH_SHORT).show();
                    punkty++;
                } else {
                    Toast.makeText(QuestionActivity.this, "...Bania.", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.buttonD:
                buttonD.setBackgroundResource(R.drawable.button_game_click);

                if (buttonD.getText() == questionsAnswers.Questions.get(licz - 1).answer1) {
                    Toast.makeText(QuestionActivity.this, "Pan to umie ale tego nie rozumie :D", Toast.LENGTH_SHORT).show();
                    punkty++;
                } else {
                    Toast.makeText(QuestionActivity.this, "...Bania.", Toast.LENGTH_SHORT).show();
                }
                break;


        }
        if (licz == 5) {
            koniec();

        } else {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    buttonA.setBackgroundResource(R.drawable.button_game);
                    buttonB.setBackgroundResource(R.drawable.button_game);
                    buttonC.setBackgroundResource(R.drawable.button_game);
                    buttonD.setBackgroundResource(R.drawable.button_game);
                    giveQuestions();
                }
            };

            Handler h = new Handler();
            h.postDelayed(r, 300);

        }

    }

    private void koniec() {
        String resultTimeString = ct.toString();
        //resultTime1ASCII = resultTimeString.charAt(resultTimeString.length() - 2);
        //resultTime2ASCII = resultTimeString.charAt(resultTimeString.length()-1);

        if(resultTimeString.length() == 2 ){
            resultTime1ASCII = resultTimeString.charAt(0);
            resultTime2ASCII = resultTimeString.charAt(1);
            resultTime1 = (int)resultTime1ASCII - 48;
            resultTime2 = (int)resultTime2ASCII - 48;
            resultTime = (resultTime1*10) + resultTime2;
        }
        else{
            resultTime1ASCII = resultTimeString.charAt(0);
            resultTime1 = (int)resultTime1ASCII - 48;
            resultTime = resultTime1;
        }
        punkty = punkty * resultTime;
        //Globals.setScore(punkty);
        ct.cancel();
        Intent intent = new Intent(QuestionActivity.this, YourResultActivity.class);
        startActivity(intent);
        finish();
    }

    public void giveQuestions() {
        questionsAnswers.Questions.get(licz).ShuffleAnswers();
        textView.setText(questionsAnswers.Questions.get(licz).question);
        buttonA.setText(questionsAnswers.Questions.get(licz).getAnswer(0));
        buttonB.setText(questionsAnswers.Questions.get(licz).getAnswer(1));
        buttonC.setText(questionsAnswers.Questions.get(licz).getAnswer(2));
        buttonD.setText(questionsAnswers.Questions.get(licz).getAnswer(3));
    }


}