java - 需要添加某种数组并为另一个类添加传递参数和想法
问题描述
import java.util.Scanner;
public class atm{
private static Scanner in;
private static float balance = 0; // initial balance to 0 for everyone
private static int anotherTransaction;
public static void main(String args[]){
in = new Scanner(System.in);
// call our transaction method here
transaction();
}
public static void transaction(){
// here is where most of the work is
int choice;
System.out.println("Please select an option");
System.out.println("1. Withdraw");
System.out.println("2. Deposit");
System.out.println("3. Balance");
choice = in.nextInt();
switch(choice){
case 1:
float amount;
System.out.println("Please enter amount to withdraw: ");
amount = in.nextFloat();
if(amount > balance || amount == 0){
System.out.println("You have insufficient funds\n\n");
anotherTransaction(); // ask if they want another transaction
} else {
// they have some cash
// update balance
balance = balance - amount;
System.out.println("You have withdrawn "+amount+" and your new balance is "+balance+"\n");
anotherTransaction();
}
break;
case 2:
// option number 2 is depositing
float deposit;
System.out.println("Please enter amount you would wish to deposit: ");
deposit = in.nextFloat();
// update balance
balance = deposit + balance;
System.out.println("You have deposited "+deposit+" new balance is "+balance+"\n");
anotherTransaction();
break;
case 3:
// this option is to check balance
System.out.println("Your balance is "+balance+"\n");
anotherTransaction();
break;
default:
System.out.println("Invalid option:\n\n");
anotherTransaction();
break;
}
}
public static void anotherTransaction(){
System.out.println("Do you want another transaction?\n\nPress 1 for another transaction\n2 To exit");
anotherTransaction = in.nextInt();
if(anotherTransaction == 1){
transaction(); // call transaction method
} else if(anotherTransaction == 2){
System.out.println("Thanks for choosing us. Good Bye!");
} else {
System.out.println("Invalid choice\n\n");
anotherTransaction();
}
}
}
基本上我被告知了,因为我的代码中没有传递参数,我还需要一种向其中添加数组的方法,请帮助
我还需要关于添加另一个课程的建议,但没有想法
这是一个 atm 系统,如果有帮助,它会填充和打印
解决方案
您可以像这样声明一个arrayList(这是我将使用的,因为实用程序要简单得多);至于你应该为用户创建一个类,很大程度上取决于你想要为每个用户提供多少细节。
package main;
import java.util.ArrayList;
public class main {
ArrayList<Object> userList = new ArrayList<Object>();
public static void main(String[] args) {
// I'm not sure what your asking. but i think your asking how to declare a arrayList/array
}
}
推荐阅读
- java - 在 Mac 上使用 Windows Native L&F (Java/Swing)
- c++ - 一个最小的跨平台代码来打印这个对吗?
- typo3 - 如何在扩展中使用 TYPO3 Link 向导
- netsuite - 在套件表单上插入列表/记录字段
- jenkins-job-builder - 如何在工作级别覆盖 jenkins_job_builder 中的环境变量?
- gstreamer - Gstreamer 多播 RTP:音频中出现意外的不连续性
- java - Java 编译器如何为具有多个边界的参数化类型选择运行时类型?
- c# - 无法从 emgucv 中的视频中检测到人脸
- vba - VBA 查找和读取多个范围
- firebase - 如何在 Google Chrome 扩展程序的后台收听 Firebase 云消息传递消息?