3 Replies - 202 Views - Last Post: 16 February 2013 - 10:27 AM Rate Topic: -----

#1 starterkit123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-February 13

Conversion Program Works Except During First Try

Posted 16 February 2013 - 08:55 AM

It works everytime after the 1st choice is made. I was wondering why it doesn't work the first time though.
Output:
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program

Enter your choice: 1

1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program

Enter your choice: 1
6.0 meters is 0.006 kilometers
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program

Enter your choice: 2
6.0 meters is 236.21999999999997 inches
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program

Enter your choice: 3
6.0 meters is 19.686 feet
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program

Enter your choice: 4
Bye
BUILD SUCCESSFUL (total time: 8 seconds)

import java.util.Scanner;

public class ConversionProgram
{
    public static void main(String[] args) 
    {
        menu();
    }
/////////////////////////////////////////////////////////////////
    public static void menu()
    {
        Scanner keyboard = new Scanner (System.in);
        System.out.print("Please enter a distance in meters: ");
        double meters = keyboard.nextDouble();
        
        System.out.println("1. Convert to kilometers");
        System.out.println("2. Convert to inches");
        System.out.println("3. Convert to feet");
        System.out.println("4. Quit the program");
        
        System.out.print("\nEnter your choice: ");
        int menu = keyboard.nextInt();
        
       while( menu != 4)
        {
        System.out.println("\n1. Convert to kilometers");
        System.out.println("2. Convert to inches");
        System.out.println("3. Convert to feet");
        System.out.println("4. Quit the program");
        
        System.out.print("\nEnter your choice: ");
        menu = keyboard.nextInt();
        
        
            if( menu == 1 )
            {
                showKilometers(meters);
            }
            else if( menu == 2 )
            {
                showInches(meters);    
            }
            else if( menu == 3 )
            {
                showFeet(meters);      
            }
            else if( menu == 4 )
            {
                System.out.println("Bye");
                exitProgram();
            }
        
        }
    }
/////////////////////////////////////////////////////////////////
    public static void showKilometers(double meters)
    {
        double kilometers;
        
        kilometers = meters * 0.001;
        System.out.print(+ meters + " meters is " + kilometers + " kilometers" );
    }
/////////////////////////////////////////////////////////////////
    public static void showInches(double meters)
    {
        double inches;
        
        inches = meters * 39.37;
        System.out.print(+ meters + " meters is " + inches + " inches" );
    }
/////////////////////////////////////////////////////////////////
    public static void showFeet(double meters)
    {
        double feet;
        
        feet = meters * 3.281;
        System.out.print(+ meters + " meters is " + feet + " feet" );
    }
/////////////////////////////////////////////////////////////////
    public static void exitProgram()
    {
        System.exit(0);   
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Conversion Program Works Except During First Try

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Conversion Program Works Except During First Try

Posted 16 February 2013 - 09:16 AM

Please trace your program's logic and answer this question yourself. If unable, show your effort at tracing and explain where you're getting lost.
Was This Post Helpful? 0
  • +
  • -

#3 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Conversion Program Works Except During First Try

Posted 16 February 2013 - 10:09 AM

Try this

Spoiler

Was This Post Helpful? 1
  • +
  • -

#4 farrar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 2
  • Joined: 15-February 13

Re: Conversion Program Works Except During First Try

Posted 16 February 2013 - 10:27 AM

I probably shouldn't tell you exactly what the answer is, until you show that you have tried. However, I'll give you something to look at. The lines of code between 16-22 are exactly identical to the lines of code between 26-32. This isn't true 100% of the time, but often times if you have a lot of duplicate code, then there is probably a better way to go about your problem, that usually involves a method or a loop. You should probably think about a way to move everything that is going on in lines 16-22, into your while loop. Your code will look prettier and as you think about that more, I think, you will probably realize what your problem was, and also find that writing the prettier code will fix your problem.

Hope that helps. I apologize if I am a bit "wordy".
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1