英文:
Can't figure out this Error in Eclipse Java
问题
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);
}
//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) {
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;
}
}
英文:
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
P.s. 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);
}
}
答案1
得分: 0
变量DfinalResult无法在doubleTrouble方法之外被访问,但你在主方法中使用了它。
英文:
The variable DfinalResult cant be seen outside of the method doubleTrouble and you are using in on the main method.
答案2
得分: 0
1: 在你的程序中有4个错误
2: 我不知道你是否了解,但当你使用return命令时,它不会继续执行代码
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)); //糟糕 :) 最终答案 x (Fx)
return (int) Fx; "在这里你使用了return,所以下一行不会被执行"
double Fy = Math.exp(7); //最终答案 y (Fy)
return Fy;
double Fz = Math.asin(Fy)/(200); //最终答案 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);
}
英文:
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);
}
答案3
得分: -1
doubleTrouble()
方法包含了很多错误。
- 你不能在其中间写
return
语句。 - 你没有在主方法中声明
DfinalResult
变量。
这就是代码无法编译的原因。
我已经修复了错误。更新为以下内容:
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;
}
}
英文:
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论