另一个数组循环中的引用数组

时间:2014-07-18 21:51:49

标签: java arrays for-loop

我试图在循环遍历另一个数组的for循环中使用一个数组的项目。例如,我想在询问每个项目的价格时显示项目名称(已存在的数组)。 " item [i] "的价格是多少。我不认为你可以在这个例子中使用item [i]。我也尝试过使用get方法并使用计数器变量,但我一定做错了。

readPrice部分不起作用 - 特别是项目[i]。有没有人建议如何在不使用item [i]的情况下完成同样的事情?

我是java的新手,所以如果这是一个明显的答案,我很抱歉,但我真的很感激帮助!

这是我的代码:

private String[] items;
private int[] priority;    
private double[] price;

public void readInput()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("You will need to enter seven shopping items.");

    items = new String[7];

    for (int i = 0; i < items.length; i++)
    {
        System.out.println("Enter an item: ");
        items[i] = keyboard.nextLine();   
    }

}

**public void readPrice()**
{
    price = new double[7];
    Scanner keyboard = new Scanner(System.in);
    for(int i = 0; i < price.length; i++)
    {

        System.out.println("Enter the price for " + **items[i]**);
        price[i] = keyboard.nextDouble();

    }

}

3 个答案:

答案 0 :(得分:0)

好的,这就是你的答案。有两种主要方法:将对象items作为参数传递,或将其设为全局。

全局变量方法

对于您似乎已经在做的全局方法,首先将您的对象声明为您的类的字段:

class MyClass
{
    private String[] items = new String[7];

    //...Rest of class goes here
}

现在您可以根据需要从两种方法中访问它。

public readInput()
{
    // Add stuff to items
    items[0] = "hi";
}
public readPrice()
{
    // Read from items
    System.out.println(items[0]);
}

方法参数方法

现在,如果您想将一个对象从一个方法实际传递到另一个方法但又不希望任何其他方法可以访问它,您可以将其设为如下参数:

public readPrice(String[] items)
{
    // Read from items
    System.out.println(items[0]);
}

然后你可以从其他地方调用该方法传递对象类型的特定实例。

public inputPrice()
{
    String[] items = new String[7];
    // Add stuff to items
    items[0] = "hi";
    this.readPrice(items);
}

如果要多次调用单个方法,但是让它作用于不同的输入,则传递参数特别有用。

答案 1 :(得分:0)

你可以制作一个课程项目。

public class Item {
    String name;
    double price;

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

    public void setPrice(double price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "Item{" + "name=" + name + ", price=" + price + '}';
    }

}

创建类Item

的对象后
    public class TestPaper {

    public static void main(String[] args) {
        Item item;
        Item[] itemTable = new Item[7];
        ArrayList<Item> itemlist = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            item = new Item();
            //set value
            item.setName("item1");
            item.setPrice(200);
            //store the object to a table or an arraylist
            itemTable[i] = item;
            itemlist.add(item);
        }

        for (Item temp : itemTable) {
            System.out.println(temp.toString());
        }

    }

}

这是更好的解决方案,但我会用你的代码回答你。

您有7个项目,因此您可以打印如下项目:

private String[] items;
private int[] priority;    
private double[] price;

public void readInput()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("You will need to enter seven shopping items.");

    items = new String[7];

    for (int i = 0; i < items.length; i++)
    {
        System.out.println("Enter an item: ");
        items[i] = keyboard.nextLine();   
    }

}

**public void readPrice()**
{
    price = new double[7];
    Scanner keyboard = new Scanner(System.in);
    for(int i = 0; i < price.length; i++)
    {

        System.out.println("Enter the price for " + **items[i]**);
        price[i] = keyboard.nextDouble();

    }

    public void printdata(){
    for(int I=0; I<7; I++){
       System.out.println("item name: " +item[i] +" price: "+price[i])

    }

    }

答案 2 :(得分:0)

请提供发生错误的堆栈跟踪,以使每个人都满意。我个人会创建一个地图来为项目分配价格。

package test;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class Items {

    private int itemsLength = 10;
    private Map<String, Double> items = new HashMap<>();

    public Items() {
        System.out.println("Please fill in the details below: ");
        for(int i = 0; i < itemsLength; i++) {
            Scanner in = new Scanner(System.in);
            String itemName;
            Double price;
            System.out.println("Enter an item name: ");
            itemName = in.nextLine();
            System.out.println(String.format("Enter %s's price: ", itemName));
            price = in.nextDouble();
            items.put(itemName, price);
        }
        System.out.println("Here are your items: ");
        System.out.println("+------------------------+");
        System.out.println(String.format("|%-10s | %-10s %-6s", "Item", "Price", "|"));
        for (Entry<String, Double> entry : items.entrySet()) {
            System.out.println(String.format("|%-10s | %-10s %-6s", entry.getKey(), entry.getValue(), "|"));
        }
        System.out.println("+------------------------+");
    }
}

输出

Please fill in the details below: 
Enter an item name: 
Shoes
Enter Shoes's price: 
20
Enter an item name: 
Shirt
Enter Shirt's price: 
20
Enter an item name: 
Necklace
Enter Necklace's price: 
50
Enter an item name: 
Bracelet
Enter Bracelet's price: 
30
Enter an item name: 
Socks
Enter Socks's price: 
5
Enter an item name: 
Flip-flops
Enter Flip-flops's price: 
10
Enter an item name: 
Soda
Enter Soda's price: 
2
Enter an item name: 
Diamonds
Enter Diamonds's price: 
20000
Enter an item name: 
Weird Item
Enter Weird Item's price: 
99999
Enter an item name: 
Another Weird Item
Enter Another Weird Item's price: 
99999
Here are your items: 
+------------------------+
|Item       | Price      |     
|Necklace   | 50.0       |     
|Another Weird Item | 99999.0    |     
|Shirt      | 20.0       |     
|Bracelet   | 30.0       |     
|Diamonds   | 20000.0    |     
|Weird Item | 99999.0    |     
|Shoes      | 20.0       |     
|Socks      | 5.0        |     
|Soda       | 2.0        |     
|Flip-flops | 10.0       |     
+------------------------+

P.S:这是我的第一个回复,抱歉,如果它不漂亮

编辑:如果你想在输出时保持顺序,那么构造一个TreeMap而不是HashMap。