Note that there are some explanatory texts on larger screens.

plurals
  1. POJava program using while loops changing it to use methods
    primarykey
    data
    text
    <p>I'm a first year computer science student and I'm running into some problems. We were asked to write a simple banking program using loops then asked to write the same program using methods. I'm currently stuck on changing it to a method program.</p> <p>Here is what I have for the loop program.</p> <p><pre><code>import java.util.Scanner;</p> <p>public class Prog6 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); String act = "MUSH"; double bal = 0.0; double inc = 0.0; while (act.charAt(0) != 'Q') { System.out.print((char) 27 + "[2J"); System.out.print("## IT\'S A BANK ##\nD - Deposit\nW - Withdraw\nI - Interest\nB - Balance\nQ - Quit\n\nAction:"); act = stdIn.next(); act = act.toUpperCase(); switch (act.charAt(0)) { case 'D': System.out.print((char) 27 + "[2J"); System.out.print("Deposit how much?:"); inc = stdIn.nextDouble(); while (inc &lt; 0) { System.out.print("Deposits must be non-negative. Please try again:"); inc = stdIn.nextDouble(); } bal += inc; break; case 'W': System.out.print((char) 27 + "[2J"); System.out.print("Withdraw how much?:"); inc = stdIn.nextDouble(); while (inc &lt; 0) { System.out.print("Withdrawalas must be non-negative. Please try again:"); inc = stdIn.nextDouble(); } while (inc > bal) { System.out.print("Insufficient funds. Please try a lower amount:"); inc = stdIn.nextDouble(); } bal -= inc; break; case 'I': System.out.print((char) 27 + "[2J"); inc = bal * .04; bal += inc; System.out.print("Interest accrued: $" + inc + "; press enter key to return to menu."); stdIn.nextLine(); stdIn.nextLine(); break; case 'B': System.out.print((char) 27 + "[2J"); System.out.print("Balance = $" + bal + "; press enter key to return to menu."); stdIn.nextLine(); stdIn.nextLine(); break; case 'Q': System.out.print((char) 27 + "[2J"); break; default: System.out.print("Invalid input, try again:"); break; } } } } </pre></code> I presume that I should make a method for <code>getbalence</code>, <code>withdraw</code>, <code>deposit</code>, <code>interest</code> but it's unclear to me how I should go about this... </p> <p>I apologize if I didn't include some other information that I should have.</p> <p>I really look forward to reading your responses!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload