Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning a value from a static void to a static void, that make use of public variables - some guidance please
    primarykey
    data
    text
    <p>Just so everyone knows I have literally just started writing C#, and this is practice.</p> <p>I found a GuessTheNumberGame code on the internet and have been trying to improve the basic game so that feedback is given, and the user can change the numbers. I had the program working but wanted to put the 'NumberChange' module separate from the 'Code' Method.</p> <p>I have made a Main() Method inside of my 'GuessTheNumberGame' with executes the 'Code' method. The thing is when I go to change the number range the 'NumberChange' module won't change the values of 'Public Static int from' or 'Public static int to'; and because of this the range of the numbers remains the same.</p> <p>Code below:</p> <pre><code>using System; namespace GuessThatNumber { class GuessTheNumberGame { static void Main() { Code(from, to, new_range); } public static int new_range = 0; public static int from = 1; public static int to = 10; static void Code(int from, int to, int new_range) { //int from = 1; // The range the user will guess from. //int to = 10; //The range the user will guess to. int guessedNumber; //This will hold the value that the user guessed. int Counter = 0; //Counter is used to count the number of guesses the user makes. int selection = 0; //This value is for the selection at the start of the program //int new_range = 0; bool exit = false; while (exit == false) { Console.WriteLine("What would you like to do?"); Console.WriteLine("1: Alter the range of guessed numbers? The range is currently from {0} to {1}.", from, to); Console.WriteLine("2: Try to guess the number?"); Console.WriteLine("3: Exit the program?"); Console.WriteLine("Please enter a number:"); if (int.TryParse(Console.ReadLine(), out selection)) { if (selection == 2) { int randomNumber = new Random().Next(from, to); //Generates a random number between the (from, to) variables. Console.Write("The number is between {0} and {1}. ", from, to); while (true) { Console.Write("Make a guess: "); if (int.TryParse(Console.ReadLine(), out guessedNumber)) { if (guessedNumber == randomNumber) { Console.WriteLine("You guessed the right number!"); if (Counter &lt; 2) { Console.WriteLine("You guessed the number in only 1 turn! Amazing!"); Console.WriteLine(" "); } else { Console.WriteLine("You guessed " + Counter + " times."); Console.WriteLine(" "); } break; } else { Console.WriteLine("Your guess was too {0}.", (guessedNumber &gt; randomNumber) ? "high" : "low"); Counter = Counter + 1; } } else { Console.WriteLine("Input was not an integer."); } } //Console.WriteLine(); //Console.WriteLine("Press any key to exit."); //Console.ReadKey(); } else if (selection == 1) { NumberChange(from, to, new_range); } else { exit = true; } } } } static int NumberChange(int from, int to, int new_range) { Console.WriteLine("Please enter the number that the guess will start from."); int.TryParse(Console.ReadLine(), out new_range); from = new_range; Console.WriteLine("Now enter the number that the guess will go to."); int.TryParse(Console.ReadLine(), out new_range); to = new_range; return new_range; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload