Java 哈维-惠尔循环

Java 哈维-惠尔循环,java,Java,Java初学者。使用书籍。对我来说,这个软件教的不好,它很模糊。 问题是: 给定正整数numInsects,编写一个while循环,在不达到200的情况下打印该数字的两倍。在每个数字后面加一个空格。循环结束后,打印换行符。例如:如果numInsects=16,则打印: 163264128 以下是我所拥有的: import java.util.Scanner; public class InsectGrowth { public static void main (String [] ar

Java初学者。使用书籍。对我来说,这个软件教的不好,它很模糊。 问题是:

给定正整数numInsects,编写一个while循环,在不达到200的情况下打印该数字的两倍。在每个数字后面加一个空格。循环结束后,打印换行符。例如:如果numInsects=16,则打印:

163264128

以下是我所拥有的:

import java.util.Scanner;

public class InsectGrowth {
   public static void main (String [] args) {
      int numInsects;
      Scanner scnr = new Scanner(System.in);
      numInsects = scnr.nextInt(); // Must be >= 1

       System.out.print(numInsects + " ");

  while (numInsects <= 100) {
     numInsects = numInsects * 2;
     System.out.print(numInsects + " ");
  }

  System.out.println();

   }
}
import java.util.Scanner;
公共类昆虫生长{
公共静态void main(字符串[]args){
国际货币区;
扫描仪scnr=新扫描仪(System.in);
numInsects=scnr.nextInt();//必须大于等于1
系统输出打印(numInsects+“”);

while(numInsects这是处理边缘情况场景的问题。如您所见,您的
while
循环通过一次迭代超出了边界条件。因此您只需调整条件

while
循环替换为以下代码块:

     if(numInsects <= 200) {
         System.out.print(numInsects);
     }
     while (numInsects <= 100) {
         numInsects = numInsects * 2;
         System.out.print(" " + numInsects);
     }

if(numInsects将第一次打印移动到while循环中并检查200

public class InsectGrowth {
   public static void main (String [] args) {
      int numInsects;
      Scanner scnr = new Scanner(System.in);
      numInsects = scnr.nextInt(); // Must be >= 1

      while (numInsects < 200) {
          System.out.print(numInsects + " ");
         numInsects = numInsects * 2;
      }

      System.out.println();

   }
}
公共类不安全增长{
公共静态void main(字符串[]args){
国际货币区;
扫描仪scnr=新扫描仪(System.in);
numInsects=scnr.nextInt();//必须大于等于1
while(numInsects<200){
系统输出打印(numInsects+“”);
numInsects=numInsects*2;
}
System.out.println();
}
}

为了强调以你更清楚的方式去做,并且因为你在学习,有多种吃鱼的方法,很高兴你会遇到每个人都在做不同的事情。

你的while循环看到了numInsects的当前值,而没有考虑实际应用的是什么

示例:

import java.util.Scanner;

public class ex {

    public static void main(String[] args) {
        int numInsects;
        Scanner scnr = new Scanner(System.in);
        numInsects = scnr.nextInt(); // Must be >= 1

        System.out.print(numInsects + " ");
        /*
          is  16 less than 200? yes
          is  32  less than 200? yes
          is  64 less than 200? yes
          is  128 less than 200? yes
          is  256 less than 200? no, break

          but if
          is  16 *2  less than 200? yes
          is  32  *2  less than 200? yes
          is  64 *2  less than 200? yes
          is  128  *2 less than 200? no, break
        */

        while (numInsects * 2 < 200) {
            numInsects = numInsects * 2;
            System.out.print(numInsects + " ");
        }

        System.out.println();

    }
}
import java.util.Scanner;
公共类{
公共静态void main(字符串[]args){
国际货币区;
扫描仪scnr=新扫描仪(System.in);
numInsects=scnr.nextInt();//必须大于等于1
系统输出打印(numInsects+“”);
/*
16比200小吗?是的
32比200小吗?是的
64比200小吗?是的
128比200小吗?是的
256小于200吗?不,中断
但如果
16*2是否小于200?是的
32*2是否小于200?是的
64*2是否小于200?是
128*2是否小于200?不,中断
*/
而(numInsects*2<200){
numInsects=numInsects*2;
系统输出打印(numInsects+“”);
}
System.out.println();
}
}

您的numInsects将始终为0,您应该说int numInsects=1; 因为您正在这样做:

0*2=0 0*2=0 0 0 0 0
是的,在一个部分中,200是应该的。然后在那之前,他说“…不到200”不仅如此,这是照片上的照片loop@NiteRain还有pcoates,谢谢大家的评论。现在查看编辑。你还需要<200,因为问题是它正在打印出200谢谢大家!我对此感到非常失望,以至于我忘记了,并且忽略了我有系统。输出。打印(numInsects+“”);当我试图修复其他部件时,在那里。效果很好!这学期是我第一次处理Java。事实上,直到去年秋季学期,我还没有编写过任何代码。(我是个老屁),我会有更多的问题,因为我觉得我们的在线书籍非常糟糕。你的问题是循环开始前的打印。你只想在numInsects<200时,将其放入循环中。
import java.util.Scanner;

public class ex {

    public static void main(String[] args) {
        int numInsects;
        Scanner scnr = new Scanner(System.in);
        numInsects = scnr.nextInt(); // Must be >= 1

        System.out.print(numInsects + " ");
        /*
          is  16 less than 200? yes
          is  32  less than 200? yes
          is  64 less than 200? yes
          is  128 less than 200? yes
          is  256 less than 200? no, break

          but if
          is  16 *2  less than 200? yes
          is  32  *2  less than 200? yes
          is  64 *2  less than 200? yes
          is  128  *2 less than 200? no, break
        */

        while (numInsects * 2 < 200) {
            numInsects = numInsects * 2;
            System.out.print(numInsects + " ");
        }

        System.out.println();

    }
}