为 Java ATM 项目切换内部 If 语句
Switch with If Statement Inside for Java ATM Project
现在我正在做一个 ATM
项目。
用户将输入他们的姓名、余额、交易类型和交易金额。交易类型有Withdraw (W)
、Deposit (D)
和R (Report)
,我用的是switch
,有3个cases
W
,D
, R
.如果您出现以下情况,程序将输出错误:输入错误的代码,您没有足够的资金来进行您选择的交易类型,并且您输入的金额为负数。现在我很难尝试设置 switch
。我的代码在下面,任何帮助将不胜感激!谢谢
import java.io.*;
import java.util.*;
public class atmValidator {
public static void main(String[] args) {
String fname = " ", lname = " ";
double balbefore = 0.0, balafter = 0.0, transamount = 0.0;
char code = ' ';
Scanner screen = null;
FileInputStream fis = null;
FileOutputStream fos = null;
PrintWriter pw = null;
//Declaring the variables/scanner/etc.
try {
fis = new FileInputStream ("transactions.txt");
fos = new FileOutputStream ("statement.txt");
screen = new Scanner (fis);
pw = new PrintWriter (fos);
} catch (FileNotFoundException e) {
System.out.println("File not found");
System.exit(0);
}
//The try/catch for reading from the file
while (screen.hasNext()) {
fname = screen.next();
lname = screen.next();
balbefore = screen.nextDouble();
code = screen.next().charAt(0);
transamount = screen.nextInt();
//A while loop to gather the info from the file, and the variables getting their values from the files.
switch (code) {
case 'D':
balafter = balbefore + transamount;
if (transamount <0) {
pw.println("ERROR: Enter positive amount.");
}
else if (balafter < 300) {
pw.println("Warning, balance below 0");
}
{
// Where I am stuck at :(
}
}
}
screen.close();
pw.close();
}
}
//Sorry for the weird braces and spaces
你就快完成了!在每个案例之后,您需要跟进 break;
并进入下一个 case
。在最后,您需要有一个 default: break
,转换为 "If the variable doesn't fit any of the cases, then break out of the switch"。
所以 switch 的工作原理如下:
switch(code) {
case 'D':
// do deposit code
break;
case 'W':
// do withdrawl code
break;
...
default:
break;
}
Switch 具有不同的语法,因为它没有花括号来定义代码块。它宁愿寻找 break;
和随后的 case:
或 default:
继续前进。
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html 有很好的例子供您学习。
评论后编辑:
为了回复您的评论,您需要执行以下操作:
case 'W':
tempBal = balbefore - transamount;
if(tempBal < 0) {
System.out.println("Insufficient Funds");
}
else {
balafter = balbefore - transamount;
if(balafter < 300) {
System.out.println("Warning, balance below 0");
}
}
break;
...
default:
System.out.println("Please select "D", "W", or "R");
break;
可以在switch body里面写case如下。你可以进一步延长条件,但在这里我只是给你一个如何做的例子。如果字符不是 D、W 或 R,则执行默认大小写。
switch (code) {
case 'D':
balafter = balbefore + transamount;
System.out.println("");
if (balafter < 0) {
pw.println("Insufficent funds");
} else if (balafter < 300) {
pw.println("Warning, balance below 0");
}else{
pw.println("Well. You have now " + balafter + "$");
}
break;
case 'W':
balafter = balbefore - transamount;
if (transamount > balbefore) {
pw.println("Sorry. You don't enough funds.");
} else if (transamount <= balbefore && balafter < 300) {
pw.println("Warning, balance below 0");
} else {
pw.println("The withdrawl was successful.");
}
break;
case 'R':
balafter = balbefore + transamount;
if (balafter < 0) {
pw.println("Insufficent funds");
} else if (balafter < 300) {
pw.println("Warning, balance below 0");
} else {
pw.println("Good. Your balance is " + balAfter);
}
break;
default:
System.out.println("Nothing to do.");
break;
}
现在我正在做一个 ATM
项目。
用户将输入他们的姓名、余额、交易类型和交易金额。交易类型有Withdraw (W)
、Deposit (D)
和R (Report)
,我用的是switch
,有3个cases
W
,D
, R
.如果您出现以下情况,程序将输出错误:输入错误的代码,您没有足够的资金来进行您选择的交易类型,并且您输入的金额为负数。现在我很难尝试设置 switch
。我的代码在下面,任何帮助将不胜感激!谢谢
import java.io.*;
import java.util.*;
public class atmValidator {
public static void main(String[] args) {
String fname = " ", lname = " ";
double balbefore = 0.0, balafter = 0.0, transamount = 0.0;
char code = ' ';
Scanner screen = null;
FileInputStream fis = null;
FileOutputStream fos = null;
PrintWriter pw = null;
//Declaring the variables/scanner/etc.
try {
fis = new FileInputStream ("transactions.txt");
fos = new FileOutputStream ("statement.txt");
screen = new Scanner (fis);
pw = new PrintWriter (fos);
} catch (FileNotFoundException e) {
System.out.println("File not found");
System.exit(0);
}
//The try/catch for reading from the file
while (screen.hasNext()) {
fname = screen.next();
lname = screen.next();
balbefore = screen.nextDouble();
code = screen.next().charAt(0);
transamount = screen.nextInt();
//A while loop to gather the info from the file, and the variables getting their values from the files.
switch (code) {
case 'D':
balafter = balbefore + transamount;
if (transamount <0) {
pw.println("ERROR: Enter positive amount.");
}
else if (balafter < 300) {
pw.println("Warning, balance below 0");
}
{
// Where I am stuck at :(
}
}
}
screen.close();
pw.close();
}
}
//Sorry for the weird braces and spaces
你就快完成了!在每个案例之后,您需要跟进 break;
并进入下一个 case
。在最后,您需要有一个 default: break
,转换为 "If the variable doesn't fit any of the cases, then break out of the switch"。
所以 switch 的工作原理如下:
switch(code) {
case 'D':
// do deposit code
break;
case 'W':
// do withdrawl code
break;
...
default:
break;
}
Switch 具有不同的语法,因为它没有花括号来定义代码块。它宁愿寻找 break;
和随后的 case:
或 default:
继续前进。
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html 有很好的例子供您学习。
评论后编辑:
为了回复您的评论,您需要执行以下操作:
case 'W':
tempBal = balbefore - transamount;
if(tempBal < 0) {
System.out.println("Insufficient Funds");
}
else {
balafter = balbefore - transamount;
if(balafter < 300) {
System.out.println("Warning, balance below 0");
}
}
break;
...
default:
System.out.println("Please select "D", "W", or "R");
break;
可以在switch body里面写case如下。你可以进一步延长条件,但在这里我只是给你一个如何做的例子。如果字符不是 D、W 或 R,则执行默认大小写。
switch (code) {
case 'D':
balafter = balbefore + transamount;
System.out.println("");
if (balafter < 0) {
pw.println("Insufficent funds");
} else if (balafter < 300) {
pw.println("Warning, balance below 0");
}else{
pw.println("Well. You have now " + balafter + "$");
}
break;
case 'W':
balafter = balbefore - transamount;
if (transamount > balbefore) {
pw.println("Sorry. You don't enough funds.");
} else if (transamount <= balbefore && balafter < 300) {
pw.println("Warning, balance below 0");
} else {
pw.println("The withdrawl was successful.");
}
break;
case 'R':
balafter = balbefore + transamount;
if (balafter < 0) {
pw.println("Insufficent funds");
} else if (balafter < 300) {
pw.println("Warning, balance below 0");
} else {
pw.println("Good. Your balance is " + balAfter);
}
break;
default:
System.out.println("Nothing to do.");
break;
}