![]() ![]() Int combo = compare(guess, code, s -> s. Int correct = compare(guess, code, Stream::count) //count elems Return //guess was correct, can also just compare correct to guess length Scanner scanner = new Scanner(System.in) var num Math. So, the user guessed immediatly without doing nothing. Streams clean this up a little bit, however I removed print statements here so that the logic can be seen a little clearer: String code = "53492" Just initialize it with : var guess 0 However be careful there's a possibility that num is initialize to 0. For quality of life, after the user has clicked the Guess button, re-select the guess entry. Consider telling the user what their minimum and maximum guess is permitted to be. ("You ran out of guesses! The code was " + code) Consider using a spinbox instead of an unconstrained entry box for the guess, so that lower and upper limits are automatically enforced. After each guess, the code maker gives feedback in. Stack Exchange Network Stack Exchange network consists of 181 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. ("You have " + right + " number(s) correct that equal to " + combo + " and you have " + guesses + " guesses left.") The players then alternate, trying to guess their opponents number. The program allows you to guess a number and also returns the attempts. The player is asked for repeated guesses until the the target number is guessed correctly At each guess. Right++ //keeps track of how many are right The computer chooses a number between given set limits. If(correctInt = codeGuess) //if the num at the position matches the code at the right positionĬombo += correctInt //totals up the correct amount of numbers in right spot Int codeGuess = Integer.parseInt(getCodeGuess) If your guess is too high or too low, you'll get a hint. String getCodeGuess = code.substring(x, x + 1) Guess the number - App Lab - Guess the Number Guess the magic number in the my hat. String getCorrectInt = guess.substring(x, x + 1) ĬorrectInt = Integer.parseInt(getCorrectInt) //stores value of num at the value of You have 10 guesses: ") įor(int i = 0 i < 10 i++) //will allow you to guess a certain amount of timesįor(int x = 0 x < 5 x++) //starts searching at first spot(0) Guess the number is a simple and classic game that is often used as a way to introduce people to basic programming concepts. Also this is in a main method, i just didn't copy the class name or the main method) String code = "53492" (I apologize in advance for any sloppy coding or poor choice of variable names. I could use arrays (although i'm not the best with them yet) to find certain values, but i'm not positive on how to do that. I know there has to be a much simpler way to do this, I just can't put one together. If you have any numbers in the right spot, it will tell you how many you have correct and the value of the correct numbers so you can figure it out. In this program, there is a code that the user has to try to solve in 10 guesses. ![]()
0 Comments
Leave a Reply. |