简体   繁体   中英

How can I make a while loop print userNum = 1 with whitespace after it?

My assignment (an intro to Java course from Zybooks) says to: "Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow each number by a space. Example output for userNum = 20: 20 10 5 2 1

Note: This activity will perform four tests, with userNum = 20, then with userNum = 1, then with userNum = 0, then with userNum = -1."

I can get it to print "20 10 5 2 1 ", but when I successfully add a whitespace to the end of the above sequence of numbers, it doesn't add white space to 1 when userNum = 1. Example: "1" instead of "1 " for userNum = 1. I am just focusing on 1 for now and 0 and -1 later. Also the commented out while loop below just runs an infinite loop but I thought it would be useful to add it in anyways, sorry for the confusion. Here is my code so far, thank you very much in advance:

import java.util.Scanner;

public class DivideByTwo {
   public static void main (String [] args) {
      int userNum = 0;

      userNum = 20;

      System.out.print(userNum);

      while (userNum > 1) { 
         System.out.print(" " + (userNum/2));
         userNum = userNum/2;


      if (userNum == 1) {
         System.out.print(" ");
      }
   }
    /*  while (userNum <= 1) {
        System.out.print(" ");
        userNum = userNum + 1; 
      }*/

      System.out.println("");

      return;
   }
}

You can move this block of yours out of the while loop -

} // end of the while loop
if (userNum == 1) {
     System.out.print(" ");
}

Alternatively, you don't need the if there, while (userNum > 1) ensures that check is taken care of.


To make use of your existing code, change as --

while (userNum > 1) { 
    System.out.print(" " + (userNum/2));
    userNum = userNum/2;
}
System.out.println(" ");

After working on this beginner program for 5 days, I finally found the solution. It first checks if userNum is greater than or equal to 1 (so if it's -1 or 0 (-1/2, 0/2, nothing gets printed), then prints out the original userNum value (20, 1, etc..). It then adds this value to a space, the process repeats and it then prints the new userNum value (userNum/2). The while loop makes this process repeat until the value is no longer >= 1. It finally ends and prints a newline. Much simpler than my original thought:

while (userNum >= 1) {
        System.out.print(userNum + " ");
        userNum = userNum / 2;
     }

  System.out.println("");

  return;
public class DivideByTwo {
   public static void main (String [] args) {
      int userNum = 20;

      while (userNum >= 1) { 
         System.out.print(userNum+" ");
         userNum = userNum/2;
      }

      return;
   }
}

If you hard set the userNum to 20, there's no need to declare it and then assign a value, you can just do it in the same line.

So the change: If you change the while loop to check (userNum >= 1) the loop will include the number 1 in it's operations (If I'm not mistaken Java will round down integer devisions).

So what will happen is that it will see that userNum is larger or equal to 1, then print userNum and a space until the value is less than 1.

This should output the string you're after.

If however it does continually output a string of 1 1 1 1 1 1 1 1 at the end you can make use of the floor() function to make sure it rounds down.

This while loop should do the trick and it's pretty simple.

while(true){
    System.out.print(number + " ");
    number = number / 2;

    if(number==1){
        break;
    }
}

You're using the wrong kind of loop; a for loop is more sppropriate than a while loop. Your code can just be:

System.out.print(userNum)
for (;userNum > 1; userNum /= 2)
    System.out.print(" " + usernum);

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