为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕尼黑8549860 2022-09-07 17:55:33
在使用 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;    }}预期的结果是一个干净的程序,没有错误。
查看完整描述

4 回答

?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

你的问题是,在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;

    }

}


查看完整回答
反对 回复 2022-09-07
?
Helenr

TA贡献1780条经验 获得超3个赞

您需要在方法中移动代码。其中一个解决方案可能如下所示


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;

        }

    }



}

但是,根据您的需求,它可能会有所不同。


查看完整回答
反对 回复 2022-09-07
?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

您需要在函数中执行代码,例如“包应用程序;


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;

    }

    }


}`


查看完整回答
反对 回复 2022-09-07
?
饮歌长啸

TA贡献1951条经验 获得超3个赞

与定义类一致:类描述属于它的对象的内容:它描述数据字段(称为实例变量)的聚合,并定义操作(称为方法)。


一个类包含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;

    }

  }


}


查看完整回答
反对 回复 2022-09-07
  • 4 回答
  • 0 关注
  • 76 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信