2014-09-20 2 views
0

기본적으로 배열의 행과 열에 대해 물어 보지 않고 사용자에게 배열의 행과 열에 대한 프롬프트를 표시해야합니다. 어떻게해야 다음 코드를 개선 할 수 있습니까?2 차원 정수 배열 사용

System.out.println("Please input the first set of integers separated by spaces"); 
    String givenSet1 = console.readLine(); 

    set1 = givenSet1.split(" "); 
    set1Values = new int[set1.length]; 

    for(int i = 0; i < set1.length; i++) 
    { 
     set1Values[i] = Integer.parseInt(set1[i]); 
    } 

    while(counter <= set1Values.length) 
    { 
     numbers = new int[set1Values.length][]; 
     numbers[counter] = set1Values[counter]; 
    } 

    for(int a = 0; a < set1Values.length; a++) 
    { 
     System.out.println("Please input the set of integers that are to be associated with element " + a + ", separated by spaces"); 
     String givenSet2 = console.readLine(); 

     set2 = givenSet2.split(" "); 
     set2Values = new int[set2.length]; 

     numbers[a] = new int[set2Values.length]; 
     System.out.println(numbers[a]); 

     for(int b = 0; b < set2Values.length; b++) 
     { 

     } 
    } 
+0

나는 정말로 혼란 스럽다. 만들려는 2 차원 배열의 행과 열을 묻지 않고 만들려는 2 차원 배열의 행과 열을 묻는 메시지가 필요합니까? 이 점을 분명히 해 주시겠습니까? 어쩌면 나는 그것을 잘못 읽고있다. – Makoto

답변

0

Array.length() 함수를 사용하면 배열의 요소 수를 반환 할 수 있습니다.

0

귀하의 질문은 불확실하고, 당신이 당신의 코드에서 뭘 하려는지 이해하기 어렵다하지만 난 어둠 속에서 촬영 ...

그것은 당신은 2 개 차원 배열을 만들려면 것처럼 보인다했다 사용자의 입력에 따라 사용자가 입력 한 총 숫자는 어레이의 열 수를 정의하며, 사용자는 해당 열의 각 관련 숫자로 저장하기 위해 정수를 제공합니다. 결과는 사용자가 각 열과 연결할 수있는 동일한 양의 숫자를 항상 입력하는 한 2 차원 배열이되지만 지그재그 배열을 만들기 위해 코드를 넣었습니다.

코드 :

import java.util.Scanner; 

public class Create2DArray { 

    public static void main(String[] args) 
    { 

     int[][] jaggedArray; //declare 2D array 
     String[] userInput; //declare array for user input 
     String[] userInput2; //declare array for ints to associate with first input 

     Scanner input = new Scanner(System.in); //scanner to read 

     System.out.println("Please input the first set of integers separated by spaces: ");  
     userInput = input.nextLine().split(" "); //parsing first set of string input 

     jaggedArray = new int[userInput.length][]; //create 2D array based on no. of columns defined in first set of user input  

     // array elements will be referenced by x and y, like coordinates. 
     for (int x = 0; x < jaggedArray.length; x++) //cycle through first set of numbers to prompt for associate numbers 
     { 
      System.out.println("Please input the set of integers that are" + 
           "to be associated with element " + userInput[x] + ", separated by spaces: "); 
      userInput2 = input.nextLine().split(" "); //parse and store the set to be associated with userInput[x] 

      jaggedArray[x] = new int[userInput2.length + 1]; //create a new int array in column x of jagged array, +1 to hold userInput[x] then associated values 

      for (int y = 0; y < jaggedArray[x].length; y++) // cycle through the new array @ column x 
      { 
       if (y == 0) 
        jaggedArray[x][y] = Integer.parseInt(userInput[x]); //if y = 0 then copy the "column header" first 
       else 
        jaggedArray[x][y] = Integer.parseInt(userInput2[y-1]); // else copy the new elements at position y-1 due to jagged array being +1 more than userInput2 array 
      }  
     }  


     System.out.println(); 

     for (int x = 0; x < jaggedArray.length; x++) //cycling through the array to print it all 
     { 
      System.out.print("For integer: " + jaggedArray[x][0] + 
          " the following integers are associated with it. "); 

      for (int y = 1; y < jaggedArray[x].length; y++) 
      { 
       System.out.print(jaggedArray[x][y] + " ");     
      }  

      System.out.println();     
     }  
    } 
} 

다음에 원하는 최종 결과에 대한 자세한 정보와 더 완전한 예제 코드를 제공하기 위해 시도하십시오. 우리가 어떤 유형의 객체와 같은 어떤 것을 알고 있다면 코드와 요구 사항을 이해하는 것이 약간 쉬울 것입니다. 정보가 많을수록 좋습니다.

이 내용을 따르기가 너무 어려울뿐만 아니라 실제로 원하는 내용입니다.