简体   繁体   English

为什么没有可变印刷?

[英]Why isn't my variable printing?

 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));            
   }

This is a school assignment, I don't want an answer, just help. 这是学校的作业,我不想要答案,只是帮助。 Basically, I am given that the hypotenuse of the triangle is always 1. I also know that X has to be between -1 and 1 , and it has to be in the tenths place if its a double ( 0.5 , 0.6 , etc.). 基本上,我知道三角形的斜边始终为1。我也知道X必须在-11之间,并且如果它的倍数0.50.6等),则它必须在十分之几。 。 So let's say X is 0.9 , then I have to let Java calculate the result of the third side of my triangle using the Pythagorean theorem. 因此,假设X0.9 ,那么我必须让Java使用勾股定理来计算三角形第三边的结果。 I got that part. 我有那部分。 The problem, I am having relies on the output. 问题是,我依赖输出。 I get this output: 我得到以下输出:

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

As you can see, it prints X and then it prints Y right below it. 如您所见,它先打印X,然后在其正下方打印Y。 How can I separate them into different columns? 如何将它们分为不同的列?

You have to specify what you want to print inside System.out.println(); 您必须在System.out.println();指定要打印的内容System.out.println(); instead: System.out.println(Y); 相反: System.out.println(Y);

Your second while loop won't get entered because i is smaller than -10 after the first while loop. 您的第二个while循环将不会输入,因为在第一个while循环之后i小于-10。 You should reset its value before the second loop. 您应该在第二个循环之前重置其值。

There are multiple problems with your code: 您的代码有多个问题:

First, you shout reset your i back to ten at the start of the second loop; 首先,在第二个循环开始时,您大声将i重置为十; or use a for loop, it would be cleaner. 或使用for循环,会更清洁。 That way, this code 这样,这段代码

int i = 10;
while (i >= -10) {
    // work
    i--;
}

becomes this: 变成这个:

for (int i = 10; i >=-10; i--) {
    // work
}

Secondly, in your second loop, you should format your Y and actually print it out. 其次,在第二个循环中,应格式化Y并实际打印出来。 You can do the formatting and the printing in one go too: 您也可以一口气进行格式化和打印:

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

When while loop exits the value for i is i = -1.0 while循环退出时, i的值是i = -1.0

So, Your next while does not get executes as i >= -10 returns false 因此,由于i> = -10返回false ,因此下一个while不执行

Solution : 解决方案:

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();
}

在第二次while循环之前在代码中再次初始化i = 10它将起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM