Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Trying To Get User Input As Int In Simon Game
    text
    copied!<p>I'm trying to figure out why 1) my Simon game hangs up after saying "Enter A Number"- it doesn't look like it even gets past validation. I'm trying to get user input, and check to see if that was the right number to press at that time. 2) Also, it used to generate a random number but when the user pressed it, it came back as false for some reason. Some other random number would pass though. 3) Also, is the code below color coded for you? Thanks guys.</p> <pre><code>import acm.program.*; import acm.graphics.*; import java.awt.Color; import java.awt.Font; import javax.swing.*; import java.awt.event.*; import javax.swing.JOptionPane; import java.util.Scanner; import java.io.DataInputStream; public class Simon extends Program implements ActionListener { Scanner usersInputScanner = new Scanner(System.in); private int array[]; private int currentSeqLength; private int usersInput; private String usersInputString; public Simon() { //Initialize Class Values array = new int[20]; currentSeqLength = 1; usersInput = 0; generateSequence(); while(currentSeqLength &lt; array.length) { playSequence(); //Wait For User's Input, Assign To Variable System.out.println("Enter A Number"); usersInput = usersInputScanner.nextInt(); if (pushButton(usersInput) == true) { System.out.println("You Entered: " + usersInput); currentSeqLength++; } else { gameOverMessage(); break; //Reset Variables: } } } //----------------------- Methods Here On Down ----------------------------- public void generateSequence() { //Fill Array With Random Numbers for (int i = 0; i &lt; array.length; i++ ) { array[i] = (int)(Math.random()*4); } } public void setLength(int length) { //Set Current Length To Size Of Given Argument currentSeqLength = length; } int getLength() { return currentSeqLength; } int[] playSequence() { //Print Out The Current Sequence //New Local Array To Return int newArray[]= new int[currentSeqLength]; //Repeat As Many Times As Value Of currentSeqLength for(int i = 0; i &lt; currentSeqLength ; i++) { System.out.println(array[i]); //Return an array of int's to the player. newArray[i] = array[i]; } return newArray; } boolean pushButton(int usersInput) { //Given A Button Press (0-3), Return Whether That Was The //Correct Button To Play At The Moment if (usersInput == array[currentSeqLength]) { return true; } else { return false; } } boolean isTurnOver() { //If Current Sequence Length Matches Or Exceeds Value Of //Array Element In Location Of Current Sequence Length if (currentSeqLength &gt;= array[currentSeqLength]) { return true; } else { return false; } } //Not Needed? boolean isGameOver() { if (pushButton(usersInput) == false) { return true; } else { return false; } } String gameOverMessage() { return "Game Over"; } /*public void actionPerformed(ActionEvent event) { int input; } */ } </code></pre>
 

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