Why do I get this error in Java? Exception in thread "main" java.lang.Error: Unresolved compilation problems

huangapple go评论105阅读模式
英文:

Why do I get this error in Java? Exception in thread "main" java.lang.Error: Unresolved compilation problems

问题

import java.util.Scanner;

public class linearequationproblemredo {
    private double a;
    private double b;
    private double c;
    private double d;
    private double e;
    private double f;

    public void linearequationproblemredo(double a, double b, double c, double d, double e, double f) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.e = e;
        this.f = f;
    }

    public double getA(){
        return this.a;
    }

    public double getB(){
        return this.b;
    }

    public double getC(){
        return this.c;
    }

    public double getD(){
        return this.d;
    }

    public double getE(){
        return this.e;
    }

    public double getF(){
        return this.f;
    }

    public boolean isSolvable(){
        if(this.a * this.d - this.b * this.c == 0) return false;
        return true;
    }

    public double getX(){
        if(isSolvable()) return ((e * d) - (b * f))/((a * d) - (b * c));
        return -1;
    }

    public double getY(){
        if(isSolvable()) return ((a * f) - (e * c))/((a * d) - (b * c));
        return -1;
    }

    public static void main(String []args){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a, b, c, d, e, f one at a time starting with a.");
        double []in = new double[6];
        for(int l = 0; l < 6; l++){
            in[l] = input.nextDouble();
        }
        linearequationproblemredo test = new linearequationproblemredo(in[0], in[1], in[2], in[3], in[4], in[5]);
        while(true){
            System.out.println("Type in a-f to show what you have entered OR 'progress' to progress with the script.");
            String t = input.next();
            if(t.equals("progress")) break;
            else if(t.equals("a")) System.out.println(test.getA());
            else if(t.equals("b")) System.out.println(test.getB());
            else if(t.equals("c")) System.out.println(test.getC());
            else if(t.equals("d")) System.out.println(test.getD());
            else if(t.equals("e")) System.out.println(test.getE());
            else if(t.equals("f")) System.out.println(test.getF());
            else System.out.println("Error, please enter a valid input.");
        }

        while(true){
            System.out.println("To show the output of either x or y, enter 'x' or 'y' or type 'quit' to end the script.");
            String output = input.next();
            if(output.equals("quit")) break;

            try{
                if(!test.isSolvable()) throw new Exception();
                if(output.equals("x")) System.out.println(test.getX());
                else if(output.equals("y")) System.out.println(test.getY());
                else System.out.println("Error, please enter a valid input.");
            }catch (Exception e){
                System.out.println("The equation has no solution.");
                break;
            }
        }
    }
}
英文:

I constantly get the following error when running this code:

"Exception in thread "main" java.lang.Error: Unresolved compilation problems: linearequationproblem cannot be resolved to a type linearequationproblem cannot be resolved to a type at linearequationproblemredo.main(linearequationproblemredo.java:86)"

import java.util.Scanner;
public class lineequation {
private double a;
private double b;
private double c;
private double d;
private double e;
private double f;
public void lineequation(double a, double b, double c, double d, double e, double f) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
}
public double getA(){
return this.a;
}
public double getB(){
return this.b;
}
public double getC(){
return this.c;
}
public double getD(){
return this.d;
}
public double getE(){
return this.e;
}
public double getF(){
return this.f;
}
public boolean isSolvable(){
if(this.a * this.d - this.b * this.c == 0) return false;
return true;
}
public double getX(){
if(isSolvable()) return ((e * d) - (b * f))/((a * d) - (b * c));
return -1;
}
public double getY(){
if(isSolvable()) return ((a * f) - (e * c))/((a * d) - (b * c));
return -1;
}
public static void main(String []args){
Scanner input = new Scanner(System.in);
System.out.println(&quot;Enter a, b, c, d, e, f one at a time starting with a. &quot;);
double []in = new double[6];
for(int l = 0; l &lt; 6; l++){
in[l] = input.nextDouble();
}
lineequation test = new lineequation(in[0], in[1], in[2], in[3], in[4], in[5]);
while(true){
System.out.println(&quot;Type in a-f to show what you have entered OR &#39;progress&#39; to progress with the script.&quot;);
String t = input.next();
if(t.equals(&quot;progress&quot;)) break;
else if(t.equals(&quot;a&quot;)) System.out.println(test.getA());
else if(t.equals(&quot;b&quot;)) System.out.println(test.getB());
else if(t.equals(&quot;c&quot;)) System.out.println(test.getC());
else if(t.equals(&quot;d&quot;)) System.out.println(test.getD());
else if(t.equals(&quot;e&quot;)) System.out.println(test.getE());
else if(t.equals(&quot;f&quot;)) System.out.println(test.getF());
else System.out.println(&quot;Error, please enter a valid input. &quot;);
}
while(true){
System.out.println(&quot;To show the output of either x or y, enter &#39;x&#39; or &#39;y&#39; or type &#39;quit&#39; to end the script.&quot;);
String output = input.next();
if(output.equals(&quot;quit&quot;)) break;
try{
if(!test.isSolvable()) throw new Exception();
if(output.equals(&quot;x&quot;)) System.out.println(test.getX());
else if(output.equals(&quot;y&quot;)) System.out.println(test.getY());
else System.out.println(&quot;Error, please enter a valid input. &quot;);
}catch (Exception e){
System.out.println(&quot;The equation has no solution.&quot;);
break;
}
}
}
}

答案1

得分: 2

import java.util.Scanner;

public class lineequation {
    private double a;
    private double b;
    private double c;
    private double d;
    private double e;
    private double f;

    public lineequation(double a, double b, double c, double d, double e, double f) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.e = e;
        this.f = f;
    }
    
    public double getA(){
        return this.a;
    }
    
    public double getB(){
        return this.b;
    }
    
    public double getC(){
        return this.c;
    }
    
    public double getD(){
        return this.d;
    }
    
    public double getE(){
        return this.e;
    }
    
    public double getF(){
        return this.f;
    }
    
    public boolean isSolvable(){
        if(this.a * this.d - this.b * this.c == 0) return false;
        return true;
    }
    
    public double getX(){
        if(isSolvable()) return ((e * d) - (b * f))/((a * d) - (b * c));
        return -1;
    }
    
    public double getY(){
        if(isSolvable()) return ((a * f) - (e * c))/((a * d) - (b * c));
        return -1;
    }
    
    public static void main(String []args){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a, b, c, d, e, f one at a time starting with a.");
        double []in = new double[6];
        for(int l = 0; l < 6; l++){
            in[l] = input.nextDouble();
        }
        lineequation test = new lineequation(in[0], in[1], in[2], in[3], in[4], in[5]);
        while(true){

            System.out.println("Type in a-f to show what you have entered OR 'progress' to progress with the script.");
            String t = input.next();

            if(t.equals("progress")) break;
            else if(t.equals("a")) System.out.println(test.getA());
            else if(t.equals("b")) System.out.println(test.getB());
            else if(t.equals("c")) System.out.println(test.getC());
            else if(t.equals("d")) System.out.println(test.getD());
            else if(t.equals("e")) System.out.println(test.getE());
            else if(t.equals("f")) System.out.println(test.getF());
            else System.out.println("Error, please enter a valid input.");
        }

        while(true){
            System.out.println("To show the output of either x or y, enter 'x' or 'y' or type 'quit' to end the script.");
            String output = input.next();
            if(output.equals("quit")) break;

            try{
                if(!test.isSolvable()) throw new Exception();
                if(output.equals("x")) System.out.println(test.getX());
                else if(output.equals("y")) System.out.println(test.getY());
                else System.out.println("Error, please enter a valid input.");
            }catch (Exception e){
                System.out.println("The equation has no solution.");
                break;
            }
        }
    }
}
英文:

A class can be instantiated via the constructor . so you need to make linearequationproblem as a constructor like below.

import java.util.Scanner;
public class lineequation {
private double a;
private double b;
private double c;
private double d;
private double e;
private double f;
public lineequation(double a, double b, double c, double d, double e, double f) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
}
public double getA(){
return this.a;
}
public double getB(){
return this.b;
}
public double getC(){
return this.c;
}
public double getD(){
return this.d;
}
public double getE(){
return this.e;
}
public double getF(){
return this.f;
}
public boolean isSolvable(){
if(this.a * this.d - this.b * this.c == 0) return false;
return true;
}
public double getX(){
if(isSolvable()) return ((e * d) - (b * f))/((a * d) - (b * c));
return -1;
}
public double getY(){
if(isSolvable()) return ((a * f) - (e * c))/((a * d) - (b * c));
return -1;
}
public static void main(String []args){
Scanner input = new Scanner(System.in);
System.out.println(&quot;Enter a, b, c, d, e, f one at a time starting with a. &quot;);
double []in = new double[6];
for(int l = 0; l &lt; 6; l++){
in[l] = input.nextDouble();
}
lineequation test = new lineequation(in[0], in[1], in[2], in[3], in[4], in[5]);
while(true){
System.out.println(&quot;Type in a-f to show what you have entered OR &#39;progress&#39; to progress with the script.&quot;);
String t = input.next();
if(t.equals(&quot;progress&quot;)) break;
else if(t.equals(&quot;a&quot;)) System.out.println(test.getA());
else if(t.equals(&quot;b&quot;)) System.out.println(test.getB());
else if(t.equals(&quot;c&quot;)) System.out.println(test.getC());
else if(t.equals(&quot;d&quot;)) System.out.println(test.getD());
else if(t.equals(&quot;e&quot;)) System.out.println(test.getE());
else if(t.equals(&quot;f&quot;)) System.out.println(test.getF());
else System.out.println(&quot;Error, please enter a valid input. &quot;);
}
while(true){
System.out.println(&quot;To show the output of either x or y, enter &#39;x&#39; or &#39;y&#39; or type &#39;quit&#39; to end the script.&quot;);
String output = input.next();
if(output.equals(&quot;quit&quot;)) break;
try{
if(!test.isSolvable()) throw new Exception();
if(output.equals(&quot;x&quot;)) System.out.println(test.getX());
else if(output.equals(&quot;y&quot;)) System.out.println(test.getY());
else System.out.println(&quot;Error, please enter a valid input. &quot;);
}catch (Exception e){
System.out.println(&quot;The equation has no solution.&quot;);
break;
}
}
}
}

答案2

得分: 0

我认为你在Java方面完全是个新手。
一个很好的起点是Java教程,然后继续学习Oracle Java教程

这段代码无法编译,存在以下问题:

  1. 类名为lineequation,构造函数名为linearequationproblem,这是不被允许的。在Java中,类名和构造函数名应该相同,因为构造函数是用于创建类的实例并初始化成员变量的特殊方法。

  2. 第56行:java linearequationproblem test = new linearequationproblem(in[0], in[1], in[2], in[3], in[4], in[5]);
    你尝试创建一个linearequationproblem的实例,但这种类型(类)不存在。

  3. private double f;` 那个反引号是不合法的,会导致编译失败,因为这不是正确的语法。

你可以参考上面的文档以获取关于Java的更多细节。享受学习吧!

英文:

I think you are completely new in Java.
A good starting point is Java Tutorials and then move on to Oracle Java Tutorials

This code wont compile and these are the problems:

  1. The class name is lineequation and the constructor name is linearequationproblem, which is not allowed. In Java class name and constructor name should be same, since constructor is a special method for creating the instance of the class and initializing the member variables.

  2. Line 56: java
    linearequationproblem test = new linearequationproblem(in[0], in[1], in[2], in[3], in[4], in[5]);

    You are trying to create and instance of linearequationproblem but this type (Class) does not exists.

  3. private double f;` That backtick is not legit and will fail to compile, since this is not a correct syntax.

You can refer the docs above for more details about Java. Enjoy Learning !

huangapple
  • 本文由 发表于 2020年8月16日 22:51:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63438272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定