使用 while 循环的 Java 程序将其更改为使用方法

发布于 2024-10-07 16:31:37 字数 2551 浏览 1 评论 0原文

我是计算机科学专业一年级的学生,我遇到了一些问题。我们被要求使用循环编写一个简单的银行程序,然后要求使用方法编写相同的程序。我目前正坚持将其更改为方法程序。

这是我的循环程序。

import java.util.Scanner;

公开课Prog6 { 公共静态无效主(字符串[]参数) { 扫描仪 stdIn = new Scanner(System.in); 字符串行为=“MUSH”; 双平衡 = 0.0; 双精度 = 0.0; while (act.charAt(0) != 'Q') { System.out.print((char) 27 + "[2J"); System.out.print("## IT\'SA BANK ##\nD - 存款\nW - 提款\nI - 利息\nB - 余额\nQ - 退出\n\n操作:"); act = stdIn.next(); act = act.toUpperCase(); 开关 (act.charAt(0)) { 案例“D”: System.out.print((char) 27 + "[2J"); System.out.print("存款多少?:"); inc = stdIn.nextDouble(); 而(包括 < 0) { System.out.print("充值必须为非负数,请重试:"); inc = stdIn.nextDouble(); } bal += 公司; 休息; 案例“W”: System.out.print((char) 27 + "[2J"); System.out.print("提现多少?:"); inc = stdIn.nextDouble(); 而(包括 < 0) { System.out.print("提现金额必须为非负数,请重试:"); inc = stdIn.nextDouble(); } while (inc > bal) { System.out.print("资金不足,请尝试较低的金额:"); inc = stdIn.nextDouble(); } bal -= 公司; 休息; 案例“我”: System.out.print((char) 27 + "[2J"); inc = bal * .04; bal += 公司; System.out.print("应计利息:$" + inc + "; 按 Enter 键返回菜单。"); stdIn.nextLine(); stdIn.nextLine(); 休息; 情况“B”: System.out.print((char) 27 + "[2J"); System.out.print("余额 = $" + bal + "; 按 Enter 键返回菜单。"); stdIn.nextLine(); stdIn.nextLine(); 休息; 案例“Q”: System.out.print((char) 27 + "[2J"); 休息; 默认: System.out.print("输入错误,请重试:"); 休息; } } } }

I presume that I should make a method for getbalence, withdraw, deposit, interest but it's unclear to me how I should go about this...

如果我没有提供一些我应该提供的其他信息,我深表歉意。

我真的很期待阅读您的回复!

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.

Here is what I have for the loop program.

import java.util.Scanner;

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


I presume that I should make a method for getbalence, withdraw, deposit, interest
but it's unclear to me how I should go about this...

I apologize if I didn't include some other information that I should have.

I really look forward to reading your responses!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晰传感 2024-10-14 16:31:37

好的,我将从

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

你的 switch 语句开始,它不会像这样。

switch (act.charAt(0)){
  case 'D': 
     deposit(stdIn);
     break;
  case 'W': 
     // the rest of your code

我所做的,是采用一个完整的、可重用的函数,并从中创建一个方法。代码位于 switch 语句中的位置,我已将其替换为对该方法的调用。这使得代码更容易阅读、调试维护等,因为一切都很好地分开了。

您应该能够自己完成申请的其余部分。

我还想说,从主方法中完成所有操作并调用静态方法是不好的做法。最好创建对象来代表您的银行,但我只是扩展了您已经拥有的对象,并且不想一开始就让您变得过于复杂。

Okay, I'll start you off

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

your switch statement will not look like this

switch (act.charAt(0)){
  case 'D': 
     deposit(stdIn);
     break;
  case 'W': 
     // the rest of your code

What I have done, is to take a complete, re-usable function, and made a method out of it. And where the code was in your switch statement, I have replaced it with a call to that method. This makes the code easier to read, debug maintain etc, because everything is nicely separated.

You should be able to finish the rest of the application yourself.

I would also say, that doing it all from a main method and calling static methods is bad practice. It would be better to create objects to represent your Bank, but I have simply extended what you have already got, and did not want to overcomplicate for you at the first instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文