我试图在main中调用我的方法,但在输入我的美元和美分后,当我打印它们时,值不会改变
import java.text.DecimalFormat;
import java.util.Scanner;
public class ChangeMachine {
public static void main(String[] args) {
System.out.println("Change is good");
int dollars = 0;
int cents = 0;
int toonie = dollars/2;
int loonie = dollars%2;
int quarter = cents/25;
int dime = (cents%25)/10;
int nickel = ((cents%25)%10)/5;
ChangeMachine.getAmountFromUser(dollars, cents);
ChangeMachine.calculateCoins(dollars, cents);
ChangeMachine.displayInvoice(dollars, cents, toonie, loonie, quarter, dime, nickel);
}
输入美元和美分的方法 在这里,我可以输入金额,但在我尝试显示时不会出现
public static void getAmountFromUser(int dollars, int cents) {
Scanner input = new Scanner(System.in);
System.out.print("How many dollars? ");
dollars = input.nextInt();
System.out.print("How many cents? ");
cents = input.nextInt();
System.out.println();
input.close();
}
计算硬币的方法
public static void calculateCoins (int dollars, int cents){
DecimalFormat df = new DecimalFormat("#0.00");
double amount = dollars + cents/100.0;
System.out.println("$"+df.format(amount)+" requires:");
//-----Bonus-----
dollars=dollars+(cents+2)/100;
cents=(cents+2)%100;
//---------------
}
显示所需硬币的方法
public static void displayInvoice (int dollars, int cents, int toonie, int loonie, int quarter, int dime, int nickel){
System.out.println(toonie+" Toonies");
System.out.println(loonie+" Loonies");
System.out.println(quarter+" Quarters");
System.out.println(dime+" Dimes");
System.out.println(nickel+" Nickels");
}
}
答案 0 :(得分:2)
......打印时值不会改变
是的,它们不应该被更改,因为Java是一种按值传递的语言。传递给方法的任何原始变量都不会被更改(您只传递此变量的值,将为该方法创建一个新的局部变量)。
如何解决?
编辑:
当我们开始谈论从方法中返回多个值时,可能我们的方法构建不完全正确(例如,违反单一责任原则)。在这种情况下,我们需要重构我们的方法。考虑具有单一责任的方法(calculateDollars
,calculateCents
,displayCoin
)。它有助于将代码分成小的逻辑部分,使它们更加独立。
编辑2:
现在,您将与程序开头定义的变量联系起来。反过来,它们与那时的变量值绑定在一起。看看你如何纠正:
public int getLoonie(int dollars) {
return dollars % 2;
}
更好?我们在displayInvoice
:
System.out.println(getLoonie(dollars) + " Loonies");
或更好
System.out.println(getLoonie(userCash.getDollars()) + " Loonies");
答案 1 :(得分:1)
Java中对参数参数(局部变量)的引用是按值传递,而不是按引用传递。
每个方法都有自己的一组局部变量引用。当您为局部变量引用重新分配新值时,此更改是该方法的本地更改。
编辑:
这是一种方法:
import java.text.DecimalFormat;
import java.util.Scanner;
public final class ChangeMachine {
private ChangeMachine() {
}
public static Cash getAmountFromUser() {
try(final Scanner input = new Scanner(System.in)) {
System.out.print("How many dollars? ");
final int dollars = input.nextInt();
System.out.print("How many cents? ");
final int cents = input.nextInt();
return new Cash(dollars, cents);
}
}
public static void calculateCoins(final Cash cash) {
final DecimalFormat decimalFormat = new DecimalFormat("#0.00");
final double amount = cash.getDollars() + cash.getCents() / 100.0D;
System.out.println("$" + decimalFormat.format(amount) + " requires:");
cash.setDollars(cash.getDollars() + (cash.getCents() + 2) / 100);
cash.setCents((cash.getCents() + 2) % 100);
}
public static void displayInvoice(final Cash cash) {
System.out.println(cash.calculateToonies() + " Toonies");
System.out.println(cash.calculateLoonies() + " Loonies");
System.out.println(cash.calculateQuarters() + " Quarters");
System.out.println(cash.calculateDimes() + " Dimes");
System.out.println(cash.calculateNickels() + " Nickels");
}
public static void main(final String[] args) {
final Cash cash = getAmountFromUser();
calculateCoins(cash);
displayInvoice(cash);
}
private static final class Cash {
private int cents;
private int dollars;
public Cash(final int dollars, final int cents) {
this.dollars = dollars;
this.cents = cents;
}
public int calculateDimes() {
return this.cents % 25 / 10;
}
public int calculateLoonies() {
return this.dollars % 2;
}
public int calculateNickels() {
return this.cents % 25 % 10 / 5;
}
public int calculateQuarters() {
return this.cents / 25;
}
public int calculateToonies() {
return this.dollars / 2;
}
public int getCents() {
return this.cents;
}
public int getDollars() {
return this.dollars;
}
public void setCents(final int cents) {
this.cents = cents;
}
public void setDollars(final int dollars) {
this.dollars = dollars;
}
}
}
答案 2 :(得分:0)
您之前是否可以编译此代码?我只是检查ChangeMachine,getAmountFromUser和CalculateCoins方法,在Notepad ++中检查它们并提出了几个错误。 getAmountFromUser中的最后两行代码,System.out.println();
和' input.close();'似乎没有必要。
至于其他错误,我发现了很多class, interface, or enum expected
错误。