2012-11-28 2 views
0

과제 : 작은 사촌이 ​​다니는 학교에서 쿠키를 판매하고 있습니다. 사촌의 수업이 다른 수업보다 많은 쿠키를 판매하는 경우 선생님은 전체 수업을 소풍으로 가져갈 것을 약속했습니다. 물론, 사촌이 모든 판매를 추적하고 승자를 결정하기 위해 자원했습니다.txt 파일의 모든 다른 행을 읽고 배열로 저장

각 클래스는 교사의 이름으로 식별됩니다. 각 판매 전표에는 교사의 이름과 판매 된 상자 수가 있습니다. 선생님의 이름을 담는 배열과 판매 된 상자의 수를 기록하는 배열을 두 개의 병렬 배열로 만들기로 결정했습니다.

가 첫 번째 숫자는 클래스의 수를 제공하고 교사의 이름이 코티 스미스를 판매 상자의 숫자로 다음에 등등 등등 ... : 여기에 데이터의 샘플입니다 는 박스 배열로 저장하는 다른 모든 라인을 받고있다 (난 단지 "로하는-일"parrallel 배열을 복제 할 수 있기 때문에)


내 주요 문제는 같을 것이다 그래서 배열 "boxSold" 판매

[1] 15
[2] 3

package assignment5Package; 

    import java.util.Scanner; 

    import java.io.*; 



    public class assignment5Demo 
    { 

     /** 
     * @param args 
     * @throws IOException 
     */ 
     public static void main(String[] args) throws IOException 
     { 
      // TODO Auto-generated method stub 
      //create arrays, variables 
      Scanner keyboard = new Scanner(System.in); 
      BufferedReader input = new BufferedReader 
       (new FileReader ("/Users/lee/Desktop/class/cs 113/Assignment5/cookies.txt")); 

      System.out.println("How many sale slips are there"); 

      int numSaleSlips = keyboard.nextInt(); 
      int[] soldBox = new int[numSaleSlips]; 
      //______String[] teacherName = new String[numSaleSlips]; 
      int soldBoxIndex; 
      int teacherNameIndex; 
      //String soldBoxString; (line 50) 




      //initializing both strings to 0 and "_" 
      for (soldBoxIndex = 0; soldBoxIndex < numSaleSlips; soldBoxIndex++) 
      { 
       soldBox[soldBoxIndex] = 0; 
      } 

      //**for (teacherNameIndex = 0; teacherNameIndex < numSaleSlips; teacherNameIndex++) 
      //**{ 
      //** teacherName[teacherNameIndex] = "_"; 
      //**} 

      //reading from the cookies.txt file 
      for (soldBoxIndex = 0; soldBoxIndex < numSaleSlips; soldBoxIndex++) 
      { 
       if (soldBoxIndex % 2 != 0 
       { 
        String soldBoxString; 


        soldBoxString = input.readLine(); //reads in value and assigns/re-assigns 
        soldBox[numSaleSlips] = (int) Double.parseDouble(soldBoxString); //type-casted to fit variable type, converts to double, stores in array 
        System.out.println(soldBox[soldBoxIndex]); 
       } 
       else 
       { 
        System.out.println("Error at " + soldBoxIndex +"."); 
       } 

      }  
} 

답변

0

다음은 빠른 - 및 - 더러운 솔루션이 될 수 있지만, 수행 작업 얻을 것이다 :

for (soldBoxIndex = 0; soldBoxIndex < numSaleSlips; soldBoxIndex++) 
{ 
    if (soldBoxIndex % 2 != 0 
    { 
     String soldBoxString; 


     soldBoxString = input.readLine(); //reads in value and assigns/re-assigns 
     soldBox[numSaleSlips] = (int) Double.parseDouble(soldBoxString); //type-casted to fit variable type, converts to double, stores in array 
     System.out.println(soldBox[soldBoxIndex]); 
    } 
    else 
    { 
     input.readLine(); //read the following line, but ignore its content, effectivly skipping the line 
    } 

} 

를 또한의의 수를 작동해야 할 수도 있습니다 -loop 비트를 건너 뛰고 건너 뛴 줄을 수용합니다.

+0

THANKS for the concept : D "다음 줄을 읽고 내용 아이디어를 무시합니다" 그냥 간단한 루프를 만들었습니다. –

관련 문제