2014-03-12 7 views
-1

기본적으로 배열의 길이를 결정하는 숫자를 묻는 프로그램을 작성한 다음 셀의 해당 값을 0으로 바꾸는 다른 숫자를 묻는 프로그램을 작성하려고합니다. 그러나 인쇄물에 지쳐서 내 배열은 모든 셀은 0배열의 정수가 왜 0으로 바뀌나요?

있습니다 나는 내가 피곤 세포에서 하나 하나를 인쇄하고 그들은 여전히 ​​0을 반환 이후

System.out.println (Arrays.toString(NumBox)); 

라인과 관련이있다 생각하지 않습니다.

코드 :

코드를 통해 스캔에서
import java.util.ArrayList; 
import java.util.Scanner; 
import java.util.Arrays; 


public class Game 
    { 
     public static void main(String []args) 
     { 
      int StartNum = 0; 
      Scanner scan = new Scanner (System.in); 
      System.out.println ("How many number would you like to play with?"); 
      StartNum = scan.nextInt(); 
      int score1 = 0; 
      int score2 = 0; 



      int[] NumBox = new int[StartNum]; 
      for (int i = 1; i < NumBox.length+1; i++) 
      { 
      } 



      int pick = 0; 
      boolean GG = false; 
      System.out.println ("Playing with " + StartNum + "numbers"); 
      System.out.println ("Notice that 0 represnts an unavailable numbers"); 
      for (int i = 1; i < NumBox.length+1; i++) 
       { 
       System.out.println(i); 
       } 

      while (GG == false) 
      { 

       System.out.println ("Pick an an available number: "); 
       pick = scan.nextInt(); 
       if (pick != 0) 
       { 
       score1 = pick + score1; 
       NumBox[pick-1] = 0; 
       System.out.println (Arrays.toString(NumBox)); 




      } 
      else 
      { 
       System.out.println ("Invalid Number"); 
       } 

      } 



     } 
    } 
+2

'NumBox'를 선언 한 곳에서 for for 루프가 비어있는 것은 무엇입니까? – PakkuDon

+0

'int'가 선언 될 때, 그것은 (기본값으로) 0으로 초기화됩니다 -'ints'의 배열 역시 이와 같이 행동 할 것입니다 – ochi

+1

int는 ** NOT ** "_tourning into_"0 ... 그들은 시작합니다 0으로 간주하고 어떤 것도 다시 할당하지 않습니다.왜 당신은 그 (것)들이 다른 무엇이든을 예상 할 것입니까? – jahroy

답변

2

가, 배열이 초기화 값을 0으로 설정하지만 배열 내부의 값이 무엇으로 설정되지 않습니다 그 후 ..

2

배열에 값을 입력하지 마십시오. 따라서 자바에서는 처음에는 모두 0으로 설정됩니다.

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

를 배열로 자바 제로 기반 아칸소 : 당신이 방법에 의해 아마 같을 것이다, 배열을 채우기 위해 그 빈 루프를 사용할 필요가 같은

보인다.

0
NumBox[pick-1] = 0; 
System.out.println (Arrays.toString(NumBox)); 

당신은 항상 당신이 배열을 만든 후 모든 값을 초기화하지 않기 때문에 System.out.println(i) 0의를 인쇄하여 값을 인쇄 한, 0 전에 forloop 다시 값을 설정합니다. (아무 값도 루프가 끝날 때까지 초기화되지 않습니다.)

 Scanner scan = new Scanner (System.in); 
     System.out.println ("How many number would you like to play with?"); 
     int StartNum = scan.nextInt(); //receive input 
     int score1 = 0; 
     int score2 = 0; 

     int[] NumBox = new int[StartNum]; //create array length of input 
     for (int i = 1; i < NumBox.length+1; i++) { 
      //? 
     } 

     int pick = 0; 
     boolean GG = false; 
     System.out.println ("Playing with " + StartNum + "numbers"); //prints input number 
     System.out.println ("Notice that 0 represnts an unavailable numbers"); 
     for (int i = 1; i < NumBox.length+1; i++) //prints all the values that havent been initialized yet 
      { 
      System.out.println(i); //prints all values in array (none have been set yet, 0s) 
      } 

     while (GG == false) 
     { 

      System.out.println ("Pick an an available number: "); 
      pick = scan.nextInt(); //receives next input 
      if (pick != 0) 
      { 
      score1 = pick + score1; 
      NumBox[pick-1] = 0; //sets a value to 0. the only time a value is set for array 
      System.out.println (Arrays.toString(NumBox)); //prints all the 0s 
0

int 변수를 초기화하지 않으면 0으로 설정됩니다.

코드는 모든 0을 포함하는 새로운 int 배열을 만듭니다.

앞으로는 어떤 배열의 값도 0 이외의 값으로 설정하지 않습니다.

어떤 이유인지 절대 사용되지 않는 score1이라는 변수가 있습니다. GCtrue로 설정되지 않습니다 때문에 또한 영원히 실행됩니다

귀하의 while 루프 ...

또한

, 왜 당신이 아무것도하지 않는 루프 빈을해야합니까?

자신의 코드를 읽고 각 행이하는 일에 대해 생각하는 시간 ...

0

그래서, 당신은 그들이 당신의 프로그램이 충돌하는 번호를 입력하지 않은 경우, 루프를 종료 할 수단이 없다, 나는 당신이 득점으로 무엇을하고 있는지 전혀 모릅니다. 그러나 이것은 다른 많은 문제를 해결할 것입니다.

import java.util.Scanner; 
import java.util.Arrays; 

public class Game { 
    public static void main(String[] args) { 
     int startNum = 0; //start variable and method names with a lower case 
     Scanner scan = new Scanner(System.in); 
     System.out.println("How many number would you like to play with?"); 
     startNum = scan.nextInt(); 
     int score1 = 0; 

     int[] numBox = new int[startNum]; 
     for (int i = 0; i < numBox.length; i++) { 
      numBox[i] = i+1; //initialize your numbers to not be 0- this was your main problem 
     } 

     int pick = 0; 
     boolean GG = false; 
     System.out.println("Playing with " + startNum + "numbers"); 
     System.out.println("Notice that 0 represnts an unavailable numbers"); 

     while (GG == false) { //GG never changes, so THEY MUST PLAY FOREVER! 
      System.out.println("Pick an an available number: "); 
      System.out.println(Arrays.toString(numBox)); //moved this up here so it is more obvious what they can pick 
      pick = scan.nextInt(); 
      if (pick > 0 && pick <= startNum) {//this will make sure they are in the index range- doesn't account for it not being a number, though... you need a try catch block for that 
       score1 = pick + score1; //...no idea what you're doing here 
       numBox[pick-1] = 0; //indexing is one below values 
      } else { 
       System.out.println("Invalid Number"); 
      } 
     } 
     scan.close(); //gotta close dat scanner 
    } 
} 
관련 문제