礼品登记循环程序

时间:2012-12-07 11:22:31

标签: java while-loop

我正在尝试编写一个在礼品登记处创建条目的程序。用户可以输入所需数量的礼品和可以购买的商店。一旦用户表达了停止输入新项目的愿望,所有礼品项目的摘要和将显示商店。

这些是我目前的代码:

import java.util.*;
public class GiftRegistry
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        List<GiftRegistry> list = new ArrayList<GiftRegistry>();
        private String item;
        private String store;
        char ans;

        System.out.print("Do you wish to make a gift registry? (y/n) ");
        ans = input.nextLine().charAt(0);

        while (ans != 'n') 
        {
            GiftRegistry object = new GiftRegistry();
                System.out.print("Enter item: ");
                object.setItem(input.nextLine());
                System.out.print("Enter store: ");
                object.setStore(input.nextLine());
                System.out.print("Any more items? (y/n) ");
                ans = input.nextLine().charAt(0);
                list.add(object);
        }

         System.out.println("Gift Registry");
         for (GiftRegistry myObject : list) {
              System.out.print(myObject.getItem()+" - "+myObject.getStore());
              System.out.println();
    }
}

}

这是当前的输入

Do you wish to make a gift registry? (y/n) y
Enter item: laptop
Enter store: Acer
Any more items? (y/n) y
Enter item: watch
Enter store: Swatch
Any more items? (y/n) n

它应该产生这样的输出:

Gift Registry
laptop - Acer
watch - Swatch

有人可以帮我提供正确的输出,谢谢

3 个答案:

答案 0 :(得分:2)

您可以创建一个自定义对象来保存您的值 -

class MyObject {
    private String item;
    private String Store;

    <getter & setters>

}

并将用户输入数据添加到列表中。

List<MyObject> list = new ArrayList<MyObject>();
...
while (ans != 'n') {
    MyObject object = new MyObject();
    System.out.print("Enter item: ");
    object.setItem(input.nextLine());
    System.out.print("Enter store: ");
    object.setStore(input.nextLine());
    System.out.print("Any more items? (y/n) ");
    ans = input.nextLine().charAt(0);
    list.add(object);
}

正确的打印方式是 -

System.out.println("Gift Registry");
for (MyObject myObject : list) {
    System.out.print(myObject.getItem()+" - "+myObject.getStore());
    System.out.println();
}

输出将是 -

Gift Registry
laptop - Acer
watch - Swatch

答案 1 :(得分:1)

你必须制作一个单独的字符串来存储你的物品并像这样存储

String itemandStore=""

while(ans!='n')         {

    System.out.print("Enter item: ");
    itemandStore += input.nextLine() + " - ";

    System.out.print("Enter store: ");
    itemandStore += input.nextLine() + "\n";

    System.out.print("Any more items? (y/n) ");
    ans = input.nextLine().charAt(0);

    }
System.out.println("Gift Registry\n" + item + store);

代码中发生了什么

项目包含附加在一个对象中的“laptop-watch”,因此输出错误。 如果您仍想为项目和商店使用不同的变量,我建议您在打印时使用

    String[] items=item.split("-");
    String[] stores=store.split("-");
     int i=0;
    while(items.length>i)
    {
    System.out.println(items[i] + " - " + stores[i]);
     i++;
    }

答案 2 :(得分:1)

我会在这里使用更面向对象的方法,我会创建一个Gift类,使用此结构覆盖toString方法以满足我的预期输出。

public class Gift{
  private String name;
  private String store;

//Getters & Setters

  public String toString(){
   return name + " " + store;
  }

}

然后我会创建一个Collection(一个List适合)礼物,并在while (ans != 'n')循环中添加一个新礼物到列表中。要打印结果,我会循环礼品清单并打印出所有礼品:

for (Gift gift: myListofGifts){
  System.out.println(gift);
}