java - 输出从美元到人民币的货币表

标签 java arrays

我的教育任务是: “让程序输出一张有关将 1,2, ...20 美元转换为人民币的表格 按当前汇率(从键盘输入)”

我能够制作这样的东西:

public class TaskCurremcy {
public static void main(String[] args) {
    System.out.println("Enter current exchange rate:");
    Scanner sc = new Scanner(System.in);
    int value = sc.nextInt();
    System.out.println(Arrays.toString(convertDollarArray(value)));
}

public static int[] convertDollarArray(int n) {
    int [] currencyArray = new int[21];
    int [] convertedValue = new int[21];
    for (int i = 1; i < currencyArray.length; i++) {
        currencyArray[i] = i;
        convertedValue[i] = currencyArray[i] * n;
    }
    return convertedValue;
  }
}

但是如果我从键盘输入“6”,我会得到以下输出:[0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84 、90、96、102、108、114、120]

如何像工作表一样以垂直方式显示它以及如何跳过“0”并从数组的“1”位置开始? 感谢您的关注!

最佳答案

您可以考虑添加一个实用方法来打印结果,并接受要跳过的索引作为参数。最后的方法是一个例子。

public class TaskCurrency {

    public static void main(String[] args) {
        System.out.println("Enter current exchange rate:");
        Scanner sc = new Scanner(System.in);
        int value = sc.nextInt();
        printVertical(convertDollarArray(value), 0);
    }

    public static int[] convertDollarArray(int n) {
        int[] currencyArray = new int[21];
        int[] convertedValue = new int[21];
        for (int i = 1; i < currencyArray.length; i++) {
            currencyArray[i] = i;
            convertedValue[i] = currencyArray[i] * n;
        }
        return convertedValue;
    }

    // New method to print while skipping
    public static void printVertical(int[] arr, int skipIndex) {
        for (int i = 0; i < arr.length; i++)
            if (i == skipIndex) { // Skip the index you want
            } else
                System.out.println(arr[i]); // Or else print the result in a new array
    }

}

关于java - 输出从美元到人民币的货币表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51244404/

相关文章:

java - 需要帮助理解这个双重递归

java - 是否可以在 Spring 启动时将Web套接字与rest api一起使用?

java - 请求已超过允许的时间限制和 Java Heap Space Null 错误

python映射字典到数组

javascript - 如何找到任何网页上每个按钮的坐标?

Java String split() 零长度值的分隔符

java - 为什么java程序可以编译但不能运行?

c# - c#中带有字符串键的数组数组

c++ - 从 matlab 中的二进制文件读取 float 不正确

php - 不能使用 php 返回的数组