调用方法但没有返回java

时间:2010-12-12 18:08:45

标签: java methods

在从另一位非常有帮助的成员那里得到一些帮助后,我被困在另一个地方

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();
   }
}

我在想我需要将一个值返回给主参数?这将由切换功能接收?

3 个答案:

答案 0 :(得分:1)

好主人,试试吧。看到区别?

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();
   }
}

答案 1 :(得分:0)

为什么你觉得你需要返回一个值?也许每种方法都应该打印出结果。

答案 2 :(得分:0)

这里有几个主要问题。 您需要考虑变量的范围 - 例如inc和bal

此变量是main()方法的本地变量,其他方法无法看到(例如deposit())

您可以通过多种方式解决此问题,最简单的(也可能是最差的)是将此变量设置为静态类变量,如下所示:

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";
//.....

然后,类中的每个方法都可以访问单个类范围的变量bal和inc。

此外,如果你想在一次运行中做多个操作(我假设你这样做),你的开关应该在while(!='Q')循环内。