Results 1 to 2 of 2
  1. #1
    DanPe is offline Member
    Join Date
    May 2012
    Rep Power

    Default Question: Loops, Positive ints etc...

    Hi again my friends,
    I encountered the following question:
    "Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, each followed by a space, and that terminates when it reads an integer that is not positive. Declare any variables that are needed."

    In order to solve the exercise I wrote the following code which seems to work. However, I was wondering if this really is the correct way to solve the question. If it's not, do you think it's an acceptable solution or would I get some points detracted for this code.
    Would love to hear your opinion. Thanks :)

    Java Code:
    import java.util.Scanner;
    public class copy {
        public static void main(String[] args)
            Scanner input = new Scanner(;
            while (true)
                int num = input.nextInt();
                if (num >= 100)
                    System.out.print(num + " ");
                else if (num >= 0 && num < 100)
            System.out.println("Program terminated");

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Question: Loops, Positive ints etc...

    I would do it like this:

    Java Code:
    while ((num= input.nextInt()) >= 0) 
       if (num >= 100)
          System.out.print(num+" ");
    (I left out the declaration part and the code doesn't anticipate for incorrect input).

    kind regarrds,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Question concerning loops
    By jim01 in forum New To Java
    Replies: 8
    Last Post: 04-13-2011, 03:17 PM
  2. Question about sentinal loops with strings
    By dienesh77 in forum New To Java
    Replies: 3
    Last Post: 04-06-2011, 01:57 AM
  3. Noob question about using ints in booleans
    By elfdreaming in forum New To Java
    Replies: 6
    Last Post: 03-14-2011, 11:07 AM
  4. Question about loops
    By SwEeTAcTioN in forum New To Java
    Replies: 1
    Last Post: 10-23-2009, 07:15 AM
  5. Question about loops
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 05:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts