2014-04-07 1 views
0

코딩에 익숙하지 않고 배열 조건을 만들기 위해 노력하고 있었지만 while 루프 조건을 설정하여 어떤 이유로 든 배열을 변경하면 ' 있다. 미안하지만 이것이 명백한 문제라면 여기에서 내 문제에 적용되는 질문을 찾을 수없는 것 같습니다.while 루프가 알 수없는 이유로 의도 한 값을 변경하고 있습니다.

코드 : 올바르게 while 루프가 입력되고, 값이 보정 후 값을 입력 while 루프가 보유하고있는 상태로되어 있지만, 처음에 표시

import java.util.Scanner; 
//================================================================ 
public class ArrayIrreg { 
//---------------------------------------------------------------- 
    private static Scanner Keyboard = new Scanner(System.in); 

    public static void main(String[] args) { 
    //---------------------------------------------------------------- 
     char group, rLetter; 

     int num  = 10; // for test 
     int rows = 10; 
     int columns = 8; 

     // creating 2d array 


     System.out.print("Please enter number of rows    : "); 
     rows = Keyboard.nextInt(); 

     while (rows < 0 || rows >= 10) { 
      System.out.print("ERROR:Out of range, try again    : "); 
      rows = Keyboard.nextInt(); 
     } 

     double[][] figures = new double[rows + 1][num]; 

     for(int t = 0; t < rows; t++) { 
      rLetter = (char)((t)+(int)'A'); 
      System.out.print("Please enter number of positions in row " + rLetter + " : "); 
      columns = Keyboard.nextInt(); 

      while(columns < 0 || columns >= 8) { 
       System.out.print("ERROR:Out of range, try again    : "); 
       columns = Keyboard.nextInt(); 
      } 

      for(int j = 0; j <= columns; j++) { 
       figures[j] = new double[j] ; 
      } 

     } 

     // filling the array 
     for(int row = 0; row < figures.length; ++row) { 
      for(int col = 0; col < figures[row].length; ++col) { 
       figures[row][col] = 0.0; 
      } 
     } 

     // printing the array 
     for(int row = 0; row < figures.length; ++row) {  
      // printing data row 
      group = (char)((row)+(int)'A'); 
      System.out.print(group + " : "); 

      for(int col = 0; col < figures[row].length; ++col) {  
       System.out.print(figures[row][col] + " "); 
       System.out.print(" "); 
      } 

      System.out.println(); 
     } 

     // printing final border 
     for(int col = 0; col < figures[0].length; ++col) { 
      System.out.print("-+"); 
     } 

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

코드 온다 다른 오류를 표시하거나 잘못된 금액을 표시합니다.

초기 입력이 의도 한 범위를 벗어나면 while 루프가 새로운 입력 값을 강제하기 때문에 결과가 변경되는 이유가 확실하지 않습니다.

+0

디버깅에 대해 배울 시간입니다. 몇 가지 물건 (변수, 흐름 제어)을 인쇄하고 얻은 오류/스택 추적을 읽으십시오. 라인 번호가 있습니다. 실제 디버거를 사용하는 것은 과도 할 수도 있지만 배우기에도 매우 유용합니다. – keyser

+0

그것은 나를 위해 작동하는 것 같습니다. 다른 오류가있는 _보다 더 구체적인 오류를 표시하거나 잘못 표시 할 수 있습니까? – Keppil

답변

1

Keyboard.nextInt() 다음에 Keyboard.nextLine()으로 전화해야합니다.

설명 : 스캐너가 nextInt()를 호출하면 첫 번째 값을 가져오고 나머지 문자열은 \n에 남겨 둡니다. 그런 다음 나머지 문자열을 nextLine()과 함께 사용합니다.

관련 문제