Solved

using "while" loop to output all odd numbers

Posted on 2003-10-27
4
1,033 Views
Last Modified: 2007-12-19
Hi:
I'm trying to use a while loop to prompt the user to input 2 integers: firstNum & secondNum (firstNum must be less than secondNum).
well, this step is easy for me,
but, i'm having a little bit problem getting the right equation of outputting all odd numbers between these 2 integers.
Here's my codes:

import java.io.*;
import java.util.*;

public class WhileLoops
{
      static BufferedReader keyboard =
                        new BufferedReader(new InputStreamReader(System.in));
      
      public static void main(String[] args) throws IOException
      {
            int firstNum, secondNum, n;
            int rem;
            
            n = 0;
            
            System.out.print("please enter an integer: ");
            System.out.flush();
            firstNum = Integer.parseInt(keyboard.readLine());
            
            System.out.print("please enter another one that is larger than the first integer: ");
            System.out.flush();
            secondNum = Integer.parseInt(keyboard.readLine());
            
            rem = firstNum % 2;
            
            while(firstNum < (secondNum - 2))
            {
                  if(rem == 0)
                  {
                        firstNum = firstNum + (2*n + 1);
                        System.out.print(firstNum + " ");
                  }
                  else
                  {
                        firstNum = firstNum + 2;
                        System.out.print(firstNum + " ");
                        
                  }
            }
      }
}


If i input 3 and 17, the output is "5 7 9 11 13 15", which is correct.
but, it doesn't work when i input 8 and 15, the output becomes "9 10 11 12 13"

I'm not sure which part i have to fix.
0
Comment
Question by:crystalfish0318
  • 2
4 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 9625978
Split mind?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 20 total points
ID: 9625993
Your code is unnecessarily complex. This should work:

          while (++firstNum < secondNum) {
                if (firstNum % 2 != 0)
                      System.out.println(firstNum);
          }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9626067
:-)
0
 

Author Comment

by:crystalfish0318
ID: 9632633
import java.io.*;
import java.util.*;

public class ch5_num8_partB_OddNumbers
{
      static BufferedReader keyboard =
                        new BufferedReader(new InputStreamReader(System.in));
      
      public static void main(String[] args) throws IOException
      {
            int firstNum, secondNum, n;
            int rem;
            
            n = 0;
            
            System.out.print("Please enter the first integer: ");
            System.out.flush();
            firstNum = Integer.parseInt(keyboard.readLine());
            
            System.out.print("Please enter the second integer that is larger than "
                                 + "the first integer: ");
            System.out.flush();
            secondNum = Integer.parseInt(keyboard.readLine());
            
            rem = firstNum % 2;
            
             while (++firstNum < secondNum)
             {
                   if (firstNum % 2 != 0)
                   System.out.println(firstNum);
             }

      }
}

Sorry, how do i make the output horizontal, not vertical?
Another problem is:
if i input 3 and 17,
i want the output to be:
"The odd numbers between 3 and 17 are:
 5 7 9 11 15"

how do i do this?
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question