1

我以前曾发布过这个程序,但意识到我通过添加循环和其他什么东西想多了。我已经把它配对了一点,但仍然遇到问题。该程序应该是一个改变机器。在用户输入价格后,程序应将其四舍五入到最接近的美元,然后输出将分配多少零钱以及哪些硬币的数量。此时的输出完全错误。我对编程很陌生,我很茫然。

package changemachine;

import java.util.Scanner;
import java.text.*;

public class Main 
{

  public static void main(String[] args) 
   {   
    Scanner scan = new Scanner(System.in);

    System.out.println("Enter Purchase Price: ");

    double price = scan.nextDouble();

    int newPrice = (int)(price*100);

    int paid = (int)(newPrice+1);

    int change = (int)(paid - newPrice);

    int quarters = (int)(change/25);
    int dimes = (int)((change%25)/10);
    int nickels = (int)((change%25%10)/5);
    int pennies = (int) (change%25%10%5);

    System.out.println("Dispensing: " + quarters + " Quarters,"
          + dimes + "Dimes," + nickels + "Nickels," 
          + pennies + "Pennies.");

    System.out.println("Program written by Ashley ");
 }
}
4

5 回答 5

2

(一旦newPriceint,您可以停止转换每一行。)而不是链接%在一起,减去您找到的值会更具可读性(并且更不容易出错):

change -= 25*quarters;
dimes = change / 10;
change -= 10*dimes;
nickels = change / 5;
change -= 5*nickels;
pennies = change;
于 2013-08-24T22:14:50.297 回答
1

我认为这将有助于您理解您是否会手动浏览代码并考虑价格、新价格、支付和零钱是什么。

newprice 是向下舍入到较低美元的价格。支付的是项目的成本。零钱是您支付的金额减去转换为整数便士的成本。

package changemachine;

import java.util.Scanner;
import java.text.*;

public class Main 
{

  public static void main(String[] args) 
   {   
    Scanner scan = new Scanner(System.in);

    System.out.println("Enter Purchase Price: ");

    double price = scan.nextDouble();

            int newPrice = (int)(price);

            int paid = (int)(newPrice+1);

            int change = (int)((paid - price) * 100);

    int quarters = (int)(change/25);
    int dimes = (int)((change%25)/10);
    int nickels = (int)((change%25%10)/5);
    int pennies = (int) (change%25%10%5);

    System.out.println("Dispensing: " + quarters + " Quarters,"
          + dimes + "Dimes," + nickels + "Nickels," 
          + pennies + "Pennies.");

    System.out.println("Program written by Ashley ");
 }
}
于 2013-08-24T22:23:46.280 回答
0

如果指令int paid= (int)(newPrice+1) ;应该四舍五入到下一个美元,那么它应该是:当两个操作数都已经是 s时int paid= ( newPrice + 99 ) / 100 * 100 ; ,您不需要转换。使您的程序略显难以辨认。之后,通过季度获得季度数后= change / 25 ; 更改更改-= 宿舍 * 25 ;`。(int)int(that's correct in your program), you can reduce the amount fromwith

这使得计算dimes与 完全相同quarters,只是使用10而不是25。不要忘记change再次使用减少挂起的角钱change-= dimes * 10 ;。你可以重复这个过程,nickels剩下的change就是pennies

如果您有任何疑问,请使用调试器或使用System.out. 了解程序的行为后,您可以随时删除它们。

于 2013-08-24T22:17:41.533 回答
0

这就是我让 Java 选择我必须支付的硬币的方式。

    int temp = m;
    int quarterCoin = 25;
    int x = m/quarterCoin;
    m=m-x*quarterCoin;

    int dimeCoin = 10;
    int z = m/dimeCoin;
    m=m-z*dimeCoin;

    int nickelCoin = 5;
    int y = m/nickelCoin;
    m=m-y*nickelCoin;

    int pennyCoin = 1;
    int w = m/pennyCoin;
    m=m-w*pennyCoin;
于 2016-01-14T17:59:42.473 回答
-1

我不会给你作业的答案/解决方案,而是帮助你弄清楚如何解决这个问题。:)

为了充分调试您的软件并解决正在发生的事情,您需要知道您的变量在做什么。有两种方法:

  1. 附加调试器 - 大多数 IDE 都会附带一个调试器,可以帮助您完成此操作。
  2. 将变量打印到控制台。这是我的首选方法。我和调试器从来没有相处得很好。:)

所以,如果我想弄清楚你的程序,我会这样做:

import java.util.Scanner;

public class Change {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

//        System.out.println("Enter Purchase Price: ");

        double price = 5.65d;//scan.nextDouble();

        int newPrice = (int) (price * 100);
        System.out.println("newPrice: " + newPrice);

        int paid = (int) (newPrice + 1);
        System.out.println("paid: " + paid);

        int change = (int) (paid - newPrice);
        System.out.println("change: " + change);

        int quarters = (int) (change / 25);
        int dimes = (int) ((change % 25) / 10);
        int nickels = (int) ((change % 25 % 10) / 5);
        int pennies = (int) (change % 25 % 10 % 5);

        System.out.println("Dispensing: " + quarters + " Quarters,"
                + dimes + "Dimes," + nickels + "Nickels,"
                + pennies + "Pennies.");

        System.out.println("Program written by Ashley ");
    }
}

(注意:我没有使用扫描仪,而是手动将“5.65”输入到price变量中以节省时间)

产生输出:

newPrice: 565
paid: 566
change: 1
Dispensing: 0 Quarters,0Dimes,0Nickels,1Pennies.
Program written by Ashley 

所以,现在你可以看到你的程序做错了什么。你能发现吗?

于 2013-08-24T22:20:45.247 回答