在 Java 中使用 Math.pow 来确定立方体的表面积并且 'skipping' 也有困难
Using Math.pow in Java to determine surface area of a cube AND also a difficulty with 'skipping'
我正在编写一个简单的程序来确定一些数学值,但我遇到了一个问题,它要求用户输入立方体的边长,然后输出表面积 (使用 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) 之类的东西,我会得到相同的错误答案和预期错误:
~
我 运行 喜欢的另一件事是,当我要求用户输入由 space 分隔的两个数字以寻找带有 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() 后在下面简单地要求更多的双打。但是,那会有些复杂。我建议你一步一步地前进。按照 books/tutorials 并保持该顺序。
6a^2
这意味着 a
的平方的六倍。
Math.pow (6*( a,2 )
这没有任何意义。它甚至不是有效的语法。您需要:
6*Math.pow (a,2 )
或更简单地说
6*a*a
对于你问的第二个问题,我建议你把用户输入的数字放到一个HashSet
中,然后用contains
的方法检查用户输入的数字是否已经被使用过.您可以检查 here 以供参考 HashSet
。
我正在编写一个简单的程序来确定一些数学值,但我遇到了一个问题,它要求用户输入立方体的边长,然后输出表面积 (使用 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) 之类的东西,我会得到相同的错误答案和预期错误:
~
我 运行 喜欢的另一件事是,当我要求用户输入由 space 分隔的两个数字以寻找带有 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() 后在下面简单地要求更多的双打。但是,那会有些复杂。我建议你一步一步地前进。按照 books/tutorials 并保持该顺序。
6a^2
这意味着 a
的平方的六倍。
Math.pow (6*( a,2 )
这没有任何意义。它甚至不是有效的语法。您需要:
6*Math.pow (a,2 )
或更简单地说
6*a*a
对于你问的第二个问题,我建议你把用户输入的数字放到一个HashSet
中,然后用contains
的方法检查用户输入的数字是否已经被使用过.您可以检查 here 以供参考 HashSet
。