gpt4 book ai didi

java - 在 Java 中使用 Math.pow 确定立方体的表面积,并且 'skipping' 也有困难

转载 作者:行者123 更新时间:2023-12-01 16:55:18 25 4
gpt4 key购买 nike

我正在编写一个简单的程序来确定一些数学值,但我遇到了一个问题,它要求用户提供立方体一边的长度,然后输出表面积(使用 Math .pow 和公式 6a^2)。我得到了公式和所有内容,但也许我输入错误?我不断得到与我应该得到的不同的答案......

System.out.print ("Please enter the length of a side of your cube: ");
a = in.nextDouble();
System.out.printf ("Surface area of your cube is : %.3f\n", Math.pow (6*( a,2 )));

这个问题涉及间距还是什么?它只是不断地给我带来括号的预期错误,但我确信一切看起来都很好......我还尝试改变诸如 (( a,2 )*6) 之类的内容,我会得到相同的错误答案和预期错误):

~

我遇到的另一件事是,当我要求用户输入由空格分隔的两个数字时,用 Math.max 查找较大的数字,它可以工作,但随后它使用相同的数字来要求较小的数字。我不希望用户使用这些相同的数字,因为我希望他们输入一些其他新数字...

System.out.print ("Please enter two numbers separated by spaces: ");
a = in.nextDouble();
System.out.printf("Larger of the first two numbers is %.3f\n", Math.max ( a,a ) );

System.out.print ("Please enter two numbers separated by spaces: ");
a = in.nextDouble();
System.out.printf ("Smaller of the first two number is %.3f\n", Math.min ( a,a ) );

我是编程新手。非常感谢任何帮助!!

最佳答案

您的代码不正确,Math.pow(base, exponent) 需要 2 个参数:-

System.out.printf ("Surface area of your cube is : %.3f\n", Math.pow (6*( a,2 )));

应该是:-

System.out.printf ("Surface area of your cube is : %.3f\n", 6 * Math.pow (a,2) );

并且,为了找到平方,您应该坚持简单的概念,即执行 (a*a),而不是选择 Math.pow(base,exponent)

[注意 -> 乘法比调用 pow() 方法更容易]。

when I ask the user for two numbers separated by a space, to look for the larger one with Math.max, it works, but then it uses the same numbers to ask for the smaller number. I DO NOT want the user to use these same numbers as I want them to enter some other new numbers...

首先,这应该是一个单独的问题。但是,即使如此,为了实现相同的目标,您也需要了解循环知识,以便可以运行任务预定的次数。

由于这个问题很简单,您可以在计算 max() 后简单地在下面要求更多的 double 。但是,这会有些复杂。我建议你一步一步地前进。请遵循书籍/教程并保持该顺序。

关于java - 在 Java 中使用 Math.pow 确定立方体的表面积,并且 'skipping' 也有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33862401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com