购买Java第2部分

时间:2013-10-10 13:03:02

标签: java

任何人都可以帮助我吗?我们的老师说他不想在我们的程序中使用数组列表。并且只有数组,for循环,并且只做。该程序是一个购买程序。(添加项目,记录交易。)这是我想要了解的第一步。

在这个程序中,我想要正确显示项目代码和描述。例如code = 123和descrip = yes。输出显示,Item code = 123, Item descrpition = yeah.但是一旦我说是,我又放了另一个,示例代码= 456和desription =哦。输出Item code = 123456, Item description = yeah.oh.

import java.util.Scanner;

public class Apps {

    public static void main(String[] args) {

        Scanner a = new Scanner(System.in);
        String code = "", des = "", ans;
        String[] b = new String[1];
        String[] aw = new String[1];
        int x;

        do {

            System.out.print("Item code:");
            code += "" + a.next();

            System.out.print("des:");
            des += "" + a.next();

            System.out.println("yes or no:");
            ans = a.next();

        }

        while (ans.equals("yes"));

        for (x = 0; x < 1; x++) {
            b[x] = code;
            aw[x] = des;

            System.out.println("Item code:" + b[x]);
            System.out.println("Item description:" + aw[x]);

        }

    }

}

4 个答案:

答案 0 :(得分:2)

您可以将此代码用于任意数量的项目。它将数据存储在String中,并在用户选择no后分割字符串。

public static void main(String[] args) {
    Scanner a = new Scanner(System.in);
    String ans;
    String itemCounts = "";
    String descriptions = "";

    do {
        System.out.print("Item code:");
        itemCounts += "" + a.next() + "\n";

        System.out.print("des:");
        descriptions += "" + a.next() + "\n";

        System.out.println("yes or no:");
        ans = a.next();
    } while (ans.equals("yes"));

    String[] b = itemCounts.split("\n");
    String[] aw = descriptions.split("\n");

    for (int i = 0; i < b.length; i++) {
        System.out.println("Item code:" + b[i]);
        System.out.println("Item description:" + aw[i]);
    }
}

答案 1 :(得分:0)

next() 没有处理该行的结尾,因此当您再次致电next()时,它会将输入作为输入(\n你之前进入过。

您应该在每个nextLine()之后致电next()(因此它会吞下前一个\n。)

答案 2 :(得分:0)

这适用于例如:

public static void main(String[] args) {

    Scanner a = new Scanner(System.in);
    String ans;
    int capacity = 100;
    String[] b = new String[capacity];
    String[] aw = new String[capacity];
    int itemCount = 0;
    int x;

    do {           
        System.out.print("Item code:");
        b[itemCount] =  a.next();

        System.out.print("des:");
        aw[itemCount] = a.next();

        System.out.println("yes or no:");
        ans = a.next();
         itemCount++;
    } while (ans.equals("yes"));

    for (x = 0; x < itemCount; x++) {
        System.out.println("Item code:" + b[x]);
        System.out.println("Item description:" + aw[x]);
    }
}

如果你想确定,该用户可以添加“无限”数量的项目,你必须手动检查itemCount是否小于数组的容量,当它到达时,你必须分配具有更高容量的新数组将值复制到其中。

答案 3 :(得分:0)

可以使用以下集合来实现这一目标:

import java.util.ArrayList;
import java.util.Scanner;

public class Apps
{

    public static void main(String[] args)
    {

    Scanner a = new Scanner(System.in);
    String ans;

    ArrayList<String> br = new ArrayList<String>();
    ArrayList<String> ar = new ArrayList<String>();

    do
    {
        System.out.print("Item code:");
        br.add(a.next());

        System.out.print("des:");
        ar.add(a.next());

        System.out.println("yes or no:");
        ans = a.next();

    }

    while (ans.equals("yes"));

    for (int i = 0; i < br.size(); i++)
    {
        System.out.println("Item code:" + br.get(i));
        System.out.println("Item description:" + ar.get(i));
    }

    }

}

但不确定是否可以通过数组实现,或

public static void main(String[] args)
{

Scanner a = new Scanner(System.in);
    String code = "", des = "", ans;
    int capacity = 100;
    String[] b = new String[Integer.MAX_VALUE];
    String[] aw = new String[Integer.MAX_VALUE];
    int itemCount = 0;
    int x;

    do {           
        System.out.print("Item code:");
        b[itemCount] =  a.next();

        System.out.print("des:");
        aw[itemCount] = a.next();

        System.out.println("yes or no:");
        ans = a.next();
         itemCount++;
    } while (ans.equals("yes"));

    for (x = 0; x < itemCount; x++) {
        System.out.println("Item code:" + b[x]);
        System.out.println("Item description:" + aw[x]);
    }

}

Interger.MAX_VALUE是您可以运行此次但不是无限次的次数,因为必须在编译时定义数组大小。