javafx 项目中的新类在尝试添加 if 语句时给出错误

在使用 if 语句时,我不断收到 “- 令牌 ”;“ 上的语法错误, { 此令牌后预期 ” 错误消息。 im 不确定为什么,我对 java 有些陌生。这是一个 java fx 项目,但我与控制器有任何连接。


我尝试添加一个 else 语句并更改变量。我还试图将声明和修改分开。


package application;


public class Mathprocess {



    int numberOne = 15;

    int numberTwo = 5;

    int answerNumbers;

    int ansSubtract = 0;

    int ansDivide =  0;

    int ansMultiply = 0;

    int ansAddition = 0;





    //Question 1

    ansAddition = numberOne + numberTwo;

        String questionOne = numberOne + " + " + numberTwo +" = ";

    //Question 2 

    ansMultiply = numberOne * numberTwo;

    String questionTwo = numberOne + " * " + numberTwo +" = ";


    //Question 3 

    ansDivide = numberOne / numberTwo;





    //Question 4

    ansSubtract = numberOne - numberTwo;

    // error happens here

    if (ansAddition > 0) { 

        answerNumbers = ansAddition;

    }


}

预期的结果是一个干净的程序,没有错误。


慕尼黑8549860
浏览 77回答 4
4回答

拉风的咖菲猫

你的问题是,在java中,每个操作都应该在方法内处理。试试下面这样:public void actions() { //declaring a method    ansAddition = numberOne + numberTwo;    String questionOne = numberOne + " + " + numberTwo + " = ";    ansMultiply = numberOne * numberTwo;    String questionTwo = numberOne + " * " + numberTwo + " = ";    ansDivide = numberOne / numberTwo;    ansSubtract = numberOne - numberTwo;    if (ansAddition > 0) {        answerNumbers = ansAddition;    }}

Helenr

您需要在方法中移动代码。其中一个解决方案可能如下所示package application;public class Mathprocess {    public static void main(String[] args){        int numberOne = 15;        int numberTwo = 5;        int answerNumbers;        int ansSubtract = 0;        int ansDivide =  0;        int ansMultiply = 0;        int ansAddition = 0;        //Question 1        ansAddition = numberOne + numberTwo;        String questionOne = numberOne + " + " + numberTwo +" = ";        //Question 2        ansMultiply = numberOne * numberTwo;        String questionTwo = numberOne + " * " + numberTwo +" = ";        //Question 3        ansDivide = numberOne / numberTwo;        //Question 4        ansSubtract = numberOne - numberTwo;        // error happens here        if (ansAddition > 0) {            answerNumbers = ansAddition;        }    }}但是,根据您的需求,它可能会有所不同。

梵蒂冈之花

您需要在函数中执行代码,例如“包应用程序;public class Mathprocess {    int numberOne = 15;    int numberTwo = 5;    int answerNumbers;    int ansSubtract = 0;    int ansDivide =  0;    int ansMultiply = 0;    int ansAddition = 0;   public static void main(String[] args)   {        //Question 1    ansAddition = numberOne + numberTwo;    String questionOne = numberOne + " + " + numberTwo +" = ";    //Question 2     ansMultiply = numberOne * numberTwo;    String questionTwo = numberOne + " * " + numberTwo +" = ";    //Question 3     ansDivide = numberOne / numberTwo;    //Question 4    ansSubtract = numberOne - numberTwo;    // error happens here    if (ansAddition > 0)    {         answerNumbers = ansAddition;    }    }}`

饮歌长啸

与定义类一致:类描述属于它的对象的内容:它描述数据字段(称为实例变量)的聚合,并定义操作(称为方法)。一个类包含2个事物实例变量和方法,所以如果你想把任何东西放在其他任何东西上,你必须得到方法的帮助public class Mathprocess {  int numberOne = 15;  int numberTwo = 5;  int answerNumbers;  int ansSubtract = 0;  int ansDivide = 0;  int ansMultiply = 0;  int ansAddition = 0;  //Question 1  ansAddition = numberOne + numberTwo;  String questionOne = numberOne + " + " + numberTwo + " = ";  //Question 2   ansMultiply = numberOne * numberTwo;  String questionTwo = numberOne + " * " + numberTwo + " = ";  //Question 3   ansDivide = numberOne / numberTwo;  //Question 4  ansSubtract = numberOne - numberTwo;  // error happens here  method() {    if (ansAddition > 0) {      answerNumbers = ansAddition;    }  }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java