2014-11-12 3 views
-2

이 프로그램에서 좀 더 많은 작업을했는데 문자열 배열이 인쇄되기 때문에 멈추었습니다.하지만 인쇄 할 때 이중 배열을 얻지 못했습니다. 어떤 도움을 주시면 감사하겠습니다!double 배열이 인쇄되지 않습니다

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)); 





     } 


    } 
+1

당신이 할당의 정확한 표현을 게시하시기 바랍니다 수 있습니까? 너는 그것을 잘못 해석했을지도 모른다. – RealSkeptic

+0

문자 그대로 말하자면 "병렬 배열"같은 것은 없습니다. @RealSkeptic이 말했듯이, 과제의 전체 텍스트를 표시하십시오. – fge

답변

0

두 개의 for 루프에서 문자열 배열에 문자열을 지정했기 때문입니다.

product= sc.toString(); 

product[i] = sc.toString(); 

해야하며 동일한 itemCost 간다.

0

두 개의 for 루프에서 문자열 배열에 문자열을 할당하기 때문입니다. 또한 sc.toString()이 올바르지 않습니다.

product= sc.toString(); 

itemCost= sc.nextDouble(); 

product[i] = sc.nextLine(); 

itemCost[i] = sc.nextDouble(); 

변경되어야하고 동일한 itemCost 간다. 또한 사용자로부터 문자열을 얻을 수 sc.toString()를 사용하는

product[index] = value; 

:

0

당신은 같은 값을 설정하는 인덱스를 사용해야합니다. 그것은 당신이 사용자로부터 문자열을 얻기 위해 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(); 
} 
+0

감사합니다. 그것이 조금 도움이되었지만 이제는 제품 이름을 수락하고 있지만 비용은받지 않습니다. 이 오류는 "주"java.util.InputMismatchException 스레드의 예외입니다 java.util.Scanner.throwFor (알 수없는 소스) java.util.Scanner.next에서 \t (알 수없는 소스)에서 \t java.util.Scanner에서 \t .nextDouble (알 수없는 소스) \t at inventoryTracker.main (inventoryTracker.java:29) – JavaStudent

+0

29 행은 itemCost [i] = sc.nextDouble(); – JavaStudent

+0

불일치가 보이지 않고 모든 것이 이중으로 선언됩니다. – JavaStudent

관련 문제