【发布时间】:2016-11-11 05:54:32
【问题描述】:
我的作业有困难。我有基本的逻辑,但我搞砸了。目标是从用户输入的购物清单中制作收据。例如,用户输入:
Apples
OraNgeS // also it's not case sensitive
Oranges
Bananas
!checkout //this is to indicate the list is over
输出:
Apples x1
Oranges x2
Bananas x1
我被困住了。到目前为止我的代码:
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.printf("Enter the items you wish to buy:");
String[] input = new String [keyboard.nextLine()];
keyboard.nextLine(); //consuming the <enter> from input above
for (int i = 0; i < input.length; i++) {
input[i] = keyboard.nextLine();
}
System.out.printf("\nYour input:\n");
for (String s : input) {
System.out.println(s);
}
}
我知道我最终必须添加 if 语句,所以如果他们输入“!checkout”,它将结束列表。但我现在还过不去。
有什么提示或建议吗?
【问题讨论】:
标签: java arrays string for-loop toupper