Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么不是';这不是我的变量打印吗?_Java_While Loop_Println - Fatal编程技术网

Java 为什么不是';这不是我的变量打印吗?

Java 为什么不是';这不是我的变量打印吗?,java,while-loop,println,Java,While Loop,Println,这是学校的作业,我不需要答案,只要帮助就行。基本上,三角形的斜边总是1。我还知道X必须介于-1和1之间,如果是双精度(0.5,0.6等),则必须位于十分之一的位置。假设X是0.9,那么我必须让Java使用毕达哥拉斯定理计算三角形第三边的结果。我得到了那个角色。问题是,我有依赖于输出。我得到这个输出: public class FirstofTen { public static void main(String[] args) throws IOException { int i

这是学校的作业,我不需要答案,只要帮助就行。基本上,三角形的斜边总是1。我还知道
X
必须介于
-1
1
之间,如果是双精度(
0.5
0.6
等),则必须位于十分之一的位置。假设
X
0.9
,那么我必须让Java使用毕达哥拉斯定理计算三角形第三边的结果。我得到了那个角色。问题是,我有依赖于输出。我得到这个输出:

 public class FirstofTen
 {
public static void main(String[] args) throws IOException 
{
     int i = 10;

   while (i >= -10) {

      double x = i / 10.0;

      i--;

      String X = String.format("%.2f", x);

      System.out.println(X);        
    }

    i = 10; 

    while(i >= -10) {

      double x = i / 10.0;

      i--;

      String X = String.format("%.2f", x);

      double Y = Math.sqrt(Math.pow(1, 2) - Math.pow(x, 2));

      System.out.println(String.format("%.2f", Y));            
   }

如你所见,它先打印X,然后再打印Y,就在它的正下方。如何将它们分为不同的列

您必须指定要在内部打印的内容
System.out.println()改为:

System.out.println(Y)

无法输入第二个while循环,因为在第一个while循环之后,
i
小于-10。您应该在第二个循环之前重置其值。

您的代码存在多个问题:

首先,在第二个循环开始时,将
i
重置为10;或者对
循环使用
,这样会更干净。这样,这个代码

1.00
0.90
0.80
0.70
0.60
0.50
0.40
0.30
0.20
0.10
0.00
-0.10
-0.20
-0.30
-0.40
-0.50
-0.60
-0.70
-0.80
-0.90
-1.00
0.0
0.44
0.60
0.71
0.80
0.87
0.92
0.95
0.98
0.99
1.00
0.99
0.98
0.95
0.92
0.87
0.80
0.71
0.60
0.44
0.00
变成这样:

int i = 10;
while (i >= -10) {
    // work
    i--;
}
其次,在第二个循环中,您应该格式化
Y
,并实际打印出来。您也可以一次完成格式化和打印:

for (int i = 10; i >=-10; i--) {
    // work
}
while循环退出时,i的值为i=-1.0

因此,您的下一个while不会执行为i>=-10返回false

解决方案:

  while (i >= -10){
     double x = i / 10.0;
     i--;
     String X = String.format("%.2f", x);
     System.out.println("X : " + X);
  }

在第二个循环之前在代码中再次初始化i=10,而它将工作

在哪里打印
Y
?您可以共享当前的输出和尝试获得的输出吗?您注意到第二个打印行没有参数吗?第二个循环的条件与第一个循环相同,因此它总是错误的。我建议调试你的代码。你可能想澄清你在这里想说什么。作为一名经验丰富的程序员,我觉得答案不错。。。其他人可能也不明白。我明白了,谢谢!当我将代码复制到论坛上时,这只是一个愚蠢的错误。非常感谢!我知道了,但是我的输出现在有X的所有正确值,然后是Y的所有正确值。这是一个巨大的数字列。我怎样才能分开这些列,所以X在左边,Y在右边?(请注意,更新的代码在我的原始帖子上)@Kush根据您的格式更新上述代码Requirement@Kush如果你的问题解决了,那么接受答案,这样其他用户就可以看到答案。谢谢!现在Y正在打印,但它的输出正好低于X。我用更多的信息更新了这个问题,那么如何重新格式化Y,使其位于不同的列中呢?您可以通过
System.out.println(X+“”+String.format(“%.2f”,Y)),在同一行中打印它
  while (i >= -10){
     double x = i / 10.0;
     i--;
     String X = String.format("%.2f", x);
     System.out.println("X : " + X);
  }
int i=10;
System.out.print("Values For X : ");
while (i >= -10) {
   // Code Goes Here for first while loop
   System.out.print(" " + X)
}
i=10; // Just re-initialize i with previous value i.e., 10
System.out.print("Values For Y : ");
while(i>=-10) {
   // Code Goes Here for second while loop
    System.out.print(" " + Y);  // and not  System.out.println();
}