简体   繁体   English

为什么我的程序显示数组错误?

[英]why is my program displaying the array wrong?

I am having a problem with my program.我的程序有问题。 When I compile and run my program everything runs great until it's time to display the guesses back to the user.当我编译并运行我的程序时,一切都运行良好,直到将猜测显示给用户。 when that happens the last guess always gets displayed as 0.当这种情况发生时,最后的猜测总是显示为 0。

My assignment is to develop a program that simulates the high-low game.我的任务是开发一个模拟高低博弈的程序。 For each execution of the program, the game will generate a random number in the inclusive range of 1 to 100. The user will have up to 10 chances to guess the value.对于程序的每次执行,游戏将生成一个 1 到 100 的随机数,用户将有多达 10 次猜测该值的机会。 The program will keep track of all the user's guesses in an array.该程序将在一个数组中跟踪所有用户的猜测。 For each guess, the program will tell the user if his/her guess was too high or too low.对于每个猜测,程序都会告诉用户他/她的猜测是太高还是太低。 If the user is successful, the program will stop asking for guesses, display the list of guesses, and show a congratulatory message stating how many guesses he/she took.如果用户成功,程序将停止要求猜测,显示猜测列表,并显示一条祝贺消息,说明他/她猜了多少次。 If the user does not guess the correct answer within 10 tries, the program will display the list of guesses and show him/her the correct value with a message stating that he/she was not successful.如果用户在 10 次尝试中没有猜出正确的答案,程序将显示猜测列表并向他/她显示正确的值,并显示他/她没有成功的消息。 Regardless of the outcome, the program will give the user a chance to run the program again with a new random number.无论结果如何,该程序都会让用户有机会使用新的随机数再次运行该程序。

This is what I have so far:这是我到目前为止所拥有的:

import java.util.Random;
import java.util.Scanner;

/**
*
* @author jose
*/
public class Assignment7
{

/*

*/
public static void main(String[] args)
{
   Scanner input = new Scanner(System.in);
   int number;
  
   String again = "y";

   while (again.equalsIgnoreCase("y"))
   {
      int[] guesses = new int[10];
      int tries = 0;
  
      number = GetRandomNumber(1, 100);
      System.out.println(number); // delete before submitting

      int userGuess = GetUserGuess(1,100);
  
      while (userGuess != number && tries < guesses.length - 1 )
      {
         guesses[tries] = userGuess;
         LowOrHigh(number, userGuess);
         userGuess = GetUserGuess(1, 100);
         tries++;
  
      }
  
      if (tries != 10)
      {
         userGuess = guesses[tries];
         tries++;
         System.out.println("Congratulations! You were able to guess the correct number");
      }
  
      else
      {
         System.out.println("Sorry! You were not able to guess the correct number");
      }


      if (tries == 10)
      {
         System.out.println("Your guesses were incorrect");
         System.out.print("You guessed: ");

         for ( int i = 0; i < 10 ; i++)
         {
            System.out.print(guesses[i] + ", ");
         }

            System.out.println("The random number generated was " + number);
         }
  
         else
         {
            System.out.println("Well done! You were able to guess the "
                + "correct number in under 10 tries");
            System.out.print("You guessed: ");

            for ( int i = 0; i < tries; i++)
            {
               System.out.print(guesses[i] + " ");
            }


           System.out.println("The random number generated was "
             + number + ", it only took you " + tries + " tries.");

          }
  
      System.out.println("");

      System.out.print("Do you wish to try again with a different "
         + "number? (Enter y or n ): ");
      again = input.next();

      System.out.println("");
      }
   }
  
/*
METHOD 1
Description
  
A method that generates the random number to be guessed returns the
random number to main. Two parameters are the two numbers needed to generate
the random number (1 and 100 in this case).
  

*/
public static int GetRandomNumber (int rangeLow, int rangeHigh)
{

   Random gen = new Random();
   int number;

   number = gen.nextInt(rangeHigh) + rangeLow;

   return number;
}

/*

METHOD 2
This method tells the user if the guess is too low or too high. It will have
2 parameters one for the random number and the second is the user guess.

*/
public static void LowOrHigh (int number, int userGuess )
{
   if (userGuess > number )
   {
      System.out.println("The value that you guessed is too high, "
      +"Try guessing a lower number. ");
      System.out.println("");
   }
  
   else if (userGuess < number )
   {
      System.out.println("The value that you guessed is too low, "
      +"Try guessing a higher number. ");
      System.out.println("");
   }
}

/*

METHOD 3
This method will get the user guess. It has 2 parameters which will be the
valid range the user should guess between (in this case 1 and 100). It will
return the users guess as an integer. This method should validate that the
users guess is between the two parameters.
*/
public static int GetUserGuess(int rangeLow, int rangeHigh)

{
   Scanner scan = new Scanner(System.in);
   int userGuess;
  
   System.out.print("Enter a number between " + rangeLow + " and " + rangeHigh + ": ");
   userGuess = scan.nextInt();
  
   while (userGuess > rangeHigh || userGuess < rangeLow)
   {
      System.out.println("The number given was not within the range, Try again ");
      System.out.println("");
      System.out.print("Enter a number between " + rangeLow + " and " + rangeHigh + ": ");
      userGuess = scan.nextInt();
   }

   return userGuess;
}
  
}

I'm sorry if its obvious im still pretty new to programming.很抱歉,如果它显然对编程仍然很陌生。

Whenever you store a guess, you always store it in guesses[tries] , and then immediately afterwards, you increment tries .每当您存储猜测时,您总是将其存储在guesses[tries]中,然后立即增加tries Your while condition then checks if tries is less than guess.length - 1 .然后,您的 while 条件检查尝试是否小于guess.length - 1

More generally, to program you need to know how to debug.更一般地说,要编程,您需要知道如何调试。 Debugging is generally the act of following along with the code and checking what it actually does vs. what you wanted it to do.调试通常是跟随代码并检查它实际执行的操作与您希望它执行的操作。 You can use a debugger for this, alternatively, you can add a boatload of System.out statements to follow along.您可以为此使用调试器,或者,您可以添加大量 System.out 语句以进行跟进。

Do that, and you'll find the error in your logic.这样做,你会发现你的逻辑错误。 I've already given you quite a sizable hint in the first paragraph;)我已经在第一段中给了你相当大的提示;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM