[英]Java passing parameters between methods
I'm starting out in Java and was given an exercise to program a simple ATM. 我刚开始使用Java并进行了练习来编写简单的ATM。 Functions include:
功能包括:
Hints included: People want to see their money in 100.00 form, but that's bad for calculating in Java, so keep the internal machine operating on cents (Rappen in Switzerland) and any external messages in Dollars and Cents (ie. double). 包括以下提示:人们希望以100.00的形式看到他们的钱,但这对于用Java进行计算是不利的,因此,请使内部计算机以美分(瑞士的Rappen)运行,而所有外部消息都以Dollars和Cents(即double)运行。 Everything is working fine apart from the translation from cents to dollars & cents.
除了从美分到美元和美分的转换之外,一切都工作正常。 I decided to use a parallel double next to the cents and call a method that would turn cents into dollars and cents without overwriting or changing the "internal" count.
我决定在美分旁边使用一个并行双精度,并调用一种将美分转换为美元和美分而不覆盖或更改“内部”计数的方法。 Unfortunately, it doesn't seem to want to pass the parameters successfully to the mentioned method and is thus not doing the conversion correctly.
不幸的是,它似乎不想将参数成功传递给所提到的方法,因此不能正确地进行转换。
In the constructor I wanted to have kontostandInFranken (account balance in Dollars and Cents) carry the same weight as the kontostand (account balance in cents), but already there I'm hitting walls. 在构造函数中,我希望kontostandInFranken(美元和美分的帐户余额)与kontostand(以美分的帐户余额)具有相同的权重,但是我已经在碰壁了。 For example Dave opens an account and immediately deposits 25.50 dollars in there.
例如,戴夫(Dave)开了一个帐户,立即在其中存入25.50美元。 When I run that calculation in the constructor the 50 cents will go missing because I have to convert the kontostandInFranken (account balance in Dollars and Cents) into an int before I can multiply it by 100 and have it equal kontostand (account balance in cents)
当我在构造函数中运行该计算时,将失去50美分,因为我必须将kontostandInFranken(美元和美分的帐户余额)转换为int,然后才能将其乘以100并等于kontostand(以美分的帐户余额)
/**
* @author (Daniel.l.einars@gmail.com)
* @version (1.0)
*
* things that don't work:
* PASSING PARAMETERS BETWEEN METHODS. WHAT AM I DOING WRONG?
* CONVERTING BETWEEN RAPPEN AND FRANKEN & RAPPEN. ALWAYS ISSUES WITH DOUBLE VS. INTS
*/
public class Aufgabe2a
{
private int kontostand;
private double kontostandInFranken;
private String kontoInhaber;
private boolean kontoSaldiert;
/**
* Constructor
*/
public Aufgabe2a(String kontoInhaber, double kontostandInFranken)
{
this.kontoInhaber = kontoInhaber;
boolean kontoSaldiert = false;
this.kontostand = kontostand;
/**
*First issue. I can't seem to get the conversion from cents to dollars and *cents right
*/ kontostand = (int)kontostandInFranken * (int)100;
}
/**
* Ermöglicht Kontoeinzahlungen
*/
public int geldEinzahlen(int geldEingezahlt){
if(geldEingezahlt <=0){
System.out.println("Sie können nur positive Beträge einzahlen. Bitte versuchen Sie es erneut.");
return 0;
}
else if (kontoSaldiert == true){
kontoCheck();
}
else frankenInRappen(geldEingezahlt); //calling converting method here
if (kontostand + geldEingezahlt > 10000000){
System.out.println("Sie überschreiten mit dieser Einzahlung die Maximalhöhe von CHF 100.000. Sie können nur CHF " + kontostandInFranken(kontostand) +
(100000 - kontostandInFranken) + "einzahlen.");
}
else kontostand = (kontostand + geldEingezahlt);
return kontostand;
}
/**
* Ermöglicht Geld abzuheben
*/
public int geldAbheben(int geldAbgehoben){
if(geldAbgehoben <=0){
System.out.println("Sie können nur positive Beträge abheben. Bitte versuchen Sie es erneut.");
return 0;
}
else if (kontoSaldiert == true){
kontoCheck();
}
else frankenInRappen(geldAbgehoben);
if (kontostand - geldAbgehoben < 0){
System.out.println("Sie möchten zuviel Geld abheben. Sie können nur" + kontostand + "abheben.");
}
else kontostand = (kontostand - geldAbgehoben);
return kontostand;
}
/**
* Rechnet Rappen in Franken um - calculats cents into dollars and cents *ISSUE HERE
*/
private double rappenInFranken(double betrag){
double frankenBetrag = (betrag/100.0);
return frankenBetrag;
}
/**
* Rechnet Franken in Rappen um
*/
private int frankenInRappen(int betrag){
int rappenBetrag = (betrag*100);
return rappenBetrag;
}
/**
* Kontostand abfragen
*/
public void kontostandAbfragen(){
kontoCheck();
rappenInFranken(kontostand);
System.out.println("Kontoinhaber: " + kontoInhaber);
System.out.println("Kontostand: CHF " + kontostand +".-" );
frankenInRappen(kontostand);
}
/**
* Konto Saldieren/Auflösen
*/
public void kontoSaldieren(boolean kontoSaldiert){
kontoCheck();
this.kontoSaldiert = kontoSaldiert;
System.out.println("Sie haben Ihr Konto aufgelöst. Bitte nehmen Sie den restlichen Kontostand an sich");
kontostand = kontostand - kontostand;
}
/**
* Überprüft ob das Konto saldiert ist
*/
private void kontoCheck(){
if (kontoSaldiert == true){
throw new IllegalStateException("Konto ist saldiert, keine weiteren Aktionen möglich.");
} else return;
}
/**
* Führt einen paralelen Kontostand in Franken
*/
private double kontostandInFranken(int kontostand){
kontostandInFranken = (kontostand / 100.0);
return kontostandInFranken;
}
}
As it says in the hint, it will be easier if you only store a number of cents. 就像提示中所说的那样,如果只存储几分钱,将会更容易。
Ex. 例如
public class CheckingAccount
{
private int _balance;
private String _name;
public CheckingAccount(String name, double initialBalance)
{
_name = name;
_balance = (int)(initialBalance * 100);
}
...
}
You probably shouldn't store "dollar" and "cents" separately in two int
values. 您可能不应将“ dollar”和“ cents”分别存储在两个
int
值中。 Just store the "amount" in a double
or a BigDecimal
, then format the value when displaying. 只需将“数量”存储在
double
或BigDecimal
,然后在显示时格式化该值。
Example using double
: 使用
double
示例:
double amount = 25.5;
System.out.println(amount); // Unformatted
System.out.printf("%.2f%n", amount);
System.out.println(new DecimalFormat("0.00").format(amount));
System.out.println(new DecimalFormat("#,##0.00").format(amount));
System.out.println(NumberFormat.getCurrencyInstance().format(amount));
amount = 100000;
System.out.printf("%.2f%n", amount);
System.out.println(new DecimalFormat("0.00").format(amount));
System.out.println(new DecimalFormat("#,##0.00").format(amount));
System.out.println(NumberFormat.getCurrencyInstance().format(amount));
Output: 输出:
25.5
25.50
25.50
25.50
$25.50
100000.00
100000.00
100,000.00
$100,000.00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.