阵列问题不打印中间或中位数

时间:2012-04-29 22:02:30

标签: java

我希望用户能够在数组中添加数字,直到输入预定的randomNumber为止。然后我想打印数组的中间和中位数。

代码适用于用户输入,但我从中间或中位数得不到任何内容,只是说0

有人可以帮我这个吗?这是我的代码:

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

public class ArrayTest {
    Scanner input = new Scanner( System.in );

    int usersGuess;       
    int randomNumber = 45;

public void enterNumbers() {
    do {
        System.out.printf("Please enter numbers: ");
        usersGuess  = input.nextInt();

        if ( usersGuess != randomNumber ) {
            System.out.printf("Please enter numbers: ");
            usersGuess  = input.nextInt();
        }
        else 
              System.out.println("Correct guess");
    } 
    while ( usersGuess != randomNumber );   

    int testArray []  = new int[ 20 ];

    boolean correctGuess = false;

    for ( int i = 0; i < testArray.length; i++ ) {
        testArray[ i ] = usersGuess ;

        if ( testArray[ i ] == randomNumber ) {
            correctGuess = true;
            break;
        }
    }

    System.out.println();System.out.println( "Middle is: " + testArray
            [ ( testArray.length + 1)/2] );

    Arrays.sort( testArray );

    System.out.println( "Median is: " + testArray[ testArray.length/2] );
    System.out.println();
}


public class Test {
    public static void main( String [] args ){
        ArrayTest arrayTest = new ArrayTest();
        arrayTest.enterNumbers();
    }
}

2 个答案:

答案 0 :(得分:2)

我很确定如果您不知道如何构造数组,那么您不应该使用数据结构ArrayList。这是没有ArrayList的代码的完整版本。

  • 数组是最终的,因为它们在创建后无法更改。所以在这个例子中我使用了最大数量的猜测

希望这会有所帮助......

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

    public class ArrayTest 
    {
        Scanner input = new Scanner( System.in );

        int usersGuess;       
        int randomNumber = 45;
         int num_of_guesses = 0;

         int max_guesses = 20;


        public void enterNumbers()
        {
            int testArray []  = new int[ max_guesses ];

             do
            {


                  if ( usersGuess != randomNumber )
                {
                    System.out.printf("Please enter numbers: ");
                    usersGuess  = input.nextInt();     

                }
                else 
                      System.out.println("Correct guess");

                  testArray[ num_of_guesses ] = usersGuess;
                  num_of_guesses++;

                  if ( num_of_guesses >= max_guesses)
                {
                    System.out.println("Your all out of guesses!");
                    break;     
                } 

            } 
            while ( usersGuess != randomNumber &&  max_guesses >= num_of_guesses );

            System.out.println( "\nMiddle is: " + testArray
                    [ ( num_of_guesses)/ 2] );

            Arrays.sort( testArray, 0, num_of_guesses );


            System.out.println( "Median is: " + testArray[ ( num_of_guesses)/ 2 ] );
            System.out.println();

        }

    }

    public class Test 
    {
        public static void main( String [] args )
        {
            ArrayTest arrayTest = new ArrayTest();

            arrayTest.enterNumbers();
        }

    }

答案 1 :(得分:1)

你没有挽救用户的猜测。您需要在第一个testArray循环中向do while添加猜测。

这应该适合你:

public void enterNumbers()
{
    List<Integer> guesses = new ArrayList<Integer>();
    do  
    {   
        System.out.printf("Please enter numbers: "); 
        usersGuess  = input.nextInt();
        guesses.add(usersGuess);

        if ( usersGuess != randomNumber )
        {   
            System.out.printf("Please enter numbers: "); 

        }   
        else 
              System.out.println("Correct guess");   

    }   
    while ( usersGuess != randomNumber );

    int size = guesses.size();
    System.out.println("\nMiddle is: " + guesses.get((size + 1) / 2);

    Collections.sort(guesses);

    System.out.println("Median is: " + guesses.get(size / 2));
    System.out.println();
}