这是一个学校项目,我应该制作一个计算总金额的程序。结果我持续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!");
}
}
答案 0 :(得分:1)
在获得实际值之前,您已经进行了计算。移动
totalCents = (quarters*25 + dimes*10 + nickels*5 + pennies);
dollars = (totalCents/100);
cents = (totalCents%100);
在您接受输入并指定quarters
到pennies
之后,但在最终输出之前。
答案 1 :(得分:1)
将此代码设为totalCents = (quarters*25 + dimes*10 + nickels*5 + pennies);
dollars = (totalCents/100);
cents = (totalCents%100);
在pennies = keyboard.nextInt();
行之后,它获取您在命令行输入的值。现在所需的输出为:
D:\>javac coin.java
D:\>java coin
How many quarters do you have -->3
How many dimes do you have -->
4
How many nickels do you have -->
5
How many pennies do you have -->
5
Quarters = 3
Dimes = 4
Nickels = 5
Pennies = 5
Total value =1dollars 45cents!
答案 2 :(得分:0)
是的,你必须把你的计算B \ W和它起作用
System.out.println("Quarters = " + quarters);
System.out.println("Dimes = " + dimes);
System.out.println("Nickels = " + nickels);
System.out.println("Pennies = " + pennies);
totalCents = (quarters*25 + dimes*10 + nickels*5 + pennies);
dollars = (totalCents/100);
cents = (totalCents%100);
System.out.println("Total value =" + dollars + "dollars " + cents + "cents!");