2014-02-06 3 views
-4
import java.util.Scanner; 
class bazar 
{ 
    void calculate() 
    { 
     int sum=0; 
     Scanner sc = new Scanner (System.in); 
     System.out.println("Hi ! welcome to out advance calculator"); 
     System.out.println("Enter the number of items that you wish to compute"); 
     int c = sc.nextInt(); 
     String item[] = new String[c]; 
     int price[] = new int[c]; 
     sc.nextLine(); 
     for (int i=1; i<=c; i++) 
     { 
      System.out.println("please enter the item name : "); 
      item[i] = sc.nextLine(); 
      System.out.println(); 
      System.out.println("please enter the price of " +item[i]+":"); 
      price[i] = sc.nextInt(); 
      sc.nextLine(); 
      sum=sum+price[i]; 
     } 
     //display part 
     for (int k=1; k<=c; k++) 
     { 
      System.out.println( "ITEM      PRICE"); 
      System.out.println (item[k]+"     "+price[k]); 
     } 
     System.out.println(); 
     System.out.println(); 
     System.out.println(); 
     System.out.println(); 
     System.out.println("YOUR BILL TOTAL HAS COME TO----------------->"+sum); 
    } 
} 
+2

배열은 0 인덱스, 1 인덱스되지 않습니다 예에서, 그 9 ​​

당신이 대신 원하는 액세스 지수 10 시도 싶지만 가장 높은 지수는 것을 의미한다. for 루프는 다음과 같아야합니다 :'for (int i = 0; i

+1

코드를 디버깅 해달라고 요청하지 마세요. 규칙을 읽으십시오. 또한 코드 태그를 사용하십시오. 마지막으로 환영합니다.하지만 시도한 내용과 콘솔의 정확한 오류를 알려줘야합니다. – Brendan

답변

1
int price[] = new int[c]; 
sc.nextLine(); 
for (int i=1; i<=c; i++) 

온. c가 10이라고 가정합시다. 이는 인덱스가 0-9이고, 총 10 개의 인덱스를 의미합니다. 그런 다음 까지 루프하고 c를 포함합니다.

for (int i=0; i<c; i++) 
+0

나는 이것을 시도했지만 여전히 같은 오류를 주었다 !! – user3258215

+0

코드의 여러 위치에서이 같은 오류가 발생했습니다. –

+0

당신은 제게 어떤 핵심 사항을 말해 줄 수 있습니까? – user3258215