如何使用用户的输入填充数组?

时间:2014-10-04 20:42:42

标签: java arrays input

我需要使用q来填充来自用户的输入数组InputDialog,然后使用它来计算总数(总计+ =(priceOfProduct *数量),但我不知道如何这样做。我尝试了类似q(nextInt());的内容。

private String [] product = {"Shirts", "Jeans", "Hats", "Pants", "Shoes"};
private int [] q = new int[5];

public void inputOrder()
{
    //ask user how many of each item they wish to buy.
    for (int count = 0; count < product.length; count++)
    {

    String input = JOptionPane.showInputDialog("How many " + product[count] + "would     you like to purchase?");

    //read input from user
    Scanner s = new Scanner(input);

4 个答案:

答案 0 :(得分:0)

您可以使用Integer.parseIntscanner.nextInt,然后将其存储在数组中

import java.util.Arrays;
import javax.swing.JOptionPane;

public class Foo {
    static String [] product = {"Shirts", "Jeans", "Hats", "Pants", "Shoes"};
    static int [] q = new int[5];
    static int total;
    private static int quantity = 1; // Either input this value from the user or initialized it with your own value

    public static void main(String[] args) {


        for (int count = 0; count < product.length; count++) {
            String input = JOptionPane.showInputDialog("How many " + product[count] + "would     you like to purchase?");
            q[count] = Integer.parseInt(input);
        }

        System.out.println(Arrays.toString(q));

        System.out.println(calcTotal());
    }

    public static double calcTotal() { for (int i = 0; i < product.length; i++) { total += (getPrice(i) * quantity); } return total; }

    private static int getPrice(int i) {
        return q[i];
    }
}

答案 1 :(得分:0)

您可以在您提供的代码中添加以下内容:

q[count] = s.nextInt();

但这种方法不是优选的。你必须使用正则表达式来完成它。

答案 2 :(得分:0)

您需要为每种产品定义价格。 这应该工作。

import java.util.Scanner;
import javax.swing.JOptionPane;

class Test {

    public static void main(String[] args) {
        inputOrder();
    }

    public static void inputOrder() {
        String[] product = {"Shirts", "Jeans", "Hats", "Pants", "Shoes"};
        int[] q = new int[5];

        float total = 0.f;

        //ask user how many of each item they wish to buy.
        for (int count = 0; count < product.length; count++) {
            String input = JOptionPane.showInputDialog("How many " + product[count] + " would you like to purchase?");

            // Prices
            float priceOfProduct = 0.f;
            if(product[count].equals("Shirts")) {
                priceOfProduct = 100.1f;
            }
            if(product[count].equals("Jeans")) {
                priceOfProduct = 100.1f;
            }
            if(product[count].equals("Hats")) {
                priceOfProduct = 100.1f;
            }
            if(product[count].equals("Pants")) {
                priceOfProduct = 100.1f;
            }
            if(product[count].equals("Shoes")) {
                priceOfProduct = 100.1f;
            }

            // Total
            total += priceOfProduct * Integer.parseInt(input);

        }

        JOptionPane.showMessageDialog(null, "TOTAL = " +total);
    }
}

答案 3 :(得分:0)

您可以使用Scanner类:

import java.util.Scanner;

public class AnyClass{
    static Scanner scan = new Scanner(System.in);
    public static void main(String[] a){
        System.out.println("How many numbers you want to enter: "); 
        int n = scan.nextInt();
        int[] array = new int[n];
        System.out.println("Now enter "+n+" numbers: ");
        for(int i=0 ; i<n ; i++)
            array[i] = scan.nextInt();              
    }
  }