简体   繁体   中英

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. 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. I want the output to only be "10" to be printed only just once. 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?

------------------------JAVA CODE BELOW-------------------------------------------

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

----jGRASP: operation complete.

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 :

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. 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 " <= ". You can remove "=" from the condition.

However you can improve your code as suggested in other comments.

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