我试图编写一个使用古老的巴比伦平均法计算数字平方根的程序。该程序的目标是显示用户输入的数字,平均方法的值,使用数学包的值,以及平均方法和数学包之间的差异。我遇到的唯一问题是差异的输出达到0.000。有人可以帮我解决两种方法之间的实际差异。这是我的程序代码:
import java.util.*;
public class SquareRoot
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int k;
double number;
double number2;
double generator ;
double Difference;
generator = (int)(Math.random() * 20 + 1); // generates a random number between '1' and '20'
////////////////////////Take input from the user/////////////////////////////////////////////////////
System.out.print("Please enter the number that you want to take the square root of: ");
number = console.nextDouble();
//////////////////////////generate the square root using the averaging method///////////////////////
for( k = 1; k <= 10; k++ )
{
generator = ( generator + (number/generator)) / 2;
}
////////////////////////Place the math package value in 'number2' and take the difference of the//////
/////////////////////// averaged value and the true math package value////////////////////////////////
number2 = Math.sqrt(number);
Difference = (number2 - generator);
///////////////////////////////Display the results to the user////////////////////////////////////////
System.out.println("\nNumber SquareRoot MathPackage Difference");
System.out.printf("%-6.3f %-10.3f ", number, generator);
System.out.printf("%-11.3f %-10.3f%n" , number2 , Difference);
}
}
答案 0 :(得分:0)
您的随机数生成器稍微偏离了一点。你想要像
这样的东西double generator = 1 + (Math.random() * 20);
当我进行更正时,您现有的代码运行正常。但是,我建议您在准备初始化变量之前不要声明变量。另外,我建议你遵循小写camelCase变量名(difference
)的Java命名约定。最后,在Scanner
字段中放置System.in
static
似乎不必要地复杂。总而言之,您的代码可能看起来像
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("Please enter the number that you want to take "
+ "the square root of: ");
double number = console.nextDouble();
double generator = 1 + (Math.random() * 20);
for (int k = 0; k < 10; k++) {
generator = (generator + (number / generator)) / 2;
}
double number2 = Math.sqrt(number);
double difference = (number2 - generator);
System.out.println("\nNumber SquareRoot MathPackage Difference");
System.out.printf("%-6.3f %-10.3f ", number, generator);
System.out.printf("%-11.3f %-10.3f%n", number2, difference);
}
经过10次迭代后,巴比伦方法将产生一个非常准确的答案(范围为double
)。尝试在循环中打印。像,
double number = console.nextDouble();
double number2 = Math.sqrt(number);
double generator = 1 + (Math.random() * 20);
System.out.println("Iteration SquareRoot Difference");
for (int k = 0; k < 10; k++) {
generator = (generator + (number / generator)) / 2;
double difference = (number2 - generator);
System.out.printf("%10d %-11.3f %-10.10f%n", k + 1, number2, difference);
}
然后您会看到近似的difference
方法0
。
答案 1 :(得分:0)
要更改显示的小数位数,您需要将来电更改为printf
。
在下面的代码中,小数点后的数字(在本例中为3)指定小数点后显示的位数。
System.out.printf("%-6.3f %-10.3f ", number, generator);
System.out.printf("%-11.3f %-10.3f%n", number2, difference);
为了显示更多数字(例如10),将该数字增加到更高的数字。像这样:
System.out.printf("%-6.10f %-10.10f ", number, generator);
System.out.printf("%-11.10f %-10.10f%n" , number2 , Difference);
如果您使用该简单更改运行相同的代码,您的输出将更改为:
Number SquareRoot MathPackage Difference
109300.0000000000 330.6055107088 330.6055050963 -0.0000056125
你可能需要稍微搞乱格式化,但除此之外它还可以。
有关此类字符串替换的更多信息,请参阅Formatter类的文档。