我做错了什么 y =4x^2 + 5x -3 ,x 必须从 0.1 开始到 1.0 结束 + 0.1

我需要xgo + in 0.1 步骤直到它达到 1.0。


对于每一步我都需要函数的输出y=4x^2 + 5x -3


我的 Java 给我的输出不是我所期望的。


我尝试了一个 while 循环。我不得不说我是一个全新的初学者。


public class Main {

    public static void main(String[] args)

    {

        double x = 0.1;

        double x2 = Math.pow(x,2);

        double y = 4*x2 + 5*x - 3;

        double counter = 0.1;


        while(counter <= 1.0)

        {

            System.out.print(y);

            counter =+ 0.1;

            x =+ 0.1;             

        }

    }

}

输出只是一个长2.46-2.46-2.46-2.46-....-...-2.46


开满天机
浏览 110回答 5
5回答

叮当猫咪

这是因为您没有在 x 更改时更新 y。您将 y 定义为:double y = 4*x2 + 5*x - 3;一开始,但你需要随着 x 的增量变化而更新它。你可以做:while(counter <= 1.0){&nbsp; &nbsp; y = 4*x*x + 5*x - 3;&nbsp; &nbsp; System.out.print(y);&nbsp; &nbsp; counter += 0.1;&nbsp; &nbsp; x += 0.1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}但这是一件美妙的事情,您可以使用 x 作为计数器来简化代码:while(x <= 1.0){&nbsp; &nbsp; y = 4*x*x + 5*x - 3;&nbsp; &nbsp; System.out.print(y);&nbsp; &nbsp; x += 0.1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}有更多方法可以简化此代码,我鼓励您尝试使用它并尝试找出一些方法来改进它!

慕丝7291255

欢迎来到编码世界!当您运行一个循环时,其中的代码将被执行多次。虽然很直观地认为通过定义为意味着什么时候更新的y函数,但不幸的是事实并非如此。为了更新该值,您必须在每次运行循环时重新评估它。xyxpublic static void main(String[] args) {&nbsp; &nbsp; double x = 0.1;&nbsp; &nbsp; double x2 = Math.pow(x,2);&nbsp; &nbsp; double y = 4*x2 + 5*x - 3;&nbsp; &nbsp; double counter = 0.1;&nbsp; &nbsp; while(counter <= 1.0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(y);&nbsp; &nbsp; &nbsp; &nbsp; counter += 0.1;&nbsp; &nbsp; &nbsp; &nbsp; //re-evaluate x, x2, and y here&nbsp; &nbsp; &nbsp; &nbsp; x += 0.1;&nbsp; &nbsp; &nbsp; &nbsp; x2 = Math.pow(x,2);&nbsp; &nbsp; &nbsp; &nbsp; y = 4*x2 + 5*x - 3;&nbsp; &nbsp; }}这行得通,但我们可以做得更好。如果您想尝试相对于 x 动态更新 y,请考虑编写一个函数:double calculateY(double x) {&nbsp; double value = 4*(x*x) + 5*x - 3;&nbsp; return value;}在你的循环中,你会这样调用函数:y = calculateY(x);函数是一种快速轻松地执行复杂代码集的好方法。作为奖励,如果您想y在代码中的其他地方计算,则不必从循环中复制粘贴。这是一个很好的做法,因为如果您以后需要更改方程式,则只需在函数中更改一次,而不是在可能会出错的多个地方更改。修改后的代码可能如下所示。请注意,我从 0 开始变量 - 这有助于减少混淆。double calculateY(double x) {&nbsp; double value = 4*(x*x) + 5*x - 3;&nbsp; return value;}public static void main(String[] args) {&nbsp; double x = 0;&nbsp; double y = 0;&nbsp; while (x <= 1.0) {&nbsp; &nbsp; x += 0.1;&nbsp; &nbsp; y = calculateY(x);&nbsp; &nbsp; System.out.print(y);&nbsp; }}更少的混乱!此代码易于阅读,也更易于编辑。如果你想用y不同的方式计算,你只需要修改calculateY函数——还有一个好处是你不需要重新计算,甚至不需要包含x2变量counter。

胡子哥哥

每次循环都需要计算。double getY(double x){...}while(counter <= 1.0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(getY(x));&nbsp; &nbsp; &nbsp; &nbsp; counter += 0.1;&nbsp; &nbsp; &nbsp; &nbsp; x += 0.1;&nbsp; &nbsp; }

qq_遁去的一_1

&nbsp;public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; double x = 0.1;&nbsp; &nbsp; &nbsp; &nbsp; double x2 = Math.pow(x,2);&nbsp; &nbsp; &nbsp; &nbsp; double y;&nbsp; &nbsp; &nbsp; &nbsp; double counter = 0.1;&nbsp; &nbsp; &nbsp; &nbsp; while(counter <= 1.0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y = 4*x2 + 5*x - 3;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(y);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter =+ 0.1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x =+ 0.1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}您需要重新计算 y 的值。

蓝山帝景

y问题是您在 update 之后没有重新计算x。这是一个可能有效的示例:public class Main {&nbsp; &nbsp; double static calcY(double x) {&nbsp; &nbsp; &nbsp; &nbsp; double x2 = Math.pow(x,2);&nbsp; &nbsp; &nbsp; &nbsp; double y = 4*x2 + 5*x - 3;&nbsp; &nbsp; &nbsp; &nbsp; return y;&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; double x = 0.1;&nbsp; &nbsp; &nbsp; &nbsp; double counter = 0.1;&nbsp; &nbsp; &nbsp; &nbsp; while(counter <= 1.0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double y = calcY(x);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(y);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter += 0.1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x += 0.1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java