1

我希望用户能够将数字添加到数组中,直到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();
    }
}
4

2 回答 2

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();
        }

    }
于 2012-04-29T22:37:59.590 回答
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();
}
于 2012-04-29T22:06:36.467 回答