2013-04-16 2 views
1

두 차원 배열에 좌표 x와 y를 삽입하여 인쇄하고 싶습니다. 주어진 코드에 주어진 예제가 입력을 요구할 때 그것을 한 번하지 않고 두 번 누른 다음 값을 받아들입니다. 값이 실제로는 각 배열이나 하나의 배열에 저장되는 것은 아닙니다. 행렬과 독립적으로 출력을 표시 할 수 있습니까? 나는 어떤 도움이나 교정을 원한다! 축복합니다.()를 두 번두 차원 배열의 값을 어떻게 표시합니까?

코드 당신은 input.nextInt를 사용하는

import java.util.Scanner; 

public class MummyMadness { 

    public static void main(String []args){ 


    int [][] desert = new int [1][1]; 
    int x =0; 
    int y =0; 
    int mummies[] = new int [1]; 
    int Mlimit = 100000; 


    coordinates(desert,mummies,x,y); 
    } 
    public static int[] inputmummy(int mummies[], int Mlimit){ 
     int i; 
     Scanner input = new Scanner(System.in); 
     System.out.println("How many mummies you want?"); 
     System.out.println("Note that the limit of mummies is 100k"); 
     i = input.nextInt(); 

     if(mummies[i] > Mlimit) 
     { 
      System.out.println("You exceeded from the mummies!"); 
     } 


     return mummies; 
    } 



    public static int[][] coordinates(int desert[][],int mummies[], int x, int y){ 


     //Idea use length of mummies for coordinates but if you take out desert i doesnt work 


     System.out.println("Enter mummy coordinates"); 

      for(int i = 0; i < desert.length; i++){ 

     System.out.println("Enter x:"); 
     Scanner input = new Scanner(System.in); 

       if(input.nextInt() > desert.length) 
       { 
        System.out.println("Error x exceeded desert length"); 
        break; 
       } 
       desert[i][0] = input.nextInt(); 

      System.out.println("Mummy location x: " + desert[i][0]); 

      for(int j = 0; j < mummies.length; j++){ 

     System.out.println("Enter y:");  
     Scanner input2 = new Scanner(System.in); 

       if(input.nextInt() > desert[0].length) 
       { 
        System.out.println("Error y exceeded desert length"); 
        break; 
       } 

       desert[0][j] = input2.nextInt(); 

       System.out.println("Mummy location y: " + desert[0][j]); 
       System.out.println(desert[i][j]) 


      } 

     } 

     return desert; 

     } 

    } 

답변

1

을 - 한 번 실제로 배열에 삽입 한 번 확인하고 위해 : 여기에 코드입니다. 이 함수를 호출 할 때마다 입력이 필요합니다. 따라서 입력을 두 번 요구합니다. 대신 다음을 수행하십시오.

int tmp = input.nextInt(); 
if(tmp > desert.length) 
{ 
    System.out.println("Error x exceeded desert length"); 
    break; 
} 
desert[i][0] = tmp; 
+0

이렇게했습니다. 순전히 shila 고맙습니다. 즉, input을 삽입하고 싶을 때마다 input.next를 사용해서는 안되며, 자동으로 입력을 요구하기 때문입니다. 고맙습니다. 또한 두 번째 입력을 수행 할 때 코드 맨 아래에서 scanner2에 대한 새로운 참조를 만들고 dint를 사용하여 x에 저장된 y 값을 만듭니다. –

관련 문제