简体   繁体   English

如何使用Java中的嵌套循环仅打印一次数字?

[英]How to print single number only once using nested loops in Java?

Everything runs fine in my Java code except at the very end of the code.除了在代码的最后,我的 Java 代码中的一切都运行良好。 So basically I can't figure out how to print out the User's Number if it is the same.所以基本上我不知道如何打印出相同的用户编号。 For example I am prompt the User for a starting number and an ending number (integers).例如,我提示用户输入起始号码和结束号码(整数)。 So say the user enters in the same integer "10" for starting number and "10" for ending number.因此,假设用户输入相同的整数“10”作为起始号码和“10”作为结束号码。 I want the output to only be "10" to be printed only just once.我希望输出只打印一次“10”。 I've tried everything I can think of with trying While Loop, Do-While Loop, and For Loops but I just can't figure it out?我已经尝试了所有我能想到的方法,包括 While 循环、Do-While 循环和 For 循环,但我就是想不通?

------------------------JAVA CODE BELOW------------------------------------------- ------------------------Java代码如下------------------------ --------------------

import java.util.Scanner;

public class LoopsAssignment {
   public static void main(String[] args) {

      // input Scanner
      Scanner input = new Scanner(System.in);
      // ask user for a starting number and a ending number
      System.out.println("Now I'll print whatever numbers you'd like!");
      System.out.println("Give me a starting number: ");
      startNum = input.nextInt();
      System.out.println("Give me an ending number: ");
      endNum = input.nextInt();

      // count the users range of numbers
      System.out.println("I counted your range of numbers: ");  
      int a = startNum;
      int b = endNum;

      while (a <= b) {
         System.out.println(a);
         a = a + 1;
      }
         while (a >= b) {
            System.out.println(a);
            a = a - 1;
         }
            while (a == b) {
               System.out.println(a); 
            }    

   }
}

---------------------OUT PUT BELOW ----------------------------------------------------- ---------------------在下面放置 -------------------------- ---------------------------

Now I'll print whatever numbers you'd like!现在我会打印你想要的任何数字! Give me a starting number: 10 Give me an ending number: 10 I counted your range of numbers: 10 11 10给我一个起始号码:10 给我一个结束号码:10 我数了你的号码范围:10 11 10

----jGRASP: operation complete. ----jGRASP:操作完成。

You could restructure your code as follows:您可以按如下方式重构代码:

  while (a < b) {
     System.out.println(a);
     a = a + 1;
  }

  while (a > b) {
     System.out.println(a);
     a = a - 1;
  }

  if (a == b) {
     System.out.println(a); 
  }

You can use for loop :您可以使用for loop

public static void printRange(int minInclusive, int maxInclusive) {
    for (; minInclusive <= maxInclusive; minInclusive++)
        System.out.println(minInclusive);
}

So you are either counting up, down or there's just one.所以你要么在计数,要么在计数,要么只有一个。

So所以

int step = endNum>startNum ? +1 : -1;
int a = startNum;
int b = endNum;
while (a != b) {
    System.out.println(a);
    a = a + step;
}
System.out.println(b);

Or put a break in the middle of a for loop.或者在for循环中间放置一个break Also there's += , and a few things we can make more conventional.还有+= ,还有一些我们可以做的更传统的事情。

int step = endNum>startNum ? +1 : -1;

for (int i=startNum; ; i+=step) {
    System.out.println(i);
    if (i == endNum) {
        break;
    }
}

The issue is in your first two while loops where you are using " >= " and " <= ".问题出在您使用“ >= ”和“ <= ”的前两个while循环中。 You can remove "=" from the condition.您可以从条件中删除"="

However you can improve your code as suggested in other comments.但是,您可以按照其他评论中的建议改进您的代码。

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

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