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]);
}
}
}
}
它不会运行我想要它运行的方式;我希望它像表一样运行,但只使用for循环而不使用数组[] []。
答案 0 :(得分:1)
只需使用常规for
循环一次迭代所有数组:
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]);
}
我建议将所有这些字段放在一个对象中,然后你可以迭代该对象的数组。
答案 1 :(得分:0)
答案 2 :(得分: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());
}
}
}
控制台输出:[0] 0 0 0 [0] 0 0 0