我试图看看这里的问题是什么。基本上,在ctr = 10,100,1000和10000之后,程序应该从sumOfRand中减去sumOfCutoff,并从sumOfRand中减去sumOfRounded,然后打印两者。但由于某种原因,它不打印两行,或做正确的减法。问题的附加部分是正确的,但减法部分是我遇到麻烦的地方。
有人看错了什么吗?
int ctr;
for(ctr = 0; ctr <= 10000; ctr++);
{
Random rand = new Random();
double A1 = rand.nextDouble();
double A2 = Math.floor(A1 * 1e3) / 1e3;
double A3 = (double)Math.round(A1* 1000) / 1000;
double A4 = Math.floor(A1 * 1e4) / 1e4;
double A5 = (double)Math.round(A1* 10000) / 10000;
double A6 = Math.floor(A1 * 1e5) / 1e5;
double A7 = (double)Math.round(A1* 100000) / 100000;
double A8 = Math.floor(A1 * 1e6) / 1e6;
double A9 = (double)Math.round(A1* 1000000) / 1000000;
System.out.println("The accumlators are:");
System.out.println(A1);
System.out.println(A2);
System.out.println(A3);
System.out.println(A4);
System.out.println(A5);
System.out.println(A6);
System.out.println(A7);
System.out.println(A8);
System.out.println(A9);
System.out.println();
double sumOfRand = (A1 + rand.nextDouble());
double sumOfCutoff = (A2 + A4 + A6 + A8);
double sumOfRounded = (A3 + A5 + A7 + A9);
System.out.println("The residuals after the 10th time are:");
if(ctr == 10)
System.out.println(sumOfRand - sumOfCutoff);
System.out.println(sumOfRand - sumOfRounded);
System.out.println();
System.out.println("The residuals after the 100th time are:");
if(ctr == 100)
System.out.println(sumOfRand - sumOfCutoff);
System.out.println(sumOfRand - sumOfRounded);
System.out.println();
System.out.println("The residuals after the 1000th time are:");
if(ctr == 1000)
System.out.println(sumOfRand - sumOfCutoff);
System.out.println(sumOfRand - sumOfRounded);
System.out.println();
System.out.println("The residuals after the 10000th time are:");
if(ctr == 10000)
System.out.println(sumOfRand - sumOfCutoff);
System.out.println(sumOfRand - sumOfRounded);
}
}
编辑编辑编辑
Random rand = new Random();
double A1 = rand.nextDouble();
double A2 = Math.floor(A1 * 1e3) / 1e3;
double A3 = (double)Math.round(A1* 1000) / 1000;
double A4 = Math.floor(A1 * 1e4) / 1e4;
double A5 = (double)Math.round(A1* 10000) / 10000;
double A6 = Math.floor(A1 * 1e5) / 1e5;
double A7 = (double)Math.round(A1* 100000) / 100000;
double A8 = Math.floor(A1 * 1e6) / 1e6;
double A9 = (double)Math.round(A1* 1000000) / 1000000;
System.out.println("The accumlators are:");
System.out.println(A1);
System.out.println(A2);
System.out.println(A3);
System.out.println(A4);
System.out.println(A5);
System.out.println(A6);
System.out.println(A7);
System.out.println(A8);
System.out.println(A9);
System.out.println();
double sumOfRand = 0.0;
double sumOfCutoff = 0.0;
double sumOfRounded = 0.0;
int ctr;
for(ctr = 0; ctr <= 10000; ctr++)
{
if (ctr == 10)
{
System.out.println("The residuals after the 10th time are:");
System.out.println("Cutoff:" + (sumOfRand - sumOfCutoff));
System.out.println("Rounded:" + (sumOfRand - sumOfRounded));
System.out.println();
}
if(ctr == 100)
{
System.out.println("The residuals after the 100th time are:");
System.out.println("Cutoff:" + (sumOfRand - sumOfCutoff));
System.out.println("Rounded:" + (sumOfRand - sumOfRounded));
System.out.println();
}
if(ctr == 1000)
{
System.out.println("The residuals after the 1000th time are:");
System.out.println("Cutoff:" + (sumOfRand - sumOfCutoff));
System.out.println("Rounded:" + (sumOfRand - sumOfRounded));
System.out.println();
}
if(ctr == 10000)
{
System.out.println("The residuals after the 10000th time are:");
System.out.println("Cutoff:" + (sumOfRand - sumOfCutoff));
System.out.println("Rounded:" + (sumOfRand - sumOfRounded));
}
}
}
}
答案 0 :(得分:2)
您的{...}
条件似乎缺少if
,例如......
if(ctr == 10)
System.out.println(sumOfRand - sumOfCutoff);
System.out.println(sumOfRand - sumOfRounded);
System.out.println();
实际上在说......
if(ctr == 10)
System.out.println(sumOfRand - sumOfCutoff);
System.out.println(sumOfRand - sumOfRounded);
System.out.println();
我认为你的意思是
if(ctr == 10) {
System.out.println(sumOfRand - sumOfCutoff);
System.out.println(sumOfRand - sumOfRounded);
System.out.println();
}
大括号为一组条件提供了背景......
这适用于所有if
语句......
您也可以使用if-else
语句代替......
if(ctr == 10) {
//...
} else if(ctr == 100) {
//...
} else if(ctr == 1000) {
//...
} else if(ctr == 10000) {
//...
}
for (ctr = 0; ctr <= 10000; ctr++);
{
如果您在for
语句的末尾看起来很狡猾,那么您就拥有;
。这基本上意味着,循环1000次,......什么都不做......
将其更改为...
for (ctr = 0; ctr <= 10000; ctr++)
{
删除声明末尾的;