java - 运行代码时出错(数组问题)?

标签 java arrays

我的代码中有关数组的问题一直存在。

我收到一连串的错误:

线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 1 在 Ass1.genTimeArray(Ass1.java:150) 在 Ass1.main(Ass1.java:54)

这是我的代码:

import java.util.* ;
public class Ass1 {


 static int VIX = 0 ; // Traversing Voltage Component in Component Value Array 
 static int CIX = 1 ; // Traversing Capacitor
 static int LIX = 2; // Traversing Inductor
 static int RIX = 3; // Traversing Resistor

 public static void main (String args[]) { 
  System.out.println(

  double[] compArr  = getCircuitComp() ; 

  System.out.println("-----------------------------------------------------") ;
  System.out.println(""); 
  System.out.println("Voltage Source V = " + compArr[VIX]+ "volts") ; 
  System.out.println("Capacitor C = " + compArr[CIX]+ "farads");
  System.out.println("Inductor L = " + compArr[LIX] + " henrys") ; 
  System.out.println("Resistor R =" + compArr[RIX] + "ohms") ; 
  System.out.println("-----------------------------------------------------") ; 

  boolean outerLoopFlag ; 
  boolean innerLoopFlag ; 
  int i ; 
  int n = 1  ; 
  int qArray[] = new int[n] ; 
  int timeArr[] = new int [n] ;
  System.out.println("-----------------------------------------------------") ;
  Scanner input = new Scanner (System.in) ;
  while ( outerLoopFlag = true) { 

   System.out.println("Reset Component Values (y/n) ?") ; 
   String answer = input.nextLine() ; 
   char ans = answer.charAt(0) ;  
   if ( ans == 'y') { 
     getCircuitComp() ; 

   } 
   else { 
     } 
   innerLoopFlag = true ; 
   while ( innerLoopFlag = true){ 
    System.out.print("Run a simulation?") ; 
      String answer2 = input.nextLine() ; 
   char ans2 = answer2.charAt(0) ;  
    if (ans2 == 'y'){

    System.out.println("Enter a maximum time :") ; 
    double tMax = input.nextDouble(); 
    System.out.println("Enter a time step:") ; 
    double tStep = input.nextDouble();
       timeArr[n]= genTimeArray(tMax,tStep,n);  
       qArray[n] = genQArray( timeArr , n , compArr) ;  
       displayQFunction(timeArr,qArray,n );

    }
    else{
     innerLoopFlag = false ;
    }
    System.out.println("-----------------------------------------------------") ; 

    while (innerLoopFlag = false) { 
     System.out.println("Do you want to quit (y/n)?") ;
     String answer3 = input.nextLine() ; 
     char ans3 = answer3.charAt(0) ; 
     if ( ans3 =='y' ){ 
      System.exit(0); 
      } 
     else{}

    }


   } 



 }

}
     ////////////////////////////
 ///*getCircuitComp Method // 

 public static double[] getCircuitComp() {
     Scanner input = new Scanner (System.in) ; 
  boolean flag = true ; 
  double compArr [] = new double[4]; 
     do{ 
   System.out.println("Enter a value V ( 4 to 15):") ; 
   compArr[VIX] = input.nextDouble() ;  
   if ((compArr[VIX] >= 4.0) && (compArr[VIX] <= 15.0)){ 
    flag = false ; 
   }
   else{ 
    System.out.println("Bad Value" + compArr[VIX] ) ;
   }

  }  while (  flag );
  flag = true  ; 
  while (flag) { 
   System.out.println("Enter a value for R ( 5 to 10) :") ; 
   compArr[RIX] = input.nextDouble();  
   if((compArr[RIX] >= 5.0) && (compArr[RIX] <= 10.0)){
flag = false;
   }
   else{
    System.out.println("Bad Value:"+ compArr[RIX]) ; 

   }

  }
   flag = true ; 
   while(flag) { 
    System.out.println("Enter a value for C( 1e-9 to 1e-7 ): ") ; 
    compArr[CIX] = input.nextDouble();  
    if (( compArr[CIX] >= Math.pow(10, -9)) && (compArr[CIX] <= Math.pow(10, -7))){
     flag = false ; 
    }
    else{ 
     System.out.println("Bad Value :"+ compArr[CIX]);
    }


   }
    flag = true ; 
    while(flag){ 
     System.out.println("Enter a value for L(1e-3 to 1e-1) "); 
     compArr[LIX] = input.nextDouble(); 
     if(compArr[LIX]>= Math.pow(10,-3) && compArr[LIX] <= Math.pow(10, -1)){
      flag = false ; 
     }
     else{ 
      System.out.println("Bad Value :" + compArr[LIX]) ; 

     }
    }
    return compArr  ; 
   }


////////////////////////////
///*getTimeArray Method //  
 public static int genTimeArray( double tMax , double tStep , int n ) { 

   int t[] = new int[n] ;
    n = 1 ; 
    t[0] = 0 ; 
    if(t[n] < tMax){
    t[n] = (int) (t[n-1] + tStep) ; 
    n = n + 1 ; 
    }
   return t[n] ;

  } 
  public static int genQArray( int timeArr[] , int n , double compArr [] ) {
   double q[] = new double[n] ; 
   n = 1 ;  
   double s = (compArr[VIX])*(compArr[CIX]) ; 
   double t = Math.exp((-compArr[RIX])/(2*compArr[LIX])); 
   double c = (1)/((compArr[LIX])*(compArr[CIX])) ; 
   double v = Math.pow((compArr[RIX])/(2*(compArr[LIX])),2) ;
   double r = (timeArr[n])*(Math.sqrt(c-v)) ;
   q[n] = (s)*(t)*(Math.cos(r)) ; 

   return (int) q[n] ; 
  }
////////////////////////////
///*displayQFunction Method // 
  public static void  displayQFunction(int[] timeArr , int[] qArray ,int n  ){ 

  n = 1 ;
  System.out.println( timeArr[n]  + qArray[n]) ; 




 }
 }

谁能帮忙,我在处理这段代码时遇到了很多麻烦!

谢谢

最佳答案

java.lang.ArrayIndexOutOfBoundsException: 1

这表明您的数组长度少于 2 个元素。

您正在请求索引 1(第二个元素),而 Java 会说,“等一下牛仔,这里的数组不够长。”

看起来您需要在畜栏中放置更多奶牛(即,填充您的数组)。 :)


仔细一看,问题似乎就从这里开始

boolean outerLoopFlag ; 
boolean innerLoopFlag ; 
int i ; 
int n = 1  ;    //  <- Here's a problem
int qArray[] = new int[n] ; 
int timeArr[] = new int [n] ;  // <- because your array is now only 1 element long

继续往下

double tStep = input.nextDouble();
timeArr[n]= genTimeArray(tMax,tStep,n);    // <- n is still 1

然后继续到错误发生的地方:

public static int genTimeArray( double tMax , double tStep , int n ) { 
    int t[] = new int[n] ;

关于java - 运行代码时出错(数组问题)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13319967/

相关文章:

c - 结构数组替换自身的值

java - 如何以编程方式获取 JBoss 中所有注册 DS 名称(对象)的列表?

java - 从对象的 ArrayList 创建 JTable - Java

c++ - 将第二个数组的元素添加到第一个数组的末尾

php - 计算不同字符串的数量?

c# - P/Invoke [In, Out] 属性对于编码(marshal)处理数组是可选的吗?

返回 List 和整数的 Java 方法

java - PolyUtil 解码 JavaScript 和 Java 的区别

java - 如何配置从maven在GWT项目中运行

创建大型二维数组