Results 1 to 5 of 5
  1. #1
    atac57 is offline Member
    Join Date
    Jan 2012
    Posts
    24
    Rep Power
    0

    Default Programming exercise help

    Write a program that calculates miles per gallon for a list of cars. The data for each car consists of initial odometer reading, final odometer reading, and number of gallons of gas. The user signals that there are no more cars by entering a negative initial odometer reading.

    Miles Per Gallon Program
    Initial miles:
    15000
    Final miles:
    15250
    Gallons
    10
    Miles per Gallon: 25.0

    Initial miles:
    107000
    Final miles:
    107450
    Gallons
    15
    Miles per Gallon: 30.0

    Initial miles:
    -1
    bye
    I'm having a couple problems with this exercise. How do you initialize the "first" variable when its suppose to be dependent on the user input which is what determines if the while loop executes (a negative number is suppose to stop the loop). If you initialize it before the loop with the Scanner variable, its gonna pointlessly have you enter the value after the "Miles Per Gallon Program" prompt? How do you get around that or am I completely off track? Thanks.

    Java Code:
    import java.util.Scanner;
    
    class Simulation{
    	public static void main (String args[]){
    		Scanner scan = new Scanner(System.in);
    		
    		int first, last, gallons, milesGallon;
    		
    		System.out.println("Miles Per Gallon Program");
    		
    		while ( first >= 0)
    		{
    			System.out.println("Initial miles: ");
    			first = scan.nextInt();
    			
    			System.out.println("Final miles: ");
    			last = scan.nextInt();
    			
    			System.out.println("Gallons: ");
    			gallons = scan.nextInt();
    			
    			milesGallon = (last - first)/ gallons;
    
    			System.out.println("Miles per Gallon: " + milesGallon);
    		}
    	}
    }

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Programming exercise help

    How do you initialize the "first" variable when its suppose to be dependent on the user input which is what determines if the while loop executes
    Ever hear of a do-while loop? Look it up.

  3. #3
    dharav is offline Member
    Join Date
    Dec 2011
    Posts
    28
    Rep Power
    0

    Default Re: Programming exercise help

    LOL Just reminded me of my olden days

  4. #4
    atac57 is offline Member
    Join Date
    Jan 2012
    Posts
    24
    Rep Power
    0

    Default Re: Programming exercise help

    Thanks, but I'm still having a problem. I want a negative integer entered once to kill the loop, but when I enter a negative it goes through the do loop and I have to enter a negative for every one of the variables to stop it. How can I fix that? Yes I know I'm a noob.

    Java Code:
    import java.util.Scanner;
     
    class Switch{
        public static void main (String args[]){
            Scanner scan = new Scanner(System.in);
             
            int first , last, gallons;
            double milesGallon;
             
            System.out.println("Miles Per Gallon Program");
            
            do
            {      
            	
                System.out.println("Initial miles: ");
                first = scan.nextInt();     
                
                System.out.println("Final miles: ");
                last = scan.nextInt();
                 
                System.out.println("Gallons: ");
                gallons = scan.nextInt();
                 
                milesGallon = (last - first)/ gallons;
     
                System.out.println("Miles per Gallon: " + milesGallon);
                System.out.println();
            }
            while ( first >= 0);
            {
            	if (first < 0)
            	{
            	System.out.println("bye");
            	}
            }
        }
    }

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Programming exercise help

    If you want it to terminate on the first entry of a negative number, you need to check each each number as it's entered and then if negative, you can add a break statement. Break will 'break' it out of the loop.

Similar Threads

  1. Having trouble with programming exercise, help?
    By atac57 in forum New To Java
    Replies: 1
    Last Post: 01-12-2012, 06:11 AM
  2. Programming exercise for beginners
    By FOX427 in forum New To Java
    Replies: 5
    Last Post: 07-16-2011, 07:43 AM
  3. Need help with a programming exercise
    By ararar in forum New To Java
    Replies: 9
    Last Post: 11-19-2010, 05:57 AM
  4. Have I done this exercise right?
    By ccie007 in forum New To Java
    Replies: 7
    Last Post: 09-28-2010, 05:54 PM
  5. I/O exercise
    By Feldom in forum New To Java
    Replies: 1
    Last Post: 10-28-2007, 04:48 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
  •