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

标签 java math pow

我正在编写一个简单的程序来确定一些数学值,但我遇到了一个问题,它要求用户提供立方体一边的长度,然后输出表面积(使用 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/

相关文章:

actionscript-3 - 使用 ActionScript 评估数学公式字符串

python - 使用 pow 函数 python 计算 mod

C 的 pow 函数拒绝使用可变指数

java - 每当它要求从java中的键盘输入时,如何从数组提供输入

excel - 在每个 jar 的最大可能容量下,找到容纳最大数量 Wine 的最少 jar 数

Java ChangeListener 多次触发

math - Clojure 中的基本几何

c - 如何从科学记数法解析长双

java - 仅 jlabel 的鼠标单击事件正确更新 jlabel 文本属性

java - JDBC连接池问题