2017-10-25 2 views
-2

그래서 저는이 지뢰 찾기 게임을 만들었고 두 가지 방법 중 하나를 사용하여 배열을 특정 문자로 초기화하고 하나의 메서드로 실제로 게임을 인쇄합니다.2D 배열 및 메서드 호출

public static void main(String[] args) { 

    Scanner in = new Scanner(System.in); 
    int a = 0; 
    int b = 0; 

    System.out.println("Welcome to Mine Sweeper!"); 

    a = promptUser(in, "What width of map would you like (3 - 20):", 3, 20); 
    b = promptUser(in, "What height of map would you like (3 - 20):", 3, 20); 

    eraseMap(new char[b][a]); 
    simplePrintMap(new char[b][a]); 
} 

public static int promptUser(Scanner in, String prompt, int min, int max) { 

    int userInput; 
    System.out.println(prompt); 
    userInput = in.nextInt(); 

    while (userInput < min || userInput > max) { 
     System.out.println("Expected a number from 3 to 20."); 
     userInput = in.nextInt(); 
    } 
    return userInput; 
} 

public static void eraseMap(char[][] map) { 

    for (int i = 0; i < map.length; ++i) { 
     for (int j = 0; j < map[i].length; ++j) { 
      map[i][j] = (Config.UNSWEPT); 

     } 
    } 
    return; 
} 

public static void simplePrintMap(char[][] map) { 

    for (int i = 0; i < map.length; ++i) { 
     for (int j = 0; j < map[i].length; ++j) { 
      System.out.print(map[b][a] + " "); 

     } 
     System.out.println(); 
    } 
    return; 
} 

질문에 방법이 eraseMap 및 simplePrintMap입니다 : 여기 내 코드입니다. eraseMap은 배열을 "." 그리고 simplePrintMap은 실제로 배열을 인쇄하기로되어 있습니다. 그래서 3과 4를 입력하면 기간은 3의 너비와 4의 높이가됩니다.

(각 마침표는 공백으로 구분됨).

+2

무엇이 문제입니까? 당신은 무엇을 기대합니까? 주어진 입력에 대해 실제로 얻는 것은 무엇입니까? –

답변

0

A)map 두 개를 새로 만듭니다. 처음에 지우기를 수행 한 다음 모든 것을 던져 버리고 새로운 map을 만들고 인쇄하십시오. 물론 비어 있습니다.

그것을 시도 하나 map 작업을 만들기 :

char[][] map = new char[b][a] 
eraseMap(map); 
simplePrintMap(map); 

인쇄 방법 B), 잘못된 인덱스를 사용

System.out.print(map[i][j] + " "); 

System.out.print(map[b][a] + " "); 

변경이를

C) 오류가 아님 힌트 : void 메서드 끝에 return;이 필요하지 않습니다.