为什么我的数组中的整数变为0?

时间:2014-03-12 23:27:33

标签: java arrays java.util.scanner

基本上我正在尝试编写一个程序,询问用户一个确定数组长度的数字,然后向他们询问另一个数字,将单元格中的相应值转换为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");  
                }

            }



        }
    }

5 个答案:

答案 0 :(得分:2)

从扫描代码开始,数组被初始化,将数值设置为0,但之后数组中的值永远不会被设置为其他任何内容。

答案 1 :(得分:2)

您永远不会使用值填充数组。因此,在Java中,它们在开始时都设置为0。

好像你需要使用那个空循环来填充数组,顺便说一句,这可能是这样的:

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

由于Java中的数组从零开始。

答案 2 :(得分:0)

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

您不断将值设置回0。 之前的forloop,使用System.out.println(i)打印值的是打印0,因为您在创建数组后从未初始化任何值。 (没有值被初始化,直到你处于无限循环中)

       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

答案 3 :(得分:0)

如果没有初始化int变量,它将被设置为零。

您的代码会创建一个包含全零的新int数组。

从那时起,你永远不会将任何数组的值设置为零而是零。

由于某种原因,您有一个从未使用过的名为score1的变量。

您的while循环也会永远运行,因为GC永远不会设置为true ...

另外,为什么你的for循环无效?

是时候阅读自己的代码并思考每一行的作用......

答案 4 :(得分: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
    }
}