我需要帮助实现一个while循环,它将用户输入作为double或int(不确定)并将其作为平方根,直到它达到小于或等于1.01。
示例:Math.sqrt(10.5)
1. Calculated square root is --> 3.24037034920393
2. Calculated square root is --> 1.8001028718392542
3. Calculated square root is --> 1.3416791240230483
4. Calculated square root is --> 1.1583087343290857
5. Calculated square root is --> 1.0762475246564267
6. Calculated square root is --> 1.0374235030383814
7. Calculated square root is --> 1.0185398877993839
import java.util.Scanner;
public class SquareRoot {
public static void main (String args[]){
final double SENTINEL = 10;
int count = 0;
Scanner scan = new Scanner(System.in);
double userInput; // input value
do
{
System.out.println( "Enter a number greater than 10" );
userInput = scan.nextDouble( );
} while ( userInput <= 1 || userInput >= 10 );
double squareRoot = Math.sqrt(userInput);
double resultSquareRoot = Math.sqrt(squareRoot);
while (resultSquareRoot <= 1.01){
resultSquareRoot = Math.sqrt(squareRoot);
}
System.out.println(resultSquareRoot);
}
}
答案 0 :(得分:2)
你的循环:
while (resultSquareRoot <= 1.01){
resultSquareRoot = Math.sqrt(squareRoot);
}
System.out.println(resultSquareRoot);
应该是:
while (resultSquareRoot > 1.01){
System.out.println("Calculated square root is --> " + resultSquareRoot);
resultSquareRoot = Math.sqrt(resultSquareRoot);
}
您不会将参数的值更改为Math.sqrt
,因此resultSquareRoot
永远不会更改。
您想要resultSquareRoot = Math.sqrt(resultSquareRoot);
而不是resultSquareRoot = Math.sqrt(squareRoot);
答案 1 :(得分:1)
您的解决方案中很少有注意事项,为什么尝试在do-while中调用以下代码行。 不需要,因为你的意图是应用Math.sqrt,直到输入的数字方块达到1.01,因此我们在谈论循环。
System.out.println(“输入一个大于10的数字”); userInput = scan.nextDouble();
此外,您不需要第二次while循环。
while (resultSquareRoot <= 1.01){
resultSquareRoot = Math.sqrt(squareRoot);
}
代码中缺少的是中断条件。
这可以通过以下代码行来实现。
do {
userInput = Math.sqrt(userInput);
System.out.println("Calculated square root is -->"+userInput);
if(userInput<= 1.01)
break;
} while (userInput >= 1.01 );
Calculated square root is -->3.24037034920393
Calculated square root is -->1.8001028718392542
Calculated square root is -->1.3416791240230483
Calculated square root is -->1.1583087343290857
Calculated square root is -->1.0762475246564267
Calculated square root is -->1.0374235030383814
Calculated square root is -->1.0185398877993839
Calculated square root is -->1.0092273717053972
答案 2 :(得分:0)
您可能想要更改
while (resultSquareRoot <= 1.01){
resultSquareRoot = Math.sqrt(squareRoot);
}
到
while (resultSquareRoot > 1.01){
resultSquareRoot = Math.sqrt(resultSquareRoot );
}
答案 3 :(得分:0)
do while条件错误。它应该是:
do
{
System.out.println( "Enter a number greater than 10" );
userInput = scan.nextDouble( );
} while ( userInput <= 10 );
而while条件也是错误的。请检查下面的代码:
public static void main (String args[]){
final double SENTINEL = 10;
int count = 0;
Scanner scan = new Scanner(System.in);
double userInput; // input value
do
{
System.out.println( "Enter a number greater than 10" );
userInput = scan.nextDouble( );
} while ( userInput <= 10 );
double resultSquareRoot = userInput;
while (resultSquareRoot >= 1.01){
resultSquareRoot = Math.sqrt(resultSquareRoot);
}
System.out.println(resultSquareRoot);
}
答案 4 :(得分:0)
您要求的数字大于10,所以您应该
do
{
System.out.println( "Enter a number greater than 10" );
userInput = scan.nextDouble( );
} while ( userInput < 10 );
因为你想要一个小于10的数字。
然后在这里,您始终执行Math.sqrt(squareRoot)
并将其存储在resultSquareRoot
中,因此您在while循环内的resultSquareRoot
内始终获得相同的值,因为你永远不会改变squareRoot
的价值。并且您希望您的条件运行while it is greater than 1.01
double squareRoot = Math.sqrt(userInput);
double resultSquareRoot = Math.sqrt(squareRoot);
while (resultSquareRoot > 1.01){
resultSquareRoot = Math.sqrt(resultSquareRoot);
}
System.out.println(resultSquareRoot);