处理多个数组并返回值

时间:2022-06-16 09:51:29

Ive been working on a program that requires the user to input values for a circuit and it it calculates the charge at given times. I have made some progress but Ive hit a road block when one of my arrays refused to return values and the reason mentioned in eclipse was :

我一直致力于一个程序,要求用户输入电路的值,并在给定时间计算电荷。我已经取得了一些进展,但当我的一个阵列拒绝返回值时,我遇到了障碍,而eclipse中提到的原因是:

  • *Multiple markers at this line
  • *此行有多个标记

  • The method genQArray(int[], int, double[], int, int, int, int) in the type Ass1 is not applicable for the arguments (int[], int, double, int, int, int, int)
  • 类型Ass1中的方法genQArray(int [],int,double [],int,int,int,int)不适用于参数(int [],int,double,int,int,int,int)

  • The method genQArray(int[], int, double[], int, int, int, int) in the type Ass1 is not applicable for the arguments (int[], int, int)*
  • 类型Ass1中的方法genQArray(int [],int,double [],int,int,int,int)不适用于参数(int [],int,int)*

I honestly don't know what the problem is at this point and want to find a solution to this , so I can continue working on this program . Any help is gladly appreciated.

老实说,我现在还不知道问题是什么,并希望找到解决方案,所以我可以继续研究这个程序。任何帮助都很高兴。

Here is my code , though it is unfinished :

这是我的代码,虽然它未完成:

import java.util.* ;
public class Ass1 {

    static int VIX ; // Traversing Voltage Component in Component Value Array 
    static int CIX ; 
    static int LIX ; 
    static int RIX ; 

    public static void main (String args[]) { 
        double compArr  = getCircuitComp() ; 
        boolean outerLoopFlag ; 
        boolean innerLoopFlag ; 
        int i ; 
        int n ;
        int timeArr[] = new int[n] ; 
        int qArray[] = new int[n] ;
        Scanner input = new Scanner (System.in) ;
        while ( outerLoopFlag = true) { 

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

            } 
            else { 
                    } 
            innerLoopFlag = true ; 
            while ( innerLoopFlag = true){ 
                System.out.print("Run a simulation?") ; 
                String answer2 = input.nextLine() ; 
                char ans2 = answer.charAt(0) ; 
                if (ans2 == 'y'){
                System.out.print("Enter a maximum time :") ; 
                double tMax = input.nextDouble(); 
                System.out.print("Enter a time step:") ; 
                double tStep = input.nextDouble();
                timeArr[n]= genTimeArray(tMax,tStep,n);  
                qArray[n] = genQArray( timeArr , n , compArr,  VIX ,  CIX ,  RIX ,  LIX) ;  
                displayQFunction(timeArr,qArray,n);

                }

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

                }


            }   



    }

}

    public static int getCircuitComp() {
        Scanner input = new Scanner (System.in) ; 
        boolean flag = true ; 
        double compArr [] = new double[4]; 
        while (  flag = true ) { 
            System.out.print("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.print("Bad Value" + compArr[VIX] ) ;
            }

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

            }

        }
            flag = true ; 
            while(flag) { 
                System.out.print("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.print("Bad Value :"+ compArr[CIX]);
                }


            }
                flag = true ; 
                while(flag){ 
                    System.out.print("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.print("Bad Value :" + compArr[LIX]) ; 

                    }
                }
                return (int) (compArr[CIX] + compArr[LIX] + compArr[RIX] + compArr[VIX]) ;  
            }


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

            return t[n] ;

        } 
        public static int genQArray( int timeArr[] , int n , double compArr [], int VIX , int CIX , int RIX , int LIX ) {
            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] ;


        }

    } 

Thanks in advance for the help !!

在此先感谢您的帮助 !!

Mark

1 个解决方案

#1


0  

You will need to change your compArr variable declaration from

您需要更改compArr变量声明

double compArr  = getCircuitComp() ; 

to

double[] compArr  = getCircuitComp() ;

to correctly match the arguments for the method genQArray. This will involve returning a double[] from getCircuitComp() instead of an int type.

正确匹配方法genQArray的参数。这将涉及从getCircuitComp()而不是int类型返回double []。

public static double[] getCircuitComp() {

In this method, you probably meant to return the variable:

在这个方法中,您可能想要返回变量:

double compArr [] = new double[4]; 

Also note that your variables:

还要注意你的变量:

static int VIX ; 
static int CIX ; 
static int LIX ; 
static int RIX ; 

will all be defaulted to 0 so, for instance, compArr[VIX] and compArr[CIX] will point to the same value at index 0 in the array compArr. To fix, you could specify values:

将默认为0,例如,compArr [VIX]和compArr [CIX]将指向数组compArr中索引0处的相同值。要修复,您可以指定值:

static int VIX = 0; 
static int CIX = 1; 
...

#1


0  

You will need to change your compArr variable declaration from

您需要更改compArr变量声明

double compArr  = getCircuitComp() ; 

to

double[] compArr  = getCircuitComp() ;

to correctly match the arguments for the method genQArray. This will involve returning a double[] from getCircuitComp() instead of an int type.

正确匹配方法genQArray的参数。这将涉及从getCircuitComp()而不是int类型返回double []。

public static double[] getCircuitComp() {

In this method, you probably meant to return the variable:

在这个方法中,您可能想要返回变量:

double compArr [] = new double[4]; 

Also note that your variables:

还要注意你的变量:

static int VIX ; 
static int CIX ; 
static int LIX ; 
static int RIX ; 

will all be defaulted to 0 so, for instance, compArr[VIX] and compArr[CIX] will point to the same value at index 0 in the array compArr. To fix, you could specify values:

将默认为0,例如,compArr [VIX]和compArr [CIX]将指向数组compArr中索引0处的相同值。要修复,您可以指定值:

static int VIX = 0; 
static int CIX = 1; 
...