我正在为我的CS类开发一个涉及方法和变量的程序。我知道这是范围的问题,或者至少我认为,但不知道如何解决。我不能使用对象。代码尚未完成,因为我目前只专注于这一个问题。
import java.util.Scanner;
public class MethodBankingSystem {
Scanner input = new Scanner(System.in);
public static double Deposit(double userDeposit, double userBalance) {
System.out.print("Enter the amount you would like to deposit: ");
userDeposit = input.nextDouble(); //Create variable userDeposit for amount entered
userBalance = userBalance + userDeposit; //Replace value for userBalance
System.out.printf("New balance after deposit is: $" + "%.2f \n\n",userBalance );
return userBalance;
}
public static double Withdraw(double userWithdraw, double userBalance) {
System.out.print("Enter the amount you would like to withdraw: ");
userWithdraw = input.nextDouble(); //Create variable userWithdraw for amount entered
if (userWithdraw > userBalance) { //Create if/else statement to make sure enough funds are available
System.out.printf("Funds are not available for this withdraw \n\n");
}
else {
userBalance = userBalance - userWithdraw; //Replace value for userBalance
System.out.printf("New balance after withdraw is: $" + "%.2f \n\n",userBalance );
}
return userBalance;
}
public static void CurrentBalance(double userBalance) {
System.out.printf("Current balance is $" + "%.2f \n\n",userBalance );
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double userBalance = 100.00;
double userDeposit;
double userWithdraw;
double userInput;
String cardNumber;
String menu = "Welcome to Mustang Bank\nPlease choose from the options below:\n1 - Make a Deposit\n" +
"2 - Make a Withdraw\n3 - Check your Current Balance\n9 - Exit the Program\nEnter Option:\n";
do {
System.out.print(menu);
userInput = input.nextDouble();
switch (userInput) {
case '1':
Deposit(userDeposit, userBalance);
break;
case '2':
Withdraw(userWithdraw, userBalance);
break;
case '3':
CurrentBalance(userBalance);
break;
case '4':
CurrentBalance(userBalance);
break;
default:
System.out.println("This option is not acceptable.");
}
} while (userInput !=9);
}
}
答案 0 :(得分:0)
基本上问题是:
static
表示每个类只有一个该字段的实例(不是实例)。该字段基本上在所有实例之间共享;在一个实例中修改它将在所有实例中影响它。您可以在实例中引用静态字段,但不能静态引用实例成员。
要么input
静态(不是真的推荐):
static Scanner input = new Scanner(System.in);
或者创建一个MethodBankingSystem
的实例并从你的方法中删除静态修饰符(这是我认为你试图做的):
import java.util.Scanner;
public class MethodBankingSystem {
Scanner input = new Scanner(System.in);
public double Deposit(double userDeposit, double userBalance) {
System.out.print("Enter the amount you would like to deposit: ");
userDeposit = input.nextDouble(); //Create variable userDeposit for amount entered
userBalance = userBalance + userDeposit; //Replace value for userBalance
System.out.printf("New balance after deposit is: $" + "%.2f \n\n",userBalance );
return userBalance;
}
public double Withdraw(double userWithdraw, double userBalance) {
System.out.print("Enter the amount you would like to withdraw: ");
userWithdraw = input.nextDouble(); //Create variable userWithdraw for amount entered
if (userWithdraw > userBalance) { //Create if/else statement to make sure enough funds are available
System.out.printf("Funds are not available for this withdraw \n\n");
}
else {
userBalance = userBalance - userWithdraw; //Replace value for userBalance
System.out.printf("New balance after withdraw is: $" + "%.2f \n\n",userBalance );
}
return userBalance;
}
public void CurrentBalance(double userBalance) {
System.out.printf("Current balance is $" + "%.2f \n\n",userBalance );
}
public static void main(String[] args) {
new MethodBankingSystem();
}
MethodBankingSystem () {
Scanner input = new Scanner(System.in);
double userBalance = 100.00;
double userDeposit;
double userWithdraw;
double userInput;
String cardNumber;
String menu = "Welcome to Mustang Bank\nPlease choose from the options below:\n1 - Make a Deposit\n" +
"2 - Make a Withdraw\n3 - Check your Current Balance\n9 - Exit the Program\nEnter Option:\n";
do {
System.out.print(menu);
userInput = input.nextDouble();
switch (userInput) {
case '1':
Deposit(userDeposit, userBalance);
break;
case '2':
Withdraw(userWithdraw, userBalance);
break;
case '3':
CurrentBalance(userBalance);
break;
case '4':
CurrentBalance(userBalance);
break;
default:
System.out.println("This option is not acceptable.");
}
} while (userInput !=9);
}
}
我在修改问题之前修改了你的代码
因为我是一个好人,我修改了你的代码。有各种各样的问题,请阅读它们。你创建一个输入实例两次,你正在打开一个你正在调用存款的双重(userDeposit,userBalance);如果没有initialisinguserDeposit,你声明cardNumber但是从不实例化甚至尝试在任何地方使用它,你就会遇到静态作用域的问题。
import java.util.Scanner;
public class MethodBankingSystem {
public static void main(String[] args) {
new MethodBankingSystem();
}
Scanner input;
double userBalance = 100.00;
MethodBankingSystem() {
input = new Scanner(System.in);
int userInput;
String menu = "Welcome to Mustang Bank\nPlease choose from the options below:\n" + "1 - Make a Deposit\n"
+ "2 - Make a Withdraw\n" + "3 - Check your Current Balance\n"
+ "9 - Exit the Program\nEnter Option:\n";
do {
System.out.print(menu);
userInput = input.nextInt();
switch (userInput) {
case 1:
Deposit();
break;
case 2:
Withdraw();
break;
case 3:
CurrentBalance();
break;
case 9:
break;
default:
System.out.println("This option is not acceptable.");
}
} while (userInput != 9);
input.close();
}
public double Deposit() {
System.out.print("Enter the amount you would like to deposit: ");
double userDeposit = input.nextDouble(); // Create variable userDeposit for amount entered
userBalance = userBalance + userDeposit; // Replace value for userBalance
System.out.printf("New balance after deposit is: $" + "%.2f \n\n", userBalance);
return userBalance;
}
public double Withdraw() {
System.out.print("Enter the amount you would like to withdraw: ");
double userWithdraw = input.nextDouble(); // Create variable userWithdraw for amount entered
if (userWithdraw > userBalance) { // Create if/else statement to make sure enough funds are available
System.out.printf("Funds are not available for this withdraw \n\n");
} else {
userBalance = userBalance - userWithdraw; // Replace value for userBalance
System.out.printf("New balance after withdraw is: $" + "%.2f \n\n", userBalance);
}
return userBalance;
}
public void CurrentBalance() {
System.out.printf("Current balance is $" + "%.2f \n\n", userBalance);
}
}