0

谁能帮我?我们的老师说他不想在我们的程序中使用数组列表。并且只有数组,for循环,并且只做while。该程序是一个采购程序。(添加项目,记录交易。)这是我想了解的第一步。

在这个程序中,我想正确显示项目代码和描述。例如代码=123 和描述=是的。输出显示,Item code = 123, Item descrpition = yeah.但是一旦我说是,我又放了另一个,示例代码 = 456 和描述 = 哦。输出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

4 回答 4

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]);
    }
}
于 2013-10-10T13:27:44.173 回答
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 是你可以运行的次数,但不是无限的,因为数组大小必须在编译时定义。

于 2013-10-10T13:21:20.947 回答
0

next() 处理行尾,因此当您next()再次调用时,它会将您之前输入的 enter ( \n) 作为输入。

你应该nextLine()在每个之后调用next()(所以它会吞下\n前一个)。

于 2013-10-10T13:07:41.870 回答
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 是否小于数组的容量,当达到时,您必须分配具有更高容量的新数组并复制值进去。

于 2013-10-10T13:14:47.723 回答