Results 1 to 15 of 15
  1. #1
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default how to go about doing this?

    Write a Java program that will ask the user for a starting number, and ending number, and an increment number. The program will then count upward, from the starting number up to and including the ending number, by the increment number, displaying each number on a separate line. Also, for each displayed number, the program will say whether the number is even or odd. This program will require the use of the “while” loop as well as the “if/else” statement. Examine the sample outputs carefully in order to structure your code in a logical manner.

    I can do all the coding, I just have no idea how to use the mix of loop and if/else to get it done. Can anyone help me out with that.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    Sure Can you show that you wrote?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    here is the psudo code for what I'm thinking it should look like

    [code]
    while (number < end)
    if even
    {
    print number even
    print number
    number + increment
    }
    else
    {
    print number odd
    print number
    number + increment
    }

  4. #4
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    not sure how to tell if the number is even or odd and where to put the print out statement that the number is even or odd with out it being in the loop as well

  5. #5
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    okay, each number it prints out, it also has to say if the number is even or odd.

  6. #6
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    It is really easy.
    see
    Java Code:
    package action;
    
    public class EvenOrOdd {
        public static void main(String[] arg) {
            for (int i=0;i!=10;++i) {
                boolean isEven = i % 2 == 0;
                if (isEven) {
                    System.out.println("Number " + i + " is even");
                } else {
                    System.out.println("Number " + i + " is odd");
                }
            }
        }
    }
    Skype: petrarsentev
    http://TrackStudio.com

  7. #7
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    here is the example we are given


    Please enter a starting number: 3
    Please enter an ending number: 19
    Please enter an increment number: 3
    The number 3 is odd
    The number 6 is even
    The number 9 is odd
    The number 12 is even
    The number 15 is odd
    The number 18 is even
    Thanks for playing!

  8. #8
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    oh crap, your right, I completly forgot to think of boolean

  9. #9
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    I gave the wrong pseudo code, I need to format it to look like the example

  10. #10
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    So Have you solved this problem?
    Skype: petrarsentev
    http://TrackStudio.com

  11. #11
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    this is what I have so far.

    Java Code:
    import java.ulti.Scanner
    
    public class Lab7
    {
    	public static void main(String [] args)
    	{
    		int start;
    		int end;
    		int increment;
    		
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("Enter a starting number");
    		start = keyboard.nextInt();
    		System.out.println("Enter a ending number");
    		end = keyboard.nextInt();
    		System.out.println("Enter an increment number");
    		increment = keyboard.nextInt();
    		while (int i=0;i!=10;++i)
            	{
            		boolean even = i % 2 == 0;
    			if (Even)
    	         	   {
    	         	       System.out.println("Number " + start + " is even");
    	         	   }
    	        else 
    	          	  { 
    	          	      System.out.println("Number " + sstart + " is odd");
    	          	  }
    	            
    	          	  ++i
    			}   
    	    	system.out.println("Thank you for playing!")
    	}
    }
    mind letting me know if I'm on the right track?

  12. #12
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    sry double post
    Last edited by jjth39347; 03-16-2011 at 02:50 PM. Reason: double post

  13. #13
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

    Default

    okay I think I almost got it. but it only shows the start number, anyone care to tell me why?

    Java Code:
    import java.util.Scanner;
    
    public class Lab7
    {
    	public static void main(String [] args)
    	{
    		int start;
    		int end;
    		int increment;
    
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("Enter a starting number");
    		start = keyboard.nextInt();
    		System.out.println("Enter a ending number");
    		end = keyboard.nextInt();
    		System.out.println("Enter an increment number");
    		increment = keyboard.nextInt();
    		while (start==0);
            	{
            		boolean even = start % 2 == 0;
    			if (even)
    	         	   {
    					   while (start==0);
    	         	       System.out.println("Number " + start + " is even");
    	         	       start = start + increment;
    	         	   }
    	        else
    	          	  {
    					  while (start==0);
    	          	      System.out.println("Number " + start + " is odd");
    	          	      start = start + increment;
    	          	  }
    
    
    			}
    	    	System.out.println("Thank you for playing!");
    	}
    }

  14. #14
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    see
    Java Code:
    import java.util.Scanner;
    
    public class EvenOrOdd {
        public static void main(String[] arg) {
            int start;
            int end;
            int increment;
    
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter a starting number");
            start = keyboard.nextInt();
            System.out.println("Enter a ending number");
            end = keyboard.nextInt();
            System.out.println("Enter an increment number");
            increment = keyboard.nextInt();
            if (start > end || increment <= 0) {
                throw new IllegalArgumentException("start < end || increment <= 0");
            }
            while (start < end) {
                boolean even = start % 2 == 0;
                if (even) {
                    System.out.println("Number " + start + " is even");
                } else {
                    System.out.println("Number " + start + " is odd");
                }
                start += increment;
            }
            System.out.println("Thank you for playing!");
        }
    }
    Skype: petrarsentev
    http://TrackStudio.com

  15. #15
    jjth39347 is offline Member
    Join Date
    Feb 2011
    Posts
    71
    Rep Power
    0

Posting Permissions

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