将int值分配给数组

时间:2017-10-23 00:34:00

标签: java arrays parsing

我需要为用户希望购买的一系列商品添加价格,比如一个书包等于40美元,这样我就可以累加价值并找到总数。我也遇到了int解析的问题。它抛出一个java主要异常。我已经在网上看了,但我找不到任何好的信息。

以下是我的代码的相关部分:

// Output items available for purchase 
    String[] csuStore = {"bookbag", "textbook", "notebook", "pen", "pencil", "sweatpants", "shirt", "sweater", "candy", "planner"};
    System.out.println("Textbook:     $50");
    System.out.println("Bookbag:      $40");
    System.out.println("Sweatpants:   $35");
    System.out.println("Sweater:      $30");
    System.out.println("Shirt:        $25");
    System.out.println("Planner:      $15");
    System.out.println("Notebook:     $3");
    System.out.println("Candy:        $2");
    System.out.println("Pen:          $1");
    System.out.println("Pencil:       $1");

    // Ask how many items the customer would like to purchase. 
    System.out.print("Enter how many items you would like to purchase: ");
    int items = Integer.parseInt(input.nextLine());
    if (items == 0) {
        System.out.println("Have a great day, come again soon!");
    if (items > 10)
        System.out.println("You're buying too much! Please decide on less than 10 items.");
    }

    // Have user to enter names of items they would like to purchase
        String arrayOfItems[] = new String[items];
        for (int i = 0; i < arrayOfItems.length; i++) {
            System.out.print("Enter the name of item #" + (i+1) + " : ");
                arrayOfItems[i] = input.nextLine();
        }       
        //Items purchased
        for (int i = 0; i < arrayOfItems.length; i++) {
            System.out.print("Items being purchased " + (i+1) + " : ");
                System.out.print(arrayOfItems[i] + "\n");
        }

2 个答案:

答案 0 :(得分:0)

一些建议:

  1. 使用Scanner类来获取输入。扫描仪有许多有用的方法,例如nextInt()并且您不必将字符串解析为int或考虑换行/空格等。

  2. 现在关于你的问题:如何处理这本书的价格

    一个。还有一个数组用于处理,现在你将与两个列表建立一对一的关系。

    湾只需要一个地图,其中bookname为关键,价格为value ...

    ℃。正如其他人所说,创建一个类:

    class Book {
      String name;
      Double price;
    }
    

    现在你的书看起来像这样

    List<Book> books = new ArrayList<>();
    
  3. 我建议阅读更多文档/ Java资料。

答案 1 :(得分:0)

可能低于代码可以帮助你。但我建议使用OOPs类和对象的概念,以获得更好的结果和简单的功能修改。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class demoClass {
public static void main(String[] args){

 int total=0;
 Scanner input = new Scanner(System.in);
// Output items available for purchase 
String[][] csuStore = {{"bookbag","40"},
                        {"textbook","30"},
                        {"notebook","20"}, 
                        {"pen","10"}};
System.out.println("1. Textbook:     $30");
System.out.println("2. Bookbag:      $40");
System.out.println("3. Notebook:     $20");
System.out.println("4. Pen:          $10");

// Ask how many items the customer would like to purchase. 
System.out.print("Enter how many items you would like to purchase: ");
int items = Integer.parseInt(input.nextLine());
if (items == 0) {
    System.out.println("Have a great day, come again soon!");
if (items > 10)
    System.out.println("You're buying too much! Please decide on less than 10 items.");
}

// Have user to enter names of items they would like to purchase
    String arrayOfItems[] = new String[items];
    for (int i = 0; i < arrayOfItems.length; i++) {
        System.out.print("Enter the name of item #" + (i+1) + " : ");
            arrayOfItems[i] = input.nextLine();
            for (int j=0; j<csuStore.length ;j++){
                if(arrayOfItems[i].equalsIgnoreCase(csuStore[j][0])){
                    total=total+Integer.parseInt(csuStore[j][1]);
                }
            }
    }       
    //Items purchased
    for (int i = 0; i < arrayOfItems.length; i++) {
        System.out.print("Items being purchased " + (i+1) + " : ");
            System.out.print(arrayOfItems[i] + "\n");

    }
    System.out.print("Total Amount:::: " + total);
  }
  }

您还可以使用for循环每次显示项目列表而不是System.out.print。