我的硬币计数代码有问题,该代码需要用户输入他们有多少硬币,并将其转换为美元金额。我不断收到一个错误代码,对于如何将其组合在一起有些困惑。你能帮忙吗?
import java.util.Scanner;
public class CountChangeWithScanner{
public static void main(String[] args){
int quarters; //number of quarters that the user inputs.
int dimes; //number of dimes that the user inputs
int nickels; //number of nickels that the user inputs
int pennies; //number of pennies that the user inputs
Scanner stdio = new Scanner(System.in); // record user input
double dollars; // total value of coins
/* Prompt user to enter the number of each type of coin */
/*Prompt user to enter the number of quarters */
System.out.print("Enter the number of quarters: ");
quarters = stdio.nextInt();
stdio.nextLine();
/*Prompt user to enter the number of dimes */
System.out.print("Enter the number of dimes: ");
dimes = stdio.nextInt();
stdio.nextLine();
/*Prompt user to enter the number of nickels */
System.out.print("Enter the number of nickels: ");
nickels = stdio.nextInt();
stdio.nextLine();
/*Prompt user to enter the number of pennies */
System.out.print("Enter the number of pennies: ");
pennies = stdio.nextInt();
stdio.nextLine();
dollars = quarters + dimes + nickels + pennies;
System.out.println("");
System.out.println("The total in dollars is $");
System.out.println(dollars);
System.out.println("");
}
}
Josh如果您有计算问题,可以尝试这个解决方案。如果有帮助,请告诉我。
import java.util.Scanner;
public class CountChangeWithScanner{
public static void main(String[] args){
int quarters; //number of quarters that the user inputs.
int dimes; //number of dimes that the user inputs
int nickels; //number of nickels that the user inputs
int pennies; //number of pennies that the user inputs
Scanner stdio = new Scanner(System.in); // record user input
double dollars; // total value of coins
/* Prompt user to enter the number of each type of coin */
/*Prompt user to enter the number of quarters */
System.out.print("Enter the number of quarters: ");
quarters = stdio.nextInt();
stdio.nextLine();
/*Prompt user to enter the number of dimes */
System.out.print("Enter the number of dimes: ");
dimes = stdio.nextInt();
stdio.nextLine();
/*Prompt user to enter the number of nickels */
System.out.print("Enter the number of nickels: ");
nickels = stdio.nextInt();
stdio.nextLine();
/*Prompt user to enter the number of pennies */
System.out.print("Enter the number of pennies: ");
pennies = stdio.nextInt();
stdio.nextLine();
/* Calculate the total value of the coins */
dollar = (quarters * 0.25) + (dimes * 0.10) + (nickels * 0.05) + (pennies * 0.01);
/* Print the total value of the coins */
System.out.println("The total value of the coins is: " + dollar);
}
}