0

基本上,不是从用户那里读取一个值并将其转换为日元。我想输入一串值并将每个值转换为日元。

例如,下面的代码将只转换输入的一个值,直到输入零。然后程序将不会运行。

我如何让它采用一串浮点值,例如:

34.4 4.9 34.0 9.98 0

并将其转换为日元,同时在达到零时仍终止?

import java.util.Scanner;

public class P4Point27 {
    public static void main(String[] args) {
        String convert = "Enter Dollars to Convert to Yen (Enter '0' to Quit): ";
        Scanner in = new Scanner(System.in);

    System.out.print("Enter the Price of $1.00 Dollar in Japanese Yen: ");
    double dollarToYen = in.nextDouble();

    System.out.print(convert);
    double dollars = in.nextDouble();
    while (dollars != 0) {
    double price = dollars * dollarToYen;
        System.out.println("$" + dollars + " is " + price + " yen.");
        System.out.print(convert);
        dollars = in.nextDouble();
    }
    in.close();
}
}

输入:

9 8 7 0

输出:

Enter the Price of $1.00 Dollar in Japanese Yen: 9 8 7 0
Enter Dollars to Convert to Yen (Enter '0' to Quit): $8.0 is 72.0 yen.
Enter Dollars to Convert to Yen (Enter '0' to Quit): $7.0 is 63.0 yen.
Enter Dollars to Convert to Yen (Enter '0' to Quit): 

如您所见,它忽略了 9,并且还不断重新打印状态消息。我相信使用 do-while 循环将修复它缺少 9 或 for 循环,但我不确定。


修正了代码!工作代码!

package assignment4;

import java.util.Scanner;

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

    System.out.print("Enter the Price of $1.00 Dollar in Japanese Yen: ");
    double dollarToYen = in.nextDouble();

    System.out.print("Enter a String of Numbers Seperated by a Space and Terminated by a '0' to End: ");
    double dollars = in.nextDouble();
    while (dollars != 0) {
        while (dollars != 0) {
            double price = dollars * dollarToYen;
            System.out.println("$" + dollars + " is " + price + " yen.");
            dollars = in.nextDouble();
        }
        System.out.print("Enter a String of Numbers again or '0' to quit: ");
        dollars = in.nextDouble();
    }
    in.close();
}
}
4

2 回答 2

1

它不会“错过” 9。这就是它用作“1.00 美元日元”价值的价值。然后它正确转换 8 (8 * 9 = 72) 和 7 (7 * 9 = 63),然后在看到 0 时退出。部分问题可能是重复打印“Enter Dollars to Convert.. ."。由于每次执行循环时都会打印该消息,因此每次执行循环时它都会出现一次也就不足为奇了。

更新: System.out.print(convert);是打印您的状态消息的代码。您应该安排您的代码,以便无论您希望消息出现多少次都执行该语句。如果您只想查看一次,那么该语句不应出现在任何循环中,因为循环主体(通常)执行多次

于 2013-10-19T00:29:00.827 回答
1

这是一个可能的解决方案。它使用该 split()函数,这里的分隔符是逗号。然后将每个逗号分隔值解析为双精度,并应用算术。否则代码是不言自明的。

import java.util.Scanner;

public class P4Point27 {
    public static void main(String[] args) {
        String convert = "Enter Dollars to Convert to Yen (Enter '0' to Quit): ";
        Scanner in = new Scanner(System.in);

        System.out.print("Enter the Price of $1.00 Dollar in Japanese Yen: ");
        double dollarToYen = in.nextDouble();

        System.out.print(convert);

        String dollarString = in.next();
        String[] dollarArray = dollarString.split(",");
        double[] dollarDoubles = new double[dollarArray.length];
        for (int i = 0; i < dollarArray.length; i++) {
            dollarDoubles[i] = Double.parseDouble(dollarArray[i]);
        }

        for (int i = 0; i < dollarDoubles.length; i++) {
            double price = dollarDoubles[i] * dollarToYen;
            System.out.println("$" + dollarDoubles[i] + " is " + price
                    + " yen.");
        }
        in.close();
    }
}

这是程序的示例运行:

Enter the Price of $1.00 Dollar in Japanese Yen: 5
Enter Dollars to Convert to Yen (Enter '0' to Quit): 3,4,5
$3.0 is 15.0 yen.
$4.0 is 20.0 yen. 
$5.0 is 25.0 yen.
于 2013-10-19T00:33:19.837 回答