Web Analytics Made Easy -
StatCounter continuous loop in my program - CodingForum

Announcement

Collapse
No announcement yet.

continuous loop in my program

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • continuous loop in my program

    Hi, When i click 1,2,3 or any other number in the menu, the program keeps looping back to the menu. It only works when I press 4 to exit the program. How do you get it so when the user presses 1/2/3, the prompt asks them to enter a number and it will be converted to the specific unit. Any suggestions would be greatly appreciated!



    import java.util.Scanner;

    public class Conversion {


    public static void main (String[] args) {
    int data;
    int meters;
    int inches;
    int feet;

    Scanner input = new Scanner (System.in);

    do {
    System.out.print (
    "Please enter choice for Conversion\nEnter 1 for meters\n Enter 2 for inches\nEnter 3 for feet\n Enter 4 to Exit\n");
    data = input.nextInt();

    if (data == 1) {
    System.out.println ( " Enter meters to be converted: " );
    }
    meters = input.nextInt ();


    if (data == 2)
    {
    System.out.print ( " Enter inches to be converted: " );
    }
    inches = input.nextInt ();

    if (data == 3)
    {
    System.out.print ( " Enter feet to be converted: " );
    }
    feet = input.nextInt ();

    if (data >= 4)
    {
    System.out.print ( " Error " );
    }

    } while (data != 4);
    System.out.print ( "Goodbye" );


    switch (data){

    case 1:
    System.out.println ( " \n meters to meters = " + (meters * 1056 ) );
    break;

    case 2:
    System.out.println ( " \n inches to meters = " + (inches * 4.184 ) );
    break;

    case 3:
    System.out.println ( " \n feet to meters= " + (feet * 1 ) );
    break;

    case 4:
    System.out.println("Goodbye");
    System.exit(0);

    default:
    System.out.println("Error, try again: ");
    data = input.nextInt();

    }


    }
    }

  • #2
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

    Ask a mod to move this over to the right forum.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

    Comment

    Working...
    X