调用方法但没有返回 java
在从另一个非常有帮助的成员那里获得一些帮助后,我陷入了另一个困境,
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();
}
}
我在想我需要将一个值返回到主要参数?开关功能的接收会收到什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
上帝啊,试试这个吧。看到区别了吗?
Good lord, try this instead. See the difference?
为什么你觉得你需要返回一个值?也许每个方法都应该只打印其结果。
Why do you feel you need to return a value? Perhaps each method should just print its result.
这里有几个主要问题。
您需要考虑变量的范围 - 例如 inc 和 bal
该变量是 main() 方法的本地变量,其他方法无法看到(例如 Deposit())
您可以通过多种方式修复此问题,最简单(也可能是最糟糕)的方法是使该变量成为静态类变量,如下所示:
然后类中的每个方法都可以访问单类作用域变量 bal 和 inc。
另外,如果您希望能够在一次运行中执行多个操作(我假设您这样做),那么您的开关可能应该位于 while ( != 'Q' ) 循环内。
You have a few major issues here.
You need to think about the scope of your variables - e.g. inc and bal
This variable is local to the main() method and cannot be seen by the other methods (e.g. 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:
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).