简体   繁体   English

如何在 while 循环后打印 userNum = 1 并带有空格?

[英]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我的作业(来自 Zybooks 的 Java 课程介绍)说:“编写一个 while 循环,打印 userNum 除以 2(整数除法)直到达到 1。在每个数字后面加一个空格。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."注意:此活动将执行四次测试,分别是 userNum = 20,然后是 userNum = 1,然后是 userNum = 0,然后是 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.我可以让它打印“20 10 5 2 1”,但是当我成功地在上述数字序列的末尾添加一个空格时,当 userNum = 1 时它不会将空格添加到 1。例如:“1” userNum = 1 而不是“1”。我现在只关注 1,稍后关注 0 和 -1。 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.此外,下面注释掉的 while 循环只是运行一个无限循环,但我认为无论如何添加它会很有用,抱歉造成混乱。 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 -您可以将这个块移出while循环 -

} // 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.或者,您不需要if there, while (userNum > 1)确保检查得到处理。


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.在这个初学者程序上工作了 5 天后,我终于找到了解决方案。 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:它首先检查 userNum 是否大于或等于 1(所以如果它是 -1 或 0(-1/2、0/2,什么都不打印),然后打印出原始的 userNum 值(20、1 等...... 。然后将此值添加到一个空格中,重复该过程,然后打印新的 userNum 值(userNum/2)。while 循环使此过程重复,直到该值不再 >= 1。最后结束并打印换行。比我原来的想法简单得多:

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.如果您将 userNum 硬设置为 20,则无需声明它然后赋值,您只需在同一行中进行即可。

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).所以变化:如果你改变 while 循环来检查(userNum >= 1)循环将在它的操作中包含数字 1(如果我没记错的话,Java 会舍入整数除法)。

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.那么会发生的事情是它会看到userNum大于或等于1,然后打印userNum和一个空格,直到值小于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.但是,如果它在最后连续输出一串 1 1 1 1 1 1 1 1 ,您可以使用floor()函数来确保它向下舍入。

This while loop should do the trick and it's pretty simple.这个 while 循环应该可以解决问题,而且非常简单。

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. for循环比while循环更合适。 Your code can just be:您的代码可以是:

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

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

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