简体   繁体   中英

Extending my program with another while loop Java

Firstly, i'm very new to java, so excuse the mess of the code below.

My current program takes input from StdIn and prints out the highest and lowest values entered.

Can this be extended to ask the useר again if they entered an integer that isn't positive? I'm almost certain that a while loop can do the trick, but not sure if one executed with my current code. Once again, i'm very new and come from a music background, so my logical sense isn't the best.

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

do {
   StdOut.println("Enter your integers:");
} while (StdIn.isEmpty());

int max = StdIn.readInt();
int min = max;

while (!StdIn.isEmpty()) {
int value = StdIn.readInt();
if (value > max) max = value;
if (value < min) min = value;
}

do {
  StdOut.println("Maximum = " + max + ", Minimum = " + min);
  return;
} while (StdIn.readInt() > 0);


  }
}

Cheers

First of all you don't need this many loops in your program. Take the user input in while loop and then print that number outside of loop.

Remember to use try-catch for error handling for user input.

Try this way :

try{
       do {
            StdOut.println("Enter your integers:");
       } while (StdIn.isEmpty() && StdIn.readInt() < 0);
}catch(Exception ex){
      StdOut.println("Error while taking user input !");
}

You can do it by just adding a single if statement to check if the number given is negative, then print the message to add it again.

Check the ENHANCED code below:

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

   StdOut.println("Enter your integers:");

   int max = Integer.MIN_VALUE;
   int min = Integer.MAX_VALUE;

   while (!StdIn.isEmpty()) {

      int value = StdIn.readInt();

      // Adding the if-statement here to check if number is negative.
      if(value < 0){
         StdOut.println("You entered negative number, try positive numbers.");
         // just reset the max and min variables..
         max = Integer.MIN_VALUE;
         min = Integer.MAX_VALUE;
         continue;
      }

      if (value > max) max = value;
      if (value < min) min = value; 
   }

   StdOut.println("Maximum = " + max + ", Minimum = " + min);

   }
}

A quick answer to this would be

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

        do {
            StdOut.println("Enter your integers:");
        } while (StdIn.isEmpty());

        int max = StdIn.readInt();
        int min = max;

        while (!StdIn.isEmpty()) {
            int value = StdIn.readInt();
            if (value < 0) {
                StdOut.println("Please enter a positive integer");
            } else {
                if (value > max) max = value;
                if (value < min) min = value;
            }
        }

        do {
            StdOut.println("Maximum = " + max + ", Minimum = " + min);
            return;
        } while (StdIn.readInt() > 0);


    }
}

I made sure to change as little as possible, but this should give you the result you are looking for.

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