-2

このプログラムでさらに作業を行いましたが、文字列配列が印刷されるため、行き詰まりましたが、二重配列を印刷することは一生できません。どんな助けでも大歓迎です!

import java.util.ArrayList; 
import java.util.Arrays;
import java.lang.Double; 
import java.util.Scanner;

public class inventoryTracker
   {
      private static Scanner sc;
    private static double itemCost;

    public static void main(String[] args)
      {
     System.out.println("Welcome to the Inventory tracker"
            + "\nThis program will accept the names and costs for 10 stocked items."
            + "\nThe program will then output a table with the names, costs and,"
            + "\nprices of the items."
            + "\nPrices are calculated with a 30 percent markup on cost.");

    sc = new Scanner(System.in);

   String[] product = new String[10];
   Double[] itemCost = new Double[10];


   for (int i = 0; i < itemCost.length; i++ ){
         System.out.print("Enter the item cost :");
         itemCost [i]= sc.nextDouble();

   }

    for (int i = 0; i < product.length; i++){
            System.out.print("Enter the product name :");
            product[i] = sc.next();
    }  

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





        }


    }
4

3 に答える 3

0

これは、2 つの for ループで文字列を文字列配列に割り当てているためです。

product= sc.toString();

する必要があります

product[i] = sc.toString();

同じことが itemCost にも当てはまります。

于 2014-11-12T18:03:28.617 に答える
0

これは、2 つの for ループで文字列を文字列配列に割り当てているためです。また、正しくない sc.toString() を実行しています。

product= sc.toString();

itemCost= sc.nextDouble();

に変更する必要があります

product[i]  = sc.nextLine();

itemCost[i] = sc.nextDouble();

同じことが itemCost にも当てはまります。

于 2014-11-12T18:05:54.187 に答える
0

次のような値を設定するには、index を使用する必要があります。

product[index] = value;

また、ユーザーから文字列を取得するために sc.toString() を使用しています。ユーザーから文字列を取得するには、 next() メソッドを使用する必要があります。

ループは次のようになります。

 for (int i = 0; i < product.length; i++){
    System.out.print("Enter the product name :");
    product[i] = sc.next();
 }
 for (int i = 0; i < itemCost.length; i++ ){
     System.out.print("Enter the item cost :");
     itemCost [i]= sc.nextDouble();
 }
于 2014-11-12T18:06:39.560 に答える