Thanks to everyone that helped with my last question...now i have another :0
to know what the program needs to do go here.
Here is the code i have so far (and it does basically all that it needs to do, but not all):
I also get an error at the end of this when i end up getting the correct number.
Here is the error message that i get:
Thanks for any help you guys/gals can give me.
to know what the program needs to do go here.
Here is the code i have so far (and it does basically all that it needs to do, but not all):
Code:
import java.util.Scanner;
public class Lab4
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in); //User input
int x = (int)(100 * Math.random()) + 1; //Generates random number
int userInt = 0; //user input number
char userAns1_1;
int counter = 0;
boolean hehe = true;
System.out.print("Do you want to play a game: ");
while (hehe == true)
{
String userAns1 = keyboard.nextLine();
userAns1_1 = userAns1.charAt(0);
char userAns2 = 'y'; //Compare to Yes or No answer from user
if (userAns1_1 != userAns2)
{
System.out.println("Goodbye!");
hehe = false;
}
else
{
System.out.println("\nI am thinking of a number between 1 and 100. Try to guess it.\n");
while (x != userInt)
{
System.out.print("What's your guess? ");
userInt = keyboard.nextInt();
if (x < userInt)
{
System.out.println(userInt + " is too big");
}
else if (x > userInt)
{
System.out.println(userInt + " is too small");
}
counter = counter + 1;
}
if (counter == 1)
{
System.out.println("You've got it in " + counter + " guesses. That was lucky!");
}
else if (counter >= 2 && counter <= 4)
{
System.out.println("You've got it in " + counter + " guesses. That was amazing!");
}
else if (counter >= 5 && counter <= 6)
{
System.out.println("You've got it in " + counter + " guesses. That was really good!");
}
else if (counter == 7)
{
System.out.println("You've got it in " + counter + " guesses. That was ok!");
}
else if (counter >= 8 && counter <= 9)
{
System.out.println("You've got it in " + counter + " guesses. That was pretty bad!");
}
else
{
System.out.println("You've got it in " + counter + " guesses. This is not your game!");
}
}
}
}
}
I also get an error at the end of this when i end up getting the correct number.
Here is the error message that i get:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:558)
at Lab4.main(Lab4.java:18)
Thanks for any help you guys/gals can give me.