不知道为什么我在线程 "main" java.lang.ArrayIndexOutOfBoundsException 中出现此错误异常:0
Dont know why I have this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
这是我的代码
import java.util.Scanner;
public class Input {
static class General_Inputs{
static int Num_Of_Ppes;
static int Num_Of_States;
static int Num_Of_Analysis_Years;
static int Number_Decision_Variables;
static int Num_objectives;
static int Num_Constraints;
static int[] Num_Alt_Decision_variable=new int[Number_Decision_Variables];
}
public static double[][] Get_Inputs(){
Scanner State_Vector=new Scanner(System.in);
System.out.println("Enter the number of Decision_Variables");
General_Inputs.Number_Decision_Variables=State_Vector.nextInt();
for(int Num=0;Num<General_Inputs.Number_Decision_Variables;Num++){
System.out.println("Enter the number of Alternatives for Decision Variable "+(Num+1));
General_Inputs.Num_Alt_Decision_variable[Num]=State_Vector.nextInt();
}
State_Vector.close();
return Current_Cond_State_Cof_lngth;
}
public static void main(String args[]){
double[][] input=Input.Get_Inputs();
输入是这样的:
输入Decision_Variables个数
3个
输入决策变量 1 的备选方案数量
2个
这应该进行三次,但它在第一个决策变量处停止并给出以下错误我不确定问题出在哪里。我知道这个错误意味着数组 "Num_Alt_Decision_variable" 的大小为零但是为什么我在上面定义了这个数组,我真的尝试了很多搜索但找不到原因 非常感谢任何帮助提前感谢.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Input.Get_Inputs(Input.java:48)
at Input.main(Input.java:66)
static int Number_Decision_Variables; // this is 0 here
static int Num_objectives;
static int Num_Constraints;
// and it's still 0 here.
static int[] Num_Alt_Decision_variable=new int[Number_Decision_Variables];
Number_Decision_Variables 在您声明 Num_Alt_Decision_variable 时为 0。所以你的数组大小为 0,这是一个不可用的数组。我建议你用一个非0的数字来初始化它。
在这一行之后初始化你的数组:
General_Inputs.Number_Decision_Variables=State_Vector.nextInt();
获得 Number_Decision_Variables 变量的实际值后。
顺便说一句,您会想要学习和使用 Java naming conventions。变量名称应全部以小写字母开头,而 class 名称应以大写字母开头。
这是我能解决的答案
import java.util.Scanner;
public class Input {
static class General_Inputs{
static int Num_Of_Ppes;
static int Num_Of_States;
static int Num_Of_Analysis_Years;
static int Number_Decision_Variables;
static int Num_objectives;
static int Num_Constraints;
static int[] Num_Alt_Decision_variable;
}
public static double[][] Get_Inputs(){
Scanner State_Vector=new Scanner(System.in);
System.out.println("Enter the number of Decision_Variables");
General_Inputs.Number_Decision_Variables=State_Vector.nextInt();
General_Inputs.Num_Alt_Decision_variable=new int[General_Inputs.Number_Decision_Variables]
for(int Num=0;Num<General_Inputs.Number_Decision_Variables;Num++){
System.out.println("Enter the number of Alternatives for Decision Variable "+(Num+1));
General_Inputs.Num_Alt_Decision_variable[Num]=State_Vector.nextInt();
}
State_Vector.close();
return Current_Cond_State_Cof_lngth;
}
public static void main(String args[]){
double[][] input=Input.Get_Inputs();
这是我的代码
import java.util.Scanner;
public class Input {
static class General_Inputs{
static int Num_Of_Ppes;
static int Num_Of_States;
static int Num_Of_Analysis_Years;
static int Number_Decision_Variables;
static int Num_objectives;
static int Num_Constraints;
static int[] Num_Alt_Decision_variable=new int[Number_Decision_Variables];
}
public static double[][] Get_Inputs(){
Scanner State_Vector=new Scanner(System.in);
System.out.println("Enter the number of Decision_Variables");
General_Inputs.Number_Decision_Variables=State_Vector.nextInt();
for(int Num=0;Num<General_Inputs.Number_Decision_Variables;Num++){
System.out.println("Enter the number of Alternatives for Decision Variable "+(Num+1));
General_Inputs.Num_Alt_Decision_variable[Num]=State_Vector.nextInt();
}
State_Vector.close();
return Current_Cond_State_Cof_lngth;
}
public static void main(String args[]){
double[][] input=Input.Get_Inputs();
输入是这样的: 输入Decision_Variables个数 3个 输入决策变量 1 的备选方案数量 2个 这应该进行三次,但它在第一个决策变量处停止并给出以下错误我不确定问题出在哪里。我知道这个错误意味着数组 "Num_Alt_Decision_variable" 的大小为零但是为什么我在上面定义了这个数组,我真的尝试了很多搜索但找不到原因 非常感谢任何帮助提前感谢.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Input.Get_Inputs(Input.java:48)
at Input.main(Input.java:66)
static int Number_Decision_Variables; // this is 0 here
static int Num_objectives;
static int Num_Constraints;
// and it's still 0 here.
static int[] Num_Alt_Decision_variable=new int[Number_Decision_Variables];
Number_Decision_Variables 在您声明 Num_Alt_Decision_variable 时为 0。所以你的数组大小为 0,这是一个不可用的数组。我建议你用一个非0的数字来初始化它。
在这一行之后初始化你的数组:
General_Inputs.Number_Decision_Variables=State_Vector.nextInt();
获得 Number_Decision_Variables 变量的实际值后。
顺便说一句,您会想要学习和使用 Java naming conventions。变量名称应全部以小写字母开头,而 class 名称应以大写字母开头。
这是我能解决的答案
import java.util.Scanner;
public class Input {
static class General_Inputs{
static int Num_Of_Ppes;
static int Num_Of_States;
static int Num_Of_Analysis_Years;
static int Number_Decision_Variables;
static int Num_objectives;
static int Num_Constraints;
static int[] Num_Alt_Decision_variable;
}
public static double[][] Get_Inputs(){
Scanner State_Vector=new Scanner(System.in);
System.out.println("Enter the number of Decision_Variables");
General_Inputs.Number_Decision_Variables=State_Vector.nextInt();
General_Inputs.Num_Alt_Decision_variable=new int[General_Inputs.Number_Decision_Variables]
for(int Num=0;Num<General_Inputs.Number_Decision_Variables;Num++){
System.out.println("Enter the number of Alternatives for Decision Variable "+(Num+1));
General_Inputs.Num_Alt_Decision_variable[Num]=State_Vector.nextInt();
}
State_Vector.close();
return Current_Cond_State_Cof_lngth;
}
public static void main(String args[]){
double[][] input=Input.Get_Inputs();