For this one I have to do the while and do-while plus the for loop. I have this bank/atm style program so the goal is to put the teller into a loop so that the user can deposit, withdraw, check the balance as long as they need to, and entering 3 to exit when they are done. I'm stuck, the book I have is very hard to understand and any help would be appreciated. Here's what I have so far:

package Banker;

import java.util.Scanner;

public class Main {

int choice;

public static void main(String[] args) {
// Create a scanner
Scanner input = new Scanner(System.in);
// User prompted to enter a choice
System.out.print("Please choose one of the following");
System.out.println ("1: Deposit " +
"2. Withdrawl " +
"3. Check Balance " +
"4. Exit ");
int choice = input.nextInt();
}
}

You want to put your choices in a while loop, so that they will continuously be displayed on the screen.

while(true){

// Your choices that you listed above go here. The way the program should exit is when the user inputs '4' to exit, you should say System.exit(0);
// You also need [I]methods[/I] that get called when the user makes certain choices. For example, the code below this line:

if (choice == 2){
//there was a withdrawal
withdrawal(personsBalance); // see below
}

}


public String withdrawal(int balance){
//Need code here, similar to Scanner code in main, that asks the user how much they want to withdraw. lets call it amount. Alternatively, you could ask the user back in main what amount they want to withdraw, then pass it as an argument to this method.
if (amount <= balance){
balance = balance - amount; // this will NOT really update your balance. to do that you'd have to create an Object, call the withdrawal method with it, and then say yourObject.balance = yourObject.balance - amount. OR you could use static variables (but I don't recommend that for this problem)
return "success";}else{
return "failure";
}


}

The above code is similar to what you want. I didn't check all cases because I'm in a rush right now, just popped into this thread. So don't get hung up on one little detail if you don't see it there, its just a general outline. Basically, you should be using method calls to handle each individual case. Another good idea would be to make a class called Person. Person would have the withdraw method in it as well as the other methods necessary to interact with the "virtual ATM". (Other methods being deposit, etc)


Sample person class:

public class Person{

private int myBalance = 0;

withdraw(){
//code to withdraw -- includes getting amount the person wants to withdraw,
// then subtracting this amount from their 'myBalance' variable, then 
//returning something which indicates how much they withdrew.
}

deposit(){
//code to deposit -- includes getting the amount they want to deposit, then adding it to their 'myBalance'
}

}

How your main would look after making a Person class:

public static void main(String[] args) {
// Create a scanner 
Scanner input = new Scanner(System.in);
// Create your person -- you need a Person constructor in the Person class,
//I didn't write one. You can always use the default. Also, note that you probably should write a method in Person that will allow you to change the balance. You could call it setBalance().
Person me = new Person();

while(true){
// User prompted to enter a choice
System.out.print("Please choose one of the following");
System.out.println ("1: Deposit " +
"2. Withdrawl " +
"3. Check Balance " +
"4. Exit ");
int choice = input.nextInt();

// 'switch' is similar to saying if (choice == 1) for case 1, if (choice == 2) for case 2, etc. So if choice is 1, it will run the code thats in case 1, and so on. The break; statement after each choice is necessary, I don't remember why, but it will not function properly without saying break;


switch(choice){
case 1:
me.whatever()
break;
case 2:
me.withdraw();
break;
case 3:
break;

}
}
}

Read your reply and tried to structure it as best as possible, could use some help eliminating its errors and placing the methods properly.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

      // Create a scanner
      Scanner input = new Scanner(System.in); 
      
double celsius; // degrees in Celsius
int choice; // 1 for F to C and 2 for C to F  

do{       
      System.out.print("Please choose one of the following");
      System.out.println (" by entering it's numeric value:  " +
      "1. Fahrenheit to Celsius  " +
      "2. Celsius to Fahrenheit  " +
      "3. Exit");
      // User prompted to enter a choice double fahrenheit; // degrees in Fahrenheit
      choice = input.nextInt(); 
          
double enteredTemp;// temperature asked from user
double convertedTemp;// temperature converted
      
      if (choice == 1){
        System.out.println("Enter Temperature in Farenheit : ");  
        // User prompted to enter temperature 
        enteredTemp = input.nextDouble();  
        convertedTemp = (5.0/9) * (enteredTemp - 32);       
        // Formula for F to C
        System.out.println("Degrees in celsius: ");  
        System.out.println(convertedTemp);}  
     
      else if (choice == 2){
        System.out.println("Enter Temperature in Celsius: " );  
        // User prompted to enter temperature 
        enteredTemp = input.nextDouble();              
        convertedTemp = (9.0/5)* enteredTemp + 32; 
        // Formula for C to F
        System.out.println("Degrees in Farenheit: ");
        System.out.println(convertedTemp);}  
    
} while(choice !=3);
      }
    }

Unless I've gone crazy, the first program you posted was a Bank program, and the second was a Temperature program. So you'll have to clarify which one you want help with and show any progress you've made as well as listing what errors you're getting and what concepts you need help with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.