简体   繁体   中英

Calling method but nothing is being returned java

After receving some help from another very helpful member I'm stuck at another point

import java.util.Scanner;

public class assn10
{
    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': 
           deposit(stdIn);
           break;
       case 'W': 
           withdraw(stdIn);
           break;
       case 'I':
           interest(stdIn);
           break;
       case 'B':
           balence(stdIn);
           break;
       }
   }   

   public static void deposit(Scanner stdIn) 
   {
       System.out.print((char)27 + "[2J");
       System.out.print("Deposit how much?:"); 
       inc = stdIn.nextDouble();

       while (inc < 0)
       { 
           System.out.print("Deposits must be non-negative. Please try again:");
           inc = stdIn.nextDouble();
       }

       bal += inc;
   }

   public static void withdraw(Scanner stdIn) 
   {
       System.out.print((char)27 + "[2J");
       System.out.print("Withdraw how much?:"); 
       inc = stdIn.nextDouble();

       while (inc < 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;
   }

   public static void interest(Scanner stdIn) 
   {
       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();
   }

   public static void balence(Scanner stdIn) 
   {    
       System.out.print((char)27 + "[2J");
       System.out.print("Balance = $" + bal + "; press enter key to return to menu."); 
       stdIn.nextLine(); 
       stdIn.nextLine();
   }
}

I'm thinking that I need to return a value back to the main argument? that would be received by the receive by the switch function?

Good lord, try this instead. See the difference?

import java.util.Scanner;

public class assn10
{
    static double bal = 0.0;
    static double inc = 0.0;


    public static void main(String[] args)
    {
       Scanner stdIn = new Scanner(System.in);
       String act = "MUSH";

       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':
               deposit(stdIn);
               break;
           case 'W':
               withdraw(stdIn);
               break;
           case 'I':
               interest(stdIn);
               break;
           case 'B':
               balance(stdIn);
               break;
           }
       }
   }

   public static void deposit(Scanner stdIn)
   {
       System.out.print((char)27 + "[2J");
       System.out.print("Deposit how much?:");
       inc = stdIn.nextDouble();

       while (inc < 0)
       {
           System.out.print("Deposits must be non-negative. Please try again:");
           inc = stdIn.nextDouble();
       }

       bal += inc;
   }

   public static void withdraw(Scanner stdIn)
   {
       System.out.print((char)27 + "[2J");
       System.out.print("Withdraw how much?:");
       inc = stdIn.nextDouble();

       while (inc < 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;
   }

   public static void interest(Scanner stdIn)
   {
       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();
   }

   public static void balance(Scanner stdIn)
   {
       System.out.print((char)27 + "[2J");
       System.out.print("Balance = $" + bal + "; press enter key to return to menu.");
       stdIn.nextLine();
       stdIn.nextLine();
   }
}

Why do you feel you need to return a value? Perhaps each method should just print its result.

You have a few major issues here. You need to think about the scope of your variables - eg inc and bal

This variable is local to the main() method and cannot be seen by the other methods (eg deposit())

You can fix this in a large number of ways, the simplest (and probably worst) is to make this variable a static class variable like so:

public class assn10
{
    private static double bal = 0.0;
    private static double inc = 0.0;

    public static void main(String[] args)
    {
       Scanner stdIn = new Scanner(System.in); 
       String act = "MUSH";
//.....

Then every method in your class can access the single class scoped variables bal and inc.

Also, your switch should probably be inside the while ( != 'Q' ) loop if you want to be able to do multiple operations in a single run (which I assume you do).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM