2016-03-19 66 views
-1

我正在嘗試創建一個報價生成器應用程序,並且該應用程序的關鍵部分是random()。但由於某些原因,隨機在我的MainActivity中,應用程序將無法編譯。爲什麼random.nextInt()創建「IllegalArgumentException:n <= 0:-1」?

package com.example.alpak.liftquote; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.TextView; 

import java.util.Random; 

public class MainActivity extends AppCompatActivity { 

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



     // WHERE MY CODE STARTS 

     //RANDOM GENERATOR that DOESN'T WORK 

     Random rand = new Random(); 

     final int irandomIndex = rand.nextInt((3 - 1) + 1)+1; 
     final int drandomIndex = rand.nextInt((4 - 6) + 1)+4; 
     final int prandomIndex = rand.nextInt((7 - 9) + 1)+7; 

     final String iIndex = "s"+irandomIndex; 
     final String dIndex = "s"+drandomIndex; 
     final String pIndex = "s"+prandomIndex; 

     final String RiIndex = getString(getApplicationContext().getResources().getIdentifier(iIndex, "string", getPackageName())); 
     final String RdIndex = getString(getApplicationContext().getResources().getIdentifier(dIndex, "string", getPackageName())); 
     final String RpIndex = getString(getApplicationContext().getResources().getIdentifier(pIndex, "string", getPackageName())); 


     //STRING 

     final TextView txt = (TextView) findViewById(R.id.textView); 

     //BUTTONS 
     // 

     Button inspireBtn = (Button) findViewById(R.id.iButton); 
     Button deepBtn = (Button) findViewById(R.id.dButton); 
     Button positiveBtn = (Button) findViewById(R.id.pButton); 

     // ON CLICK LISTENRS 


     inspireBtn.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v){ 
       txt.setText(RiIndex); 
               //"cannot resolve symbol 'index';" 
      } 
     }); 

,我回來是錯誤消息:

所致:java.lang.IllegalArgumentException異常:N < = 0:-1

+0

粘貼完整的堆棧跟蹤請 – fge

+0

你想達到什麼目的?給nextInt賦予負值拋出異常... –

+0

@Sanruza當你使用'rand.nextInt((3 - 1)+ 1)+1時,你打算產生的數字範圍是多少? – user3437460

回答

3

您的界限必須爲正

檢查Random#nextInt的Javadoc

拋出:IllegalArgumentException - 如果綁定是不是正

以下兩個行會拋出異常。

final int drandomIndex = rand.nextInt((4 - 6) + 1)+4; 
final int prandomIndex = rand.nextInt((7 - 9) + 1)+7; 
0

您的代碼引起負INT

final int drandomIndex = rand.nextInt((4 - 6) + 1)+4; 
    final int prandomIndex = rand.nextInt((7 - 9) + 1)+7; 

4-6和7-9得到負最大值

0

當使用隨機,參數必須是積極的,否則它會拋出異常:

拋出:如果綁定是不是正拋出:IllegalArgumentException

您的代碼在中生成負值:

final int irandomIndex = rand.nextInt((3 - 1) + 1) +1; 
final int drandomIndex = rand.nextInt((4 - 6) + 1) +4; 
final int prandomIndex = rand.nextInt((7 - 9) + 1) +7; 

這相當於:

final int irandomIndex = rand.nextInt(-1) +1; //negative value in nextInt() 
final int drandomIndex = rand.nextInt(-1) +4; //negative value in nextInt() 
final int prandomIndex = rand.nextInt(-1) +7; //negative value in nextInt() 

注意rand.nextInt()將被後它添加的數量之前,首先進行評價。因此rand.nextInt(-1) +4;不等於rand.nextInt(3);

0

nextInt(int)從0(含)產生一個數爲n(不包括)。例如,nextInt(3)可能會產生0,1或2.因此,值< = 0只是沒有意義;如果n爲負數,則不能產生數字R,即0 < = R < n。

從你的代碼看來,你似乎試圖在兩個值之間產生一個隨機數。而不是rand.nextInt((7 - 9) + 1)+7,你應該做rand.nextInt(9 - 7) + 7。這將使它像nextInt(2) + 7,這是(0或1)+ 7,它是7或8。如果您還想生成值9,則必須在圓括號內添加一個值。但是在這種情況下,最好傳入10(因此,nextInt(10 - 7) + 7),這樣您就可以習慣下限是包容性的,上限是排他性的 - 這是Java中非常常見的模式。

更一般地,爲了產生在min(含)和max(不含)之間的隨機整數,公式爲nextInt(max - min) + min