Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Input to Decide Actions

Name: Anonymous 2013-02-18 14:52

Hey /prog/, I'm new and trying to write a program in java. I ask the user for input and then depending on the number they give me, I do an operation. Then I ask for a new number. 4 is the number to quit. Here's the relevant part of my code. It compiles, but doesn't work correctly.

System.out.println("Press 1 to make a deposit, 2 to make a withdrawal, 3 to check your balance, or 4 to quit.");
      order = scan.nextInt();
     
      while(order!=4)
{
      if(order==1)
      {
         double amount;
         System.out.println("How much would you like to deposit?");
         amount = scan.nextDouble();
         acc1.deposit(amount);
         acc1.display();
      }
      if(order==2)
      {
         double check;
         System.out.println("How much would you like to withdraw?");
         check = scan.nextDouble();
         acc1.check(check);
         acc1.display();
      }
      if(order==3)
      {
         acc1.display();
      }
      while(order!=4)
      {
         System.out.println("Please press 1 to make a deposit, 2 to make a withdrawal, 3 to check your balance, or 4 to quit.");
         order = scan.nextInt();
      }
      System.out.println("Press 1 to make a deposit, 2 to make a withdrawal, 3 to check your balance, or 4 to quit.");
      order = scan.nextInt();
}
System.out.println("Thank you for using our services.");

Name: Anonymous 2013-02-18 14:53

I know there's a mistake in what I use to make sure they input 1-4, but I don't know how to fix it.

Name: Anonymous 2013-02-18 15:11

Nevermind, I fixed it using if/else structures.

Name: Anonymous 2013-02-18 18:47

use switch cases and separate the code you're sticking in there as static methods or something. alternatively, you can just use System.exit(0); in case 4 and not worry about order if this is your primary interface.

order = 0;
while(order!=4) {
  System.out.println("Press 1 to make a deposit, 2 to make a withdrawal, 3 to check your balance, or 4 to quit.");
  switch((order = scan.nextInt()));
  {
    case 1:
      doOrder1();
      break;
    case 2:
      doOrder2();
      break;
    case 3:
      doOrder3(); 
      break;
    case 4:
      System.out.println("Thank you for using our services.");
      break;
  }
}

Name: Anonymous 2013-02-18 18:58

Nevermind, I'll kill myself.

Name: Anonymous 2013-02-18 20:05

switch + break is ugly if

Don't change these.
Name: Email:
Entire Thread Thread List