Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 礼品登记循环程序_Java_While Loop - Fatal编程技术网

Java 礼品登记循环程序

Java 礼品登记循环程序,java,while-loop,Java,While Loop,我正试图写一个程序,使礼品注册表项。用户可以输入所需的任意数量的礼品以及可以购买这些礼品的商店。一旦用户表示希望停止输入新项目,将显示所有礼品项目和商店的摘要 以下是我目前的代码: import java.util.*; public class GiftRegistry { public static void main(String[] args) { Scanner input = new Scanner(System.in); List&

我正试图写一个程序,使礼品注册表项。用户可以输入所需的任意数量的礼品以及可以购买这些礼品的商店。一旦用户表示希望停止输入新项目,将显示所有礼品项目和商店的摘要

以下是我目前的代码:

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();
    }
}
它的输出应该是这样的:

Gift Registry
laptop - Acer
watch - Swatch

有人能帮我得到正确的输出吗?谢谢,你必须用一个字符串来存储你的物品,然后像这样存储

String itemandStore=""
而(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++;
    }

好吧,你将不得不制作一个字符串来存储你的物品,并像这样存储

String itemandStore=""
而(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++;
    }

可以创建自定义对象来保存值-

class MyObject {
    private String item;
    private String Store;

    <getter & setters>

}
而输出将为—

Gift Registry
laptop - Acer
watch - Swatch

可以创建自定义对象来保存值-

class MyObject {
    private String item;
    private String Store;

    <getter & setters>

}
而输出将为—

Gift Registry
laptop - Acer
watch - Swatch

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

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

//Getters & Setters

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

}
然后,我将创建一个礼物的
集合
(一个
列表
将是合适的),并在
while(ans!=“n”)
循环中向列表中添加一个新礼物。要打印结果,我将循环礼物列表并打印每个礼物:

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

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

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

//Getters & Setters

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

}
然后,我将创建一个礼物的
集合
(一个
列表
将是合适的),并在
while(ans!=“n”)
循环中向列表中添加一个新礼物。要打印结果,我将循环礼物列表并打印每个礼物:

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

有没有可能让它变成两个变量而不是一个?我知道让它变得更复杂听起来可能很可笑,但请帮助我:)但是项目的长度应该是不确定的。我不明白:(@DohaKik length会返回数组中的元素数。我已经尝试过了,但是这些行String[]items=item.split(“-”);String[]stores=store.split(“-”);是否有可能将其变为两个变量而不是一个?我知道这听起来可能很可笑,但请帮助我:)但是项目的长度应该是不确定的。我不明白:(@DohaKik length将返回数组中的元素数。我尝试过它,但这些行String[]items=item.split(“-”);String[]stores=store.split(“-”);正在错误地检查上面的代码。我试图用我的条款修改它。上面仍然有5行错误地检查我的代码。我试图用我的条款修改它。仍然有5行错误