创建硬币计算
Creating a coin calculation
我正在创建一个硬币计算器,它将便士价值分解为面额,但我希望这样做,以便用户可以选择排除硬币的面额,请参阅下面的代码。我 运行 在这里陷入困境,并努力想出一个代码来实现这一点,如果有人有指点,我将不胜感激。我在创建代码时没有问题,用户可以在其中选择要包含的面额。
int money;
int denom;
Scanner sc=new Scanner(System.in);
System.out.println("Please enter the amount of coins you have in penny value");
money = sc.nextInt();
System.out.println("Also the denomiation you would like exclude; 2, 1, 50p, 20p, 10p");
denom = sc.nextInt();
while (money > 0){
if (money >= 200) {
System.out.println("£2");
money -= 200;
}
else if (money >= 100) {
System.out.println("£1");
money -= 100;
}
else if (money >= 50) {
System.out.println("50p");
money -= 50;
}
else if (money >= 20) {
System.out.println("20p");
money -= 20;
}
else if (money >= 10) {
System.out.println("10p");
money -= 10;
}
else if (money >= 1) {
System.out.println("1p");
money -= 1;
}
我想说这可能是最基本的方法,无需为其创建单独的方法。将面额设置为排除值,然后在减去之前检查硬币是否等于面额可以工作,因为反正没有那么多硬币。
int money;
int denom;
Scanner sc=new Scanner(System.in);
System.out.println("Please enter the amount of coins you have in penny value");
money = sc.nextInt();
System.out.println("Also the denomiation you would like exclude; 2, 1, 50p, 20p, 10p");
String input = sc.next(); //saving user input as a string
if (input.contains("p"))
{ //converting string to int by removing the p
denom = Integer.parseInt(input.substring(0, input.indexOf("p")));
}
else
{ //if there's no p, multiply its value by 100
denom = Integer.parseInt(input) * 100;
}
while (money > 0){
if (money >= 200 && denom != 200) {
System.out.println("£2");
money -= 200;
}
else if (money >= 100 && denom != 100) {
System.out.println("£1");
money -= 100;
}
else if (money >= 50 && denom != 50) {
System.out.println("50p");
money -= 50;
}
else if (money >= 20 && denom != 20) {
System.out.println("20p");
money -= 20;
}
else if (money >= 10 && denom != 10) {
System.out.println("10p");
money -= 10;
}
else if (money >= 1) {
System.out.println("1p");
money -= 1;
}
}
我正在创建一个硬币计算器,它将便士价值分解为面额,但我希望这样做,以便用户可以选择排除硬币的面额,请参阅下面的代码。我 运行 在这里陷入困境,并努力想出一个代码来实现这一点,如果有人有指点,我将不胜感激。我在创建代码时没有问题,用户可以在其中选择要包含的面额。
int money;
int denom;
Scanner sc=new Scanner(System.in);
System.out.println("Please enter the amount of coins you have in penny value");
money = sc.nextInt();
System.out.println("Also the denomiation you would like exclude; 2, 1, 50p, 20p, 10p");
denom = sc.nextInt();
while (money > 0){
if (money >= 200) {
System.out.println("£2");
money -= 200;
}
else if (money >= 100) {
System.out.println("£1");
money -= 100;
}
else if (money >= 50) {
System.out.println("50p");
money -= 50;
}
else if (money >= 20) {
System.out.println("20p");
money -= 20;
}
else if (money >= 10) {
System.out.println("10p");
money -= 10;
}
else if (money >= 1) {
System.out.println("1p");
money -= 1;
}
我想说这可能是最基本的方法,无需为其创建单独的方法。将面额设置为排除值,然后在减去之前检查硬币是否等于面额可以工作,因为反正没有那么多硬币。
int money;
int denom;
Scanner sc=new Scanner(System.in);
System.out.println("Please enter the amount of coins you have in penny value");
money = sc.nextInt();
System.out.println("Also the denomiation you would like exclude; 2, 1, 50p, 20p, 10p");
String input = sc.next(); //saving user input as a string
if (input.contains("p"))
{ //converting string to int by removing the p
denom = Integer.parseInt(input.substring(0, input.indexOf("p")));
}
else
{ //if there's no p, multiply its value by 100
denom = Integer.parseInt(input) * 100;
}
while (money > 0){
if (money >= 200 && denom != 200) {
System.out.println("£2");
money -= 200;
}
else if (money >= 100 && denom != 100) {
System.out.println("£1");
money -= 100;
}
else if (money >= 50 && denom != 50) {
System.out.println("50p");
money -= 50;
}
else if (money >= 20 && denom != 20) {
System.out.println("20p");
money -= 20;
}
else if (money >= 10 && denom != 10) {
System.out.println("10p");
money -= 10;
}
else if (money >= 1) {
System.out.println("1p");
money -= 1;
}
}