2016-10-18 57 views
0

我有一個隨機數(loadG1),在用戶按下EditText上的輸入後輸出。我在計時器末尾添加了一些if語句,以確保根據用戶的分數輸出一定數量的特定數字長度。每次他們的輸入與loadG1匹配時,他們會得到一個點。我的問題是,有時在輸出的3分(> = 0)之前的隨機數中,我得到一個六位數的數字。這僅僅意味着當他們有3個或更多分時纔會發生。這也發生在其他階段,例如:意圖由8輸出的7個數字代替。該模式似乎幾乎完美,但有時會爲該用戶的分數輸出超過限制的一位數字。不要少一個數字,但要麼是正確的數量要麼有時多一個。隨機數發生器問題 - Android Studio

下面的代碼:

final Random generateG1 = new Random(); 
      final int loadG1 = generateG1.nextInt(99999)+10000; 
      final TextView number = (TextView) findViewById(R.id.number); 
      number.setText(""+loadG1); 

      final CountDownTimer loop = new CountDownTimer(3500, 1000) { 
       @Override 
       public void onTick(long millisUntilFinished) { 
       } 
       @Override 
       public void onFinish() { 
        number.setVisibility(View.GONE); 
        final TextView prompt = (TextView) findViewById(R.id.prompt); 
        prompt.setVisibility(View.VISIBLE); 
        prompt.setText(" Enter the number"); 
        final EditText input = (EditText) findViewById(R.id.enterAnswer); 
        input.setVisibility(View.VISIBLE); 
        input.setOnKeyListener(new View.OnKeyListener() { 
         @Override 
         public boolean onKey(View v, int keyCode, KeyEvent event) { 
          if (event.getAction() == KeyEvent.ACTION_DOWN) { 
           switch (keyCode) { 
            case KeyEvent.KEYCODE_ENTER: 
             Editable answer = input.getText(); 
             int finalAnswer = Integer.parseInt(String.valueOf(answer)); 
             int finalLoadG1 = Integer.parseInt(String.valueOf(number.getText())); 
             input.setVisibility(View.GONE); 
             prompt.setVisibility(View.GONE); 
             if (finalAnswer == finalLoadG1) { 
              score++; 
             } 

             number.setVisibility(View.VISIBLE); 

             if (score>=0) { 
              int loadG1 = generateG1.nextInt(99999)+10000; 
              number.setText(""+loadG1); 
             } 
             if (score>=3){ 
              int loadG1 = generateG1.nextInt(999999)+100000; 
              number.setText(""+loadG1); 
             } 
             if (score>=6){ 
              int loadG1 = generateG1.nextInt(9999999)+1000000; 
              number.setText(""+loadG1); 
             } 
             if (score>=10){ 
              int loadG1 = generateG1.nextInt(99999999)+10000000; 
              number.setText(""+loadG1); 
             } 

             input.getText().clear(); 

             start(); 

將不勝感激,如果有人能告訴我是什麼導致了這個錯誤,以及如何解決它。提前致謝。

回答

0

您需要更改條件。

  1. 無論是反向的if語句的順序,或

  2. 添加<條件也if語句。

並利用別人也。

 if (score>=10){ 
      int loadG1 = generateG1.nextInt(99999999)+10000000; 
      number.setText(""+loadG1); 
     } 
     else if (score>=6){ 
      int loadG1 = generateG1.nextInt(9999999)+1000000; 
      number.setText(""+loadG1); 
     } 
     else if (score>=3){ 
      int loadG1 = generateG1.nextInt(999999)+100000; 
      number.setText(""+loadG1); 
     }else if (score>=0) { 
      int loadG1 = generateG1.nextInt(99999)+10000; 
      number.setText(""+loadG1); 
     } 

if (score>=0 && score<3) { 
      int loadG1 = generateG1.nextInt(99999)+10000; 
      number.setText(""+loadG1); 
     } 
     else if (score>=3 && score<6){ 
      int loadG1 = generateG1.nextInt(999999)+100000; 
      number.setText(""+loadG1); 
     } 
     else if (score>=6 && score<10){ 
      int loadG1 = generateG1.nextInt(9999999)+1000000; 
      number.setText(""+loadG1); 
     } 
     else if (score>=10){ 
      int loadG1 = generateG1.nextInt(99999999)+10000000; 
      number.setText(""+loadG1); 
     }