简体   繁体   中英

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.). 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. 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. How can I separate them into different columns?

You have to specify what you want to print inside System.out.println(); instead: System.out.println(Y);

Your second while loop won't get entered because i is smaller than -10 after the first while loop. 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; or use a for loop, it would be cleaner. 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. 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

So, Your next while does not get executes as i >= -10 returns false

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它将起作用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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