这是针对一个学校项目的,我应该生成一个计算总金额的程序。结果我一直得到0。我做错什么了?谢谢!
package com.david.scanner;
import java.util.Scanner;
public class coin {
public static void main (String[] Args){
int quarters = 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;
int totalCents = 0;
int dollars = 0;
int cents = 0;
totalCents = (quarters*25 + dimes*10 + nickels*5 + pennies);
dollars = (totalCents/100);
cents = (totalCents%100);
@SuppressWarnings("resource")
Scanner keyboard = new Scanner(System.in);
System.out.print("How many quarters do you have -->");
quarters = keyboard.nextInt();
System.out.println("How many dimes do you have -->");
dimes = keyboard.nextInt();
System.out.println("How many nickels do you have -->");
nickels = keyboard.nextInt();
System.out.println("How many pennies do you have -->");
pennies = keyboard.nextInt();
System.out.println("Quarters = " + quarters);
System.out.println("Dimes = " + dimes);
System.out.println("Nickels = " + nickels);
System.out.println("Pennies = " + pennies);
System.out.println("Total value =" + dollars + "dollars " + cents + "cents!");
}
}
最佳答案
在获得实际值之前,您已经进行了计算。移动
totalCents = (quarters*25 + dimes*10 + nickels*5 + pennies);
dollars = (totalCents/100);
cents = (totalCents%100);
在接受输入并通过
quarters
分配pennies
之后,但在最终输出之前。