猿问

输出从USD到CNY的货币表

我的教育工作有一个任务:“使程序输出一张纸,以当前汇率(通过键盘输入)将1,2,... 20 USD转换为CNY”


我可以做这样的事情:


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”位置开始?感谢您的关注!


白板的微信
浏览 195回答 3
3回答

至尊宝的传说

您可以定义自己的垂直打印方法:public static void printArrayVertically(Object[] array) {&nbsp; &nbsp;for (Object obj: array) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(obj);&nbsp; &nbsp;}}注意,您的类应重写toString()方法。在这种情况下,它已经完成。在主要方面:printArrayVertically(convertDollarArray(value));
随时随地看视频慕课网APP

相关分类

Java
我要回答