簡體   English   中英

為什么我的程序顯示數組錯誤?

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

我的程序有問題。 當我編譯並運行我的程序時,一切都運行良好,直到將猜測顯示給用戶。 當這種情況發生時,最后的猜測總是顯示為 0。

我的任務是開發一個模擬高低博弈的程序。 對於程序的每次執行,游戲將生成一個 1 到 100 的隨機數,用戶將有多達 10 次猜測該值的機會。 該程序將在一個數組中跟蹤所有用戶的猜測。 對於每個猜測,程序都會告訴用戶他/她的猜測是太高還是太低。 如果用戶成功,程序將停止要求猜測,顯示猜測列表,並顯示一條祝賀消息,說明他/她猜了多少次。 如果用戶在 10 次嘗試中沒有猜出正確的答案,程序將顯示猜測列表並向他/她顯示正確的值,並顯示他/她沒有成功的消息。 無論結果如何,該程序都會讓用戶有機會使用新的隨機數再次運行該程序。

這是我到目前為止所擁有的:

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

很抱歉,如果它顯然對編程仍然很陌生。

每當您存儲猜測時,您總是將其存儲在guesses[tries]中,然后立即增加tries 然后,您的 while 條件檢查嘗試是否小於guess.length - 1

更一般地說,要編程,您需要知道如何調試。 調試通常是跟隨代碼並檢查它實際執行的操作與您希望它執行的操作。 您可以為此使用調試器,或者,您可以添加大量 System.out 語句以進行跟進。

這樣做,你會發現你的邏輯錯誤。 我已經在第一段中給了你相當大的提示;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM