这是我的代码
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”的大小为零,但为什么我在上面定义了这个数组,我真的试图搜索很多但找不到原因任何帮助都非常感谢提前感谢。< / p>
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Input.Get_Inputs(Input.java:48)
at Input.main(Input.java:66)
答案 0 :(得分:1)
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];
在声明Num_Alt_Decision_variable时,Number_Decision_Variables为0。所以你的数组大小为0,这是一个不可用的数组。我建议你用非0号码初始化它。
在这行之后>>初始化数组:
General_Inputs.Number_Decision_Variables=State_Vector.nextInt();
获得Number_Decision_Variables变量的实际值后。
另外,您需要学习并使用Java naming conventions。变量名都应以较低的字母开头,而类名以大写字母开头。
答案 1 :(得分: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;
}
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();