-4
String[] item ={"[1]hotdog", "[2]eggpie","[3]menudo","[4]pizza","[5]lumpia"};
int[] cost = {5, 10, 15, 20, 25};
int[] selling = {10,15,20,25,30,};
int[] qty = {2,4,6,8,10};

for(int b = 0; b<5;b++) {
    for(int c = 0; c<=1;c++) {
        for(int d = 0; d<=1;d++) {
            for(int e = 0; e<=1;e++) {
                System.out.println(" " + item[b] + "\t" +
                    cost[c] + "\t\t" + selling[d] + "\t\t" + qty[e]);
            }
        }
    }
}

It doesn't run how I want it to run; I want it to run like a table but using a for loop only not using an array[][].

4

3 回答 3

1

Just use a regular for loop to iterate over all arrays at once:

String[] item ={"[1]hotdog", "[2]eggpie","[3]menudo","[4]pizza","[5]lumpia"};
int[] cost = {5, 10, 15, 20, 25};
int[] selling = {10,15,20,25,30,};
int[] qty = {2,4,6,8,10};
for (int i = 0; i < item.length; i++)
{
    System.out.println(" " +item[i]+"\t"+cost[i]+"\t\t"+selling[i]+"\t\t"+qty[i]);
}

I would recommend putting all of these fields in an object, and then you can iterate over an array of that object.

于 2013-09-29T14:32:35.407 回答
0

Take a look at the Guava libraries Table collection.

于 2013-09-29T14:32:06.237 回答
0
public class Item {

    private int position;
    private String name;
    private int selling;
    private int quantity;
    private int cost;

    public Item()
    {      
            position = 0;
        name="";
        selling = 0;
        quantity =0;
        cost = 0;
    }

    public String GetState()
    {
        return String.format("{0} {0} {0} {0} {0} {0}", position,name,selling,cost,quantity); 

    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSelling() {
        return selling;
    }

    public void setSelling(int selling) {
        this.selling = selling;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public int getCost() {
        return cost;
    }

    public void setCost(int cost) {
        this.cost = cost;
    }


}


package test;

import java.util.ArrayList;
import java.util.List;

public class ItemTest {

    public static void main(String[] args) {

        Item i = new Item();
        List<Item> items = new ArrayList<Item>();
        items.add(i);
        items.add(i);

        for (Item item : items) {

            System.out.println(item.GetState());
        }




    }

}

console out : [0] 0 0 0 [0] 0 0 0

于 2013-09-29T14:48:14.793 回答