你好,我在找你帮我做作业!因此,提示如下:
“编写一个程序,提示用户输入从美元到人民币的汇率。提示用户输入0从美元转换为人民币,1输入从人民币兑换成美元。提示用户输入美元或人民币分别兑换成人民币或美元。当用户输入"1”(元对美元)时,该程序应在小数点之前提取美元金额,而使用indexOf()和子字符串( methods.Here )的金额后的美分是示例运行,用户的输入下划线为:
输入从美元到人民币的汇率: 6.81输入0将美元兑换成人民币,反之亦然:0输入美元金额: 100美元100.0是681.0元。输入美元兑人民币的汇率: 6.81输入0将美元兑换成人民币,反之亦然:1输入人民币金额: 1000,1000.0元是146.843美元,146美元,四分之三,0美分,1枚镍币,4便士。
因此,我得到了我的代码去做所有的事情,在它把钱分割成美分价值之前。这就是我的问题所在,我不知道如何处理十进制值(或美分),以使其显示每个值的数量。这是我的密码
import java.util.Scanner;
public class LabTask6 {
private static Scanner key = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
double exchangeRate = 0;
int conversion;
double dollar = 0;
double yuan = 0;
String Dollar = " ";
int quarter = 0;
System.out.print("Enter the exchange rate from dollars to RMB: ");
exchangeRate = key.nextDouble();
System.out.print("Enter 0 to convert dollars to RMB or "
+ " 1 for RMB to dollars: ");
conversion = key.nextInt();
//while(conversion == 0 || conversion == 1) {
if(conversion == 0) {
System.out.print("Enter the dollar amount: ");
dollar = key.nextDouble();
yuan = dollar * exchangeRate;
System.out.println("$" + dollar + " is " + yuan + " yuan.");
System.out.println();
}else if(conversion == 1) {
System.out.print("Enter the Yuan amount: ");
yuan = key.nextDouble();
dollar = yuan / exchangeRate;
System.out.printf(yuan + " yuan is%8.3f dollars.", dollar);
System.out.println();
Dollar = dollar + "";
System.out.println(Dollar.substring(0, Dollar.indexOf(".")) + " dollars");
}
}
}如您所见,在第二个if语句中,我试图通过将数字转换为字符串并只显示从索引(0)到‘’的内容来获得“美元”金额。查。然而,这将不适用于其余的项目,如镍币,一角硬币,25美分和便士。我最初的想法是创建一个was循环,以防止小数(美分)达到零,并增加被声明为变量的分值,只要它们可被分号值(即数字为126.47 )整除,我就取47,看看它是否可以被25整除,如果结果大于0,它就会加1到四分之一,或者quarter++。有两个问题,第一个问题是,最终值不会是47美分,而且我正试图从一开始就找出如何隔离和操作这些分值。我知道我写了一篇该死的文章,但我现在完全不知所措了,我正试图尽可能清楚地说,有人能帮上忙吗?
回顾一下,我想打印美元,四分之一,镍,一角和便士的数额。但不知道是怎么回事。
发布于 2017-11-01 06:10:30
下面是一些示例代码供您参考。请注意,我将您的Dollar变量重命名为dollarStr,因为变量不应该以每个Java命名约定的大写字母开头。我还想指出,ajb是非常正确的,因为使用double来表示货币不是一个好主意。然而,由于这是一个简单的家庭作业,我怀疑这将是一个问题对你。
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
...
public static void main(String[] args){
//omitting extra code to shorten the answer
...
}else if(conversion == 1) {
System.out.print("Enter the Yuan amount: ");
yuan = key.nextDouble();
dollar = yuan / exchangeRate;
System.out.printf(yuan + " yuan is%8.3f dollars.", dollar);
System.out.println();
dollarStr = String.valueOf(dollar);
System.out.println(dollarStr.substring(0, dollarStr.indexOf(".")) + " dollars");
//Get the "cents" part of the converted value
String centsStr = dollarStr.substring(dollarStr.indexOf("."));
//Convert the value into a double
double cents = Double.parseDouble(centsStr);
//Multiplying by 100 so that something like 0.173 dollars
// becomes 17.3 cents
cents *= 100;
//Assuming you want to round to nearest penny,
// but it's unclear from your question if this is correct.
cents = Math.round(cents);
//quick and dirty conversion of the rounded cents to an integer value
int change = (int) cents;
//For demonstration purposes, feel free to remove it.
System.out.println(cents+ " cents");
//Calculate how many of each kind of coin and display results.
Map<Coin, Integer> changeMap = makeChange(change);
for(Coin coin: changeMap.keySet()){
int numCoins = changeMap.get(coin);
System.out.print(numCoins +" "+ (numCoins != 1 ? coin.getPlural() : coin.name().toLowerCase())+" ");
}
System.out.print("\n");
}
}
//Calculates the number of each kind of Coin to use
// in order to make the specified amount of change.
public static Map<Coin,Integer> makeChange(int cents){
Map<Coin,Integer> results = new HashMap<>();
for(Coin coin: Coin.values()){
int numCoins = cents/coin.getValue();
cents = cents % coin.getValue();
results.put(coin, numCoins);
}
return results;
}下面是与上述代码一起使用的Coin 枚举:
public enum Coin {
QUARTER(25, "quarters"), DIME(10, "dimes"), NICKEL(5, "nickels"), PENNY(1, "pennies");
private int value;
private String plural;
private Coin(int val, String plur){
value = val;
plural = plur;
}
public int getValue(){
return value;
}
public String getPlural(){
return plural;
}
}如果您对枚举不满意,那么可以将其转换为一个传统类,然后定义一些常量。
有些事情你仍然需要担心:
https://stackoverflow.com/questions/47048192
复制相似问题