简体   繁体   English

如何在 Java 中根据 x 编写数学表达式

[英]How to write a maths expression in terms of x in Java

I am trying to write a function that parses in 2 numbers eg coefficient 5 and power 3. I want the output response to be 5x^3.我正在尝试编写一个解析 2 个数字的函数,例如系数 5 和幂 3。我希望输出响应为 5x^3。

public void Variable(double c, double p)
{
     coefficient = c;
     power = p;
}

public Expression derive() 
{
      System.out.print("Youre term is " + coefficient + "" + Math.pow("x",power));
}

What have I done wrong here?我在这里做错了什么? It's not accepting my "x" in the pow function.它在 pow 函数中不接受我的“x”。 But I'm not sure how to have a simple letter in there?但我不知道如何在里面放一个简单的字母?

Cheers干杯

Math.pow(double a, double b) doesn't accept a string as its first argument. Math.pow(double a, double b)不接受字符串作为其第一个参数。 It expects a double for both the base and the exponent.它期望基数和指数双倍。 That method is for calculating values based on the base and the exponent supplied.该方法用于根据提供的基数和指数计算值。

-> Returns the value of the first argument raised to the power of the second argument. -> 返回第一个参数的第二个参数的幂的值。

Instead, what you want to achieve can easily be done without using pow() method.相反,无需使用 pow() 方法即可轻松完成您想要实现的目标。

Just display coefficient + "x ^ " + power to the console.只需向控制台显示coefficient + "x ^ " + power

System.out.print("Your term is " + coefficient + "x^" + power);

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

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