귀하의 질문은 불확실하고, 당신이 당신의 코드에서 뭘 하려는지 이해하기 어렵다하지만 난 어둠 속에서 촬영 ...
그것은 당신은 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();
}
}
}
다음에 원하는 최종 결과에 대한 자세한 정보와 더 완전한 예제 코드를 제공하기 위해 시도하십시오. 우리가 어떤 유형의 객체와 같은 어떤 것을 알고 있다면 코드와 요구 사항을 이해하는 것이 약간 쉬울 것입니다. 정보가 많을수록 좋습니다.
이 내용을 따르기가 너무 어려울뿐만 아니라 실제로 원하는 내용입니다.
나는 정말로 혼란 스럽다. 만들려는 2 차원 배열의 행과 열을 묻지 않고 만들려는 2 차원 배열의 행과 열을 묻는 메시지가 필요합니까? 이 점을 분명히 해 주시겠습니까? 어쩌면 나는 그것을 잘못 읽고있다. – Makoto