简体   繁体   English

错误解析变量和左侧分配并计算++令牌

[英]Error resolving variables and left-side side assignments and figuring ++ tokens

I'm starting to get blind looking at my code and my brain is about to overheat. 我开始盲目查看我的代码,我的大脑即将过热。 I'm new when it comes to programming. 我是编程新手。

public class RecyclingSystem { 公共类RecyclingSystem {

public static void main(String[] args) {

System.out.println("Please put in a valid bottle");


    Scanner sc = new Scanner(System.in);



    while ( sc.nextInt() != -1) {

        if (sc.nextInt(char a) = getaBottle);
                int bottleAcount++;
    } else if { (sc.nextInt(char b) = getbBottle);
                int bottleBcount++;
    } else if { (sc.nextInt(char c) = getcBottle);
                int bottleCcount++;
    } else { throw new EmptyStackException(); 
        System.out.println("Bottle not recognized");
    }
    System.out.println("The total number of bottles is " + totalBottlecount);
    System.out.println();
    System.out.println("The total amount returned is " + sumOfBottles );
    }
    sc.close();
}

}} }}

public class Bottle { 公共课瓶{

private static final double A_BOTTLE = 1.0;
/**
 * @return the aBottle
 */
public static double getaBottle() {
    return A_BOTTLE;
}
/**
 * @return the bBottle
 */
public static double getbBottle() {
    return B_BOTTLE;
}
/**
 * @return the cBottle
 */
public static double getcBottle() {
    return C_BOTTLE;
}
private static final double B_BOTTLE = 1.5;
private static final double C_BOTTLE = 3.0;

} }

public class EmptyStackException extends Exception { 公共类EmptyStackException扩展了异常{

} }

public class bottleCount { 公共类bottleCount {

int bottleAcount = 0;

int bottleBcount = 0;

int bottleCcount = 0; 

int totalBottleCount = bottleAcount + bottleBcount + bottleCcount;

} }

I have seperate classes for the getbottle, totalBottlecount and bottlecount variables. 我为getbottle,totalBottlecount和bottlecount变量分别设置了类。

I want to make a user-input based recycling system simulator, if that makes any sense, with 3 different types of bottles, which are assigned different values, a total bottle count and the sum of the values of the 3 bottle types combined. 我想制作一个基于用户输入的回收系统模拟器,如果有任何意义的话,请为3种不同类型的瓶子分配不同的值,总瓶子数以及这3种瓶子类型的总和。

I get several compiler errors and I have spend HOURS trying to resolve them all, but every time I do, new errors occur and now I get a "coders-block". 我遇到了几个编译器错误,并且花了数小时试图解决所有这些错误,但是每次这样做,都会出现新的错误,现在我得到了一个“编码器块”。

I get asked to delete the ++ tokens, the compiler cannot resolve my variables and syntax errors. 我被要求删除++标记,编译器无法解决我的变量和语法错误。 I would really appreciate some insight, since I'm only ~3weeks into java programming. 我会很感激一些见识,因为我只有大约3周的Java编程时间。

UPDATED: Compiler errors exact copy pasta 更新:编译器错误精确复制面食

Multiple markers at this line
    - Syntax error, insert ")" to complete Expression
    - Duplicate method nextInt(char) in type RecyclingSystem
    - Syntax error, insert "}" to complete Block
    - Syntax error, insert "AssignmentOperator Expression" to complete Assignment
    - Return type for the method is missing
    - Syntax error on tokens, delete these tokens
    - The left-hand side of an assignment must be a variable
    - Syntax error, insert "AssignmentOperator Expression" to complete Expression
    - The left-hand side of an assignment must be a variable
    - Syntax error, insert ";" to complete BlockStatements
    - Syntax error on tokens, delete these tokens
    - Syntax error on token ")", { expected after this token
    - Syntax error, insert ";" to complete Statement
int bottleAcount++;

In java you need to declare the local variable like 在Java中,您需要声明局部变量,例如

type name = intial value; type name =初始值;

then do any operation on that like increament or decrement. 然后对它进行任何操作,如减脂或减脂。

In youe case declar the variable before while loop with zero as intial value like 在你的情况下,在while循环之前用zero作为初始值声明变量,例如

int  bottleAcount = 0;

then inside while increament it by 1, like bottleAcount++; 然后在里面加1,就像bottleAcount++;

or 要么

bottleAcount += 1;

So... If this is all the code there are many problems and what can I recommend in the beginning - go back to some basic Java programming course. 所以...如果这是全部代码,那么会有很多问题,我一开始可以建议什么-返回一些基本的Java编程课程。

Let's look at one of the first lines: 让我们看一下第一行:

if (sc.nextInt(char a) = getaBottle);

  • Firstly, it's a condition, and you are assigning the value of a getaBottle to the sc.nextInt(char a) . 首先,这是一个条件,您正在 getaBottle的值分配给 sc.nextInt(char a)
  • Secondly, nextInt(char a) looks like method declaring, not like a method call. 其次, nextInt(char a)看起来像方法声明,而不是方法调用。
  • Thirdly, getaBottle is never declared before 第三,getaBottle从未声明过
  • Fourthly, you have a getaBottle() method in a Bottle class, which you probably want to use instead of getaBottle which is (should) be a variable 第四,在Bottle类中有一个getaBottle()方法,可能要使用它而不是getaBottle ,后者应该是一个变量

...etc., etc. ...等。

This code is not even valid Java code. 该代码甚至不是有效的Java代码。 It's hard to help you somehow in that problem, you just need to learn a bit more, which I encourage you to do. 很难以某种方式帮助您,您只需要学习更多,我鼓励您这样做。

Good luck and in case of any specific question - come and ask! 祝您好运,如果有任何具体问题,请前来询问!

else if { (sc.nextInt(char b) = getbBottle);
                int bottleBcount++;
    }

Syntax is totally wrong for if.Also condition is checked using == not = It should be : if的语法完全错误。还使用== not =检查条件:

else if (sc.nextInt(char b) == getbBottle);
                int bottleBcount++;

Also you cant do int bottleBcount++ . int bottleBcount++能做int bottleBcount++ Its meaningless. 它没有意义。 Since you already declared bottleBcount in another class, you have to access it using the object of that class. 由于您已经在另一个类中声明了bottleBcount ,因此必须使用该类的对象进行访问。 Before that change the declaration in the class from int bottleAcount = 0; 在此之前,将类中的声明从int bottleAcount = 0;更改为int bottleAcount = 0; to public int bottleAcount = 0; public int bottleAcount = 0; . Do this for all bottles. 对所有瓶子都这样做。 Now you can use it as : 现在,您可以将其用作:

public static void main(String[] args) {

System.out.println("Please put in a valid bottle");

     bottleCount counter = new bottleCount();
    Scanner sc = new Scanner(System.in);



    while ( sc.nextInt() != -1) {

        if (sc.nextInt(char a) == getaBottle);
                counter.bottleAcount++;
    } else if (sc.nextInt(char b) == getbBottle);
                counter.bottleBcount++;
    else if (sc.nextInt(char c) == getcBottle);
                counter.bottleCcount++;
     else { throw new EmptyStackException(); 
        System.out.println("Bottle not recognized");
    }
    System.out.println("The total number of bottles is " + totalBottlecount);
    System.out.println();
    System.out.println("The total amount returned is " + sumOfBottles );
    }
    sc.close();
}

Also the statement int totalBottleCount = bottleAcount + bottleBcount + bottleCcount; 还要声明int totalBottleCount = bottleAcount + bottleBcount + bottleCcount; doesnt make sense. 没有道理。 It won't work as you wanted it to. 它不会像您想要的那样工作。 You need to write a function to do this addition and then call it. 您需要编写一个函数来执行此添加操作,然后调用它。 Or if you want this to happen just once ( which I doubt) , you can put it in a constructor. 或者,如果您希望仅发生一次(我对此表示怀疑),则可以将其放入构造函数中。

I suggest you brush up on class and variable declaration concepts before proceeding 我建议您在继续之前先复习class和变量声明的概念

You just have problem in this: 您只是对此有问题:

else { 
    throw new EmptyStackException(); 
    System.out.println("Bottle not recognized");
}

Check the proper syntax and error will be resolved. 检查正确的语法,错误将得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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