【发布时间】:2015-09-16 13:10:53
【问题描述】:
我的问题是我在调试过程中不断收到标题中的错误。
array[i] = java.lang.IndexOutOfBoundsException : Invalid array range 10 to 10
下面是for循环。我输入了值people = 10, payer = 4, rest = 6。
int[] array = new int[people];
Random rand = new Random();
int rest = people-payer;
for(int i=0; i<people; i++) {
if(payer<=0) { /*Payers are already decided*/
array[i] = 0;
continue;
}
if(rest<=0) {
array[i] = 1;
continue;
}
else {
int randomNumber = rand.nextInt(2);
array[i] = randomNumber;
if (randomNumber == 1)
payer--;
if (randomNumber == 0)
rest--;
}
}
我认为错误是说我正在尝试访问不存在的数组的索引 10。我不知道我在哪里访问数组 [10]。或者问题可能出在其他地方??
感谢您的宝贵时间:)
+加法
这是完整的代码,但它很长……我不知道它是否有帮助。 for 循环在 onClick 方法中。
package activities;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import com.example.chloe.myapplication.R;
import java.util.Random;
public class PayActivity extends ActionBarActivity implements View.OnClickListener {
Button submitButton, flip;
Switch switchButton;
EditText et_totalPeople, et_payPeople, et_amount;
TextView tv_payPeople, tv_people;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pay_numofpeople);
submitButton = (Button) findViewById(R.id.submitButton);
switchButton = (Switch)findViewById(R.id.switchButton);
et_totalPeople= (EditText) findViewById(R.id.et_totalPeople);
et_payPeople= (EditText) findViewById(R.id.et_payPeople);
et_amount = (EditText) findViewById(R.id.et_amount);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true) {
/*After entering a value in the first editText and pressing enter,
* the qwerty keyboard comes up and won't move to third editText even though I push enter...SOLVE */
et_payPeople.setEnabled(false);
et_payPeople.setClickable(false);
} else {
et_payPeople.setEnabled(true);
et_payPeople.setClickable(true);
}
}
});
et_totalPeople.setSelectAllOnFocus(true);
et_payPeople.setSelectAllOnFocus(true);
et_amount.setSelectAllOnFocus(true);
submitButton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.submitButton:
int people = Integer.parseInt(et_totalPeople.getText().toString());
if(people<1 || people>100) {
Toast.makeText(getApplicationContext(), "Enter values 1~100", Toast.LENGTH_LONG).show();
break;
}
int amount = Integer.parseInt(et_amount.getText().toString());
if(amount<1 || amount>10000000) {
Toast.makeText(getApplicationContext(), "Enter values 1~10,000,000", Toast.LENGTH_LONG).show();
break;
}
/*Only certain people pay*/
if(switchButton.isChecked()==false) {
int payer = Integer.parseInt(et_payPeople.getText().toString());
if(payer<1 || payer>100) {
Toast.makeText(getApplicationContext(), "Enter values 1~100", Toast.LENGTH_LONG).show();
break;
} /*All three values are valid: SHOW CARDS*/
else {
int[] array = new int[people];
Random rand = new Random();
int rest = people-payer;
for(int i=0; i<people; ++i) {
/*Payers are already decided*/
if(payer<=0) {
array[i] = 0;
continue;
}
if(rest<=0) {
array[i] = 1;
continue;
}
else {
int randomNumber = rand.nextInt(2);
array[i] = randomNumber;
if (randomNumber == 1)
payer--;
if (randomNumber == 0)
rest--;
}
}
AlertDialog.Builder dialog = new AlertDialog.Builder(PayActivity.this);
dialog.setView(R.layout.card_questionmark);
for(int i=0; i<people; i++) {
/*PASS*/
if(array[i] == 0) {
dialog.setView(R.layout.card_questionmark);
dialog.setPositiveButton("FLIP", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new AlertDialog.Builder(PayActivity.this)
.setView(R.layout.card_pass)
.setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish(); /*move on to next value in array*/
}
});
}
});
/*If not the first card, show previous card*/
if(i!=0) {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
i--; /*return to previous value in array*/
} /*First card*/
else {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "No previous card", Toast.LENGTH_LONG).show();
}
});
}
}/*YOU PAY*/
else {
dialog.setView(R.layout.card_questionmark);
dialog.setPositiveButton("FLIP", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new AlertDialog.Builder(PayActivity.this)
.setView(R.layout.card_youpay)
.setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish(); /*move on to next value in array*/
}
});
}
});
/*If not the first card, show previous card*/
if(i!=0) {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
i--; /*return to previous value in array*/
} /*First card*/
else {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "No previous card", Toast.LENGTH_LONG).show();
}
});
}
}
}
}
} /*Everyone Pays*/
else {
}
break;
}
}
}
【问题讨论】:
-
你能发布你声明数组的代码部分吗?
-
sry 忘了那部分 - 添加它!
-
您在调试期间是否仅收到此异常?您显示的代码对我来说似乎工作得很好。
-
日志中有行号吗?
-
显示分配
people和payer变量的位置。
标签: java android arrays random indexoutofboundsexception