简体   繁体   中英

Can't figure out this Error in Eclipse Java

My program keeps hitting a brickwall when I try to run the code. I get the error "Exception in thread "main" java.lang.Error: Unresolved compilation problem: DfinalResult cannot be resolved to a variable". I have tried to read other solutions but so far no luck. Any adivce on what im doing wrong to get this code to run. Also, it should be noted that the issue occurs when I try to call the result from one of my methods to a println. -Liam Ps If the numbers dont work out right now that's okay I havent been able to test it since I cant run it lol.

import java.util.Scanner;

public class mathMecklenburg {
    
    //note: i have a very good idea don't forget it 

    

    public static void main(String[] args) {

        System.out.println("Enter an integer between 1 and 10 to be used as the first value: ");
        Scanner scanint = new Scanner(System.in);
        int x = scanint.nextInt();
        System.out.println("Enter an integer between 1 and 10 to be used as the second value: ");
        int y = scanint.nextInt();
        System.out.println("Enter an integer between 1 and 10 to be used as the third value: ");
        int z = scanint.nextInt();    //x,y,z will be input values from person
        scanint.close();

        double A1 = operationOne(x);  //A1 is just operation 1 only using x. made double maybe for later difficulty
        System.out.println("The answer to part one of the problems is: " + A1);
        double A2 = operationTwo(x,y); // A2 is operation 2 using x and y
        System.out.println("The answer to part two of the problems is: " + A2);
        double A3 = operationThree(x,y,z); //A3 is operation 3 is using x,y,z. make something difficult here
        System.out.println("The answer to part three of the problems is: " + A3); //these print text then A3 
        int IfinalResult = (int) DfinalResult; 
        System.out.println("The double trouble answer is " + IfinalResult);
        
        
        //summary of the mentioned declared stuff in this part:
        //x,y,z are all input numbers from the person
        //A1, A2, A3 are the holders for each operations output i
        //currently all of them are same data types maybe make more confusing later 

         //final answer should be fun also make a method for final answer later to clean up code
        
        
        // finalAnswer = (Create an equation that utilizes all of the arithmetic operators and all three answers)
          //for me to check that numbers work out
        //System.out.println("The final answer is:" + IfinalResult); //put the result from the final method here for print);        

    }

    //side note: make sure all the first 3 operations give whole numbers no decimals yet 
    public static int operationOne(int x) {

        int answerOne;
        answerOne = x+2;
        // answerOne = (Create an equation that utilizes all of the arithmetic operators with the one input parameter)
        return answerOne;
    }

    public static int operationTwo(int x, int y) {
        int answerTwo;
        answerTwo = x + y;
        // answerOne = (Create an equation that utilizes all of the arithmetic operators with both input parameters)
        return answerTwo;
    }


    public static int operationThree(int x, int y, int z) {
        int answerThree;
        answerThree =  x + y + z;
        // answerThree = (Create an equation that utilizes all of the arithmetic operators using all three input parameters)
        return answerThree;
    }
    
    public static double doubleTrouble(int x, int y, int z, int A1, int A2, int A3, int IfinalResult) { //i thought the method name was funny 
    
    
    double Fx = Math.log(A1 - (A2 + A3)); //oops :) final answer x (Fx) 
    return (int) Fx;
    double Fy = Math.exp(7);  //final answer y (Fy)
    return Fy;
    double Fz = Math.asin(Fy)/(200);  //final answer z (Fz)
    return Fz;
    double Fz1 = Math.exp(Fx+Fy+Fz);
    double F1 = (Fx+Fy+Fz)*(java.lang.Math.PI)+A1; 
    double F2 = F1/(x)+(y)/(x)+5*(x+y);
    double F3 = F1+F2-A1;
    double DfinalResult = F1+F2+2*(F3*Fz1);
    }
    
}
    

在方法 doubleTrouble 之外无法看到变量 DfinalResult 并且您正在主方法中使用。

there is 4 errors in your program

1: there is no variable called. DfinalResult

2: I don't know if you know but when you use the command return it dosn't continue with the code

public static double doubleTrouble(int x, int y, int z, int A1, int A2, int A3, int IfinalResult) {


double Fx = Math.log(A1 - (A2 + A3)); //oops :) final answer x (Fx) 
return (int) Fx; "Here you return so the next line wouldn't be executed"
double Fy = Math.exp(7);  //final answer y (Fy)
return Fy;
double Fz = Math.asin(Fy)/(200);  //final answer z (Fz)
return Fz;
double Fz1 = Math.exp(Fx+Fy+Fz);
double F1 = (Fx+Fy+Fz)*(java.lang.Math.PI)+A1; 
double F2 = F1/(x)+(y)/(x)+5*(x+y);
double F3 = F1+F2-A1;
double DfinalResult = F1+F2+2*(F3*Fz1);
}

doubleTrouble() method contains a lot of errors.

  • You can't write return statements in between.
  • You didn't declare DfinalResult variable in the main method.

That's why the code didn't compile.

I have fixed the error. Update to this :

public class Sample {

    public static void main(String[] args) {

        System.out.println("Enter an integer between 1 and 10 to be used as the first value: ");
        Scanner scanint = new Scanner(System.in);
        int x = scanint.nextInt();
        System.out.println("Enter an integer between 1 and 10 to be used as the second value: ");
        int y = scanint.nextInt();
        System.out.println("Enter an integer between 1 and 10 to be used as the third value: ");
        int z = scanint.nextInt(); // x,y,z will be input values from person
        scanint.close();

        double A1 = operationOne(x); // A1 is just operation 1 only using x. made double maybe for later difficulty
        System.out.println("The answer to part one of the problems is: " + A1);
        double A2 = operationTwo(x, y); // A2 is operation 2 using x and y
        System.out.println("The answer to part two of the problems is: " + A2);
        double A3 = operationThree(x, y, z); // A3 is operation 3 is using x,y,z. make something difficult here
        System.out.println("The answer to part three of the problems is: " + A3); // these print text then A3
        int IfinalResult = (int) doubleTrouble(x, y, z, A1, A2, A3);
        System.out.println("The double trouble answer is " + IfinalResult);
    }

    // side note: make sure all the first 3 operations give whole numbers no
    // decimals yet
    public static int operationOne(int x) {

        int answerOne;
        answerOne = x + 2;
        // answerOne = (Create an equation that utilizes all of the arithmetic operators
        // with the one input parameter)
        return answerOne;
    }

    public static int operationTwo(int x, int y) {
        int answerTwo;
        answerTwo = x + y;
        // answerOne = (Create an equation that utilizes all of the arithmetic operators
        // with both input parameters)
        return answerTwo;
    }

    public static int operationThree(int x, int y, int z) {
        int answerThree;
        answerThree = x + y + z;
        // answerThree = (Create an equation that utilizes all of the arithmetic
        // operators using all three input parameters)
        return answerThree;
    }

    public static double doubleTrouble(int x, int y, int z, double a1, double a2, double a3) {
        double Fx = Math.log(a1 - (a2 + a3)); // oops :) final answer x (Fx)
        double Fy = Math.exp(7); // final answer y (Fy)
        double Fz = Math.asin(Fy) / (200); // final answer z (Fz)
        double Fz1 = Math.exp(Fx + Fy + Fz);
        double F1 = (Fx + Fy + Fz) * (java.lang.Math.PI) + a1;
        double F2 = F1 / (x) + (y) / (x) + 5 * (x + y);
        double F3 = F1 + F2 - a1;
        double DfinalResult = F1 + F2 + 2 * (F3 * Fz1);
        return DfinalResult;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM