Math Practice issues - java

I have to do this project for my programming class that requires us to prompt the user for how many math problems they want to solve, the difficulty they desire (easy or hard), and then to randomly ask math problems based on their input. For some reason, I cannot get the code to operate past asking for the difficulty level. I think the issue is the way I'm trying to randomly select operators, but I'm not sure. Here's my code:
import java.util.Scanner;
import java.util.Random;
public class MathPractice {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
int answer;
int count = 0;
int solution;
int correct = 0;
int numEasy1 = rand.nextInt(10);
int numEasy2 = rand.nextInt(10);
int numHard1 = rand.nextInt(20) - 9;
int numHard2 = rand.nextInt(20) - 9;
int sign = rand.nextInt(4);
System.out.print("How many problems do you want? ");
int number = keyboard.nextInt();
System.out.print("What level difficulty would you prefer (easy/hard)? ");
String difficulty = keyboard.next();
if (difficulty == "easy" || difficulty == "Easy") {
while (count < number) {
if (sign == '0') {
System.out.print(numEasy1+" + "+numEasy2+" = ");
answer = keyboard.nextInt();
solution = numEasy1 + numEasy2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
} else if (sign == '1') {
System.out.print(numEasy1+" - "+numEasy2+" = ");
answer = keyboard.nextInt();
solution = numEasy1 - numEasy2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
} else if (sign == '2') {
System.out.print(numEasy1+" * "+numEasy2+" = ");
answer = keyboard.nextInt();
solution = numEasy1 * numEasy2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
} else if (sign =='3') {
System.out.print(numEasy1+" / "+numEasy2+" = ");
answer = keyboard.nextInt();
solution = numEasy1 / numEasy2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
}//inner if statement
}//while loop
} else if (difficulty == "hard" || difficulty == "Hard") {
while (count < number) {
if (sign == '0') {
System.out.print(numHard1+" + "+numHard2+" = ");
answer = keyboard.nextInt();
solution = numHard1 + numHard2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
} else if (sign == '1') {
System.out.print(numHard1+" - "+numHard2+" = ");
answer = keyboard.nextInt();
solution = numHard1 - numHard2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
} else if (sign == '2') {
System.out.print(numHard1+" * "+numHard2+" = ");
answer = keyboard.nextInt();
solution = numHard1 * numHard2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
} else if (sign == '3') {
System.out.print(numHard1+" / "+numHard2+" = ");
answer = keyboard.nextInt();
solution = numHard1 / numHard2;
count++;
if (answer == solution) {
System.out.println("Correct!");
correct++;
count++;
} else {
System.out.println("Incorrect. Correct answer is "+solution);
count++;
}
}//inner if statement
}//while loop
} //if statement
System.out.println("You got "+correct+" correct out of "+number+".");
}
}

try using nextLine. which will consume new line character after the input
System.out.print("How many problems do you want? ");
int number = keyboard.nextInt();
keyboard.nextLine();
System.out.print("What level difficulty would you prefer (easy/hard)? ");
String difficulty = keyboard.next();
and use equals method to compare string values ,== will compare the object reference.

Your code if (sign == '0') { is comparing an integer form the rand.nextInt() function to a char. Change '0' to just 0 for all your if statements

You need to change if (difficulty == "easy" || difficulty == "Easy") to if (difficulty.equalsIgnoreCase("easy")) and do the same for the "hard" loop.
Also, you have sign declared as an int but you compare it as if it were a char. change all your (sign == '3') lines to (sign == 3) and you should be good.

Related

Issues with counter and for loop. Assitance appreciated

Having issues with my for loops and the counter it should add up each value of the numbers I have added and ask me what that value is, at the end of the for loop. I added the number-2 to end the loop, so I tried to subtract it from the total before printing, but it doesn't work.
Help!
import java.util.*;
public class CountingCards {
public static void main(String[] args) {
Counting();
}
public static void Counting() {
Random rand = new Random();
int count = 0;
int sum = 0;
Scanner in = new Scanner(System.in);
for (int a = 0; a < 110; a++) {
int player = rand.nextInt((10) + 1);
System.out.println(" ");
System.out.println("You got this card: " + player);
System.out.println("What is the value of that card");
int answer = in.nextInt();
if (player == 1) {
if (answer == -1) {
System.out.println("That is CORRECT");
count = -1;
}
if (answer != -1) {
System.out.println("Sorry, that is INCORRECT");
count = -1;
}
}
if (player == 2) {
if (answer == -1) {
count = -1;
System.out.println("That is CORRECT");
}
if (answer != -1) {
count = -1;
System.out.println("Sorry, that is INCORRECT");
}
}
if (player == 3) {
if (answer == -1) {
count = -1;
System.out.println("That is CORRECT");
}
if (answer != -1) {
count = -1;
System.out.println("Sorry, that is INCORRECT");
}
}
if (player == 4) {
if (answer == -1) {
count = -1;
System.out.println("That is CORRECT");
}
if (answer != -1) {
count = -1;
System.out.println("Sorry, that is INCORRECT");
}
}
if (player == 5) {
if (answer == -1) {
count = -1;
System.out.println("That is CORRECT");
}
if (answer != -1) {
count = -1;
System.out.println("Sorry, that is INCORRECT");
}
}
if (player == 6) {
if (answer == -1) {
count = -1;
System.out.println("That is CORRECT");
}
if (answer != -1) {
count = -1;
System.out.println("Sorry, that is INCORRECT");
}
}
if (player == 7) {
if (answer == 0) {
count = 0;
System.out.println("That is CORRECT");
}
if (answer != 0) {
count = 0;
System.out.println("Sorry, that is INCORRECT");
}
}
if (player == 8) {
if (answer == 0) {
count = 0;
System.out.println("That is CORRECT");
}
if (answer != 0) {
count = 0;
System.out.println("Sorry, that is INCORRECT");
}
}
if (player == 9) {
if (answer == 0) {
count = 0;
System.out.println("That is CORRECT");
}
if (answer != 0) {
count = 0;
System.out.println("Sorry, that is INCORRECT");
}
}
if (player == 10) {
if (answer == 1) {
count = 1;
System.out.println("That is CORRECT");
}
if (answer != 1) {
count = 1;
System.out.println("Sorry, that is INCORRECT");
}
}
if (answer == -2) {
sum = sum - 2;
System.out.println("Your total count is: " + sum);
break;
}
sum = sum + count;
}
}
}
the sum should add all the counters I've acquired, throughout the for loop, and ask me each time to see if the answer I give them adds to the total counter each time. It's ways off and I dont know why
I could not understand what exactly this game is; but looking at your code, I guessed that you have made a logical mistake in the following line:
int player = rand.nextInt((10) + 1);
It should be
int player = rand.nextInt(10) + 1;
Second thing is, you can replace the following code:
if (answer == -1) {
System.out.println("That is CORRECT");
count = -1;
}
if (answer != -1) {
System.out.println("Sorry, that is INCORRECT");
count = -1;
}
with
if (answer == -1) {
System.out.println("That is CORRECT");
count = -1;
} else {
System.out.println("Sorry, that is INCORRECT");
count = -1;
}
For more help, you need to clearly mention the rules of the game.
Changing
int player = rand.nextInt((10) + 1);
to
int player = rand.nextInt(10) + 1;
should solve your problem. Hope it helps..
Have a good day !

How to make code loop with boolean specified at the end of code?

Try to make my code so that I can ask the user if they want to play again, however I'm confused how I'm supposed to go about this.
Scanner in = new Scanner(System.in);
int randomNum = 1 + (int)(Math.random() * 100);
System.out.println(randomNum);
Boolean playAgain = true;
while (playAgain) {
System.out.print("I'm thinking of a number between 1 and 100.\nWhat is it?\nGuess: ");
int guessNum = in.nextInt();
while ((guessNum > randomNum) || (guessNum < randomNum)) {
if (guessNum > randomNum) {
System.out.print("Too High.\nGuess: ");
guessNum = in.nextInt();
} else if (guessNum < randomNum) {
System.out.print("Too Low.\nGuess: ");
guessNum = in.nextInt();
}
}
System.out.println("You got it!\nPlay again? (Y/N) ");
String answer = in.next();
if (answer == "y") {
playAgain = true;
} else {
playAgain = false;
System.out.println("Thanks for playing!");
}
}
You have used == equals Relational Operator which usually used to check string reference not the value actually. So, even if answer contains y then your conditional expression never returns true as a Result.
For Reference : Visit Here For More Info ( '==' vs 'equals()' method)
String answer = in.next();
//problem with this line becuase == operator use to check reference instead of value
if (answer == "y") {
playAgain = true;
} else {
playAgain = false;
System.out.println("Thanks for playing!");
}
So change your code as like below
String answer = in.next();
if (answer.startsWith("y") || answer.startsWith("Y")) { // Now this willl work fine to your code
playAgain = true;
} else {
playAgain = false;
System.out.println("Thanks for playing!");
}
It May help You
do {
System.out.print("I'm thinking of a number between 1 and 100.\nWhat is it?\nGuess: ");
int randomNum = 1 + (int) (Math.random() * 100);
System.out.println(randomNum);
int guessNum = in.nextInt();
do {
if (guessNum > randomNum) {
System.out.print("Too High.\nGuess: ");
guessNum = in.nextInt();
} else if (guessNum < randomNum) {
System.out.print("Too Low.\nGuess: ");
guessNum = in.nextInt();
}
}while ((guessNum > randomNum) || (guessNum < randomNum));
System.out.println("You got it!\nPlay again? (Y/N) ");
answer = in.next();
}while(answer.equals("y"));
If you want to use playAgain var, then use
answer.equals("y")
to compare two strings instead of ==.
otherwise have your full code inside while block or do while. And if user wants to play, then do continue, otherwise do break;
while(true)
{
// your code
String answer = in.next();
if (answer == "y") {
continue;
} else {
System.out.println("Thanks for playing!");
break;
}
}

How to automatically output strings in loops

So I just have a quick little issue
int pickmeup = 0;
while (true)
{
pickmeup = scanner.nextInt();
if (pickmeup == 1)
{System.out.println ("you entered 1");}
if(pickmeup == 2)
{System.out.println ("you entered 2");}
{
break;
}
System.out.println ("Invalid code");
Now when I run this code it all works fine however in regards to the strings but it seems as though the loop doesn't work all that well when I enter '3', as it doesn't return the string 'Invalid code'.
If I were to get rid of the strings after both if statements, then it works perfectly fine. What exactly am I doing wrong? Are there other ways to automatically have strings output?
I believe you want to use a logical or || and an else like,
int pickmeup;
while (true) {
pickmeup = scanner.nextInt();
if (pickmeup == 1 || pickmeup == 2) {
System.out.printf("you entered %d%n", pickmeup);
} else {
System.out.println("Invalid code");
}
}
Alternatively, you could use an else if chain like,
int pickmeup;
while (true) {
pickmeup = scanner.nextInt();
if (pickmeup == 1) {
System.out.println("you entered 1");
} else if (pickmeup == 2) {
System.out.println("you entered 2");
} else {
System.out.println("Invalid code");
}
}
You could start with firstly correcting your code, you can do that in eclipse Source-Format or by pressing CTRL+SHIFT+F
For your example, I corrected as much as I understood, currently it breaks only if else is reached. Break can be modified.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int pickmeup = 0;
while (true){
pickmeup = scanner.nextInt();
if (pickmeup == 1){
System.out.println("one");
}
else if (pickmeup == 2){
System.out.println("two");
}
else{
System.out.println("Invalid code");
break;
}
}

While loops having trouble

I have tried my code like below..
public class GuessNumber {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int number = rand.nextInt(100) + 1;
int guess;
System.out.println("Guess the number between 1 and 100");
System.out.println("");
guess = scan.nextInt();
while (guess < number) {
System.out.println("Higher!");
guess = scan.nextInt();
}
while (guess > number) {
System.out.println("Lower!");
guess = scan.nextInt();
}
while (guess == number) {
System.out.println("Correct!");
break;
}
}
}
and I'm having trouble making it ask until the user gets the right number. And at the end for the game to ask if the user wants to play the game again
Your use of loops here
while (guess < number) {
System.out.println("Higher!");
guess = scan.nextInt();
}
while (guess > number) {
System.out.println("Lower!");
guess = scan.nextInt();
}
while (guess == number) {
System.out.println("Correct!");
break;
}
is creative. It happens to be flawed, but it's very creative. You need something like
while (guess != number) {
System.out.println((guess < number) ? "Higher!" : "Lower!");
guess = scan.nextInt();
}
System.out.println("Correct!");
The logic is not quite right. You want one while loop to check each condition and continue asking for a new number while the guess does not equal the number.
while (guess != number) {
if (guess < number)
System.out.println("Higher!");
else
System.out.println("Lower!");
guess = scan.nextInt();
}
System.out.println("Correct!");
while (true) {
if(guess < number)
System.out.println("Higher!");
else if(guess > number)
System.out.println("Lower!");
else if (guess == number){
System.out.println("Correct!");
}
guess = scan.nextInt();
}
you need only a single while loop. rearrange your logic in your loop.
boolean isRunning = true;
while(isRunning){
guess = scan.nextInt();
if(guess < number){
System.out.println("Higher!");
}else if(guess > number){
System.out.println("Lower!");
}else if(guess == number){
System.out.println("Correct!");
//ask if need another input. (want to continue or not)
if(not continue){
break; or isRunning = false;
}
}
}

Unreachable statement?

Here is my coding:
For some reason, at the very end, when I'm trying to return winscounter and losscounter, it says unreachable statement but not for the tiescounter. I can't figure out why! If anyone can answer this, it would be greatly appreciated!!
public class RockPaperScissors {
/**
* #param args the command line arguments
*/
static int value; //computer's choice
static int choice; //user choice
static int tiescounter = 0;
static int winscounter = 0;
static int losscounter = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));// user input
int repeat;
do {
System.out.println("ROCK PAPER SCISSORS"+
"\n===================");
System.out.println("\n1=Rock" +
"\n2=Paper" +
"\n3=Scissors" +
"\n===========" +
"\nChoose:");
choice = Integer.parseInt(br.readLine());
if (choice !=1 && choice !=2 && choice !=3) {
do{
System.out.println("\nError. Please choose Rock, Paper or Scissors.");
choice = Integer.parseInt(br.readLine());
}
while (choice !=1 && choice !=2 && choice !=3);
}
System.out.println();
if (choice == 1){
System.out.println("You have chosen Rock.");
}
else if (choice ==2){
System.out.println("You have chosen Paper.");
}
else if(choice == 3){
System.out.println("You have chosen Scissors.");
}
randomWholeNumber();
if (value == 1){
System.out.println("The computer has chosen Rock." );
}
else if (value == 2){
System.out.println("The computer has chosen Paper." );
}
else if (value == 3){
System.out.println("The computer has chosen Scissors." );
}
determineOutcome();
System.out.println("Ties:"+ tiescounter);
System.out.println("Wins: " + winscounter);
System.out.println("Losses: " + losscounter);
repeat = Integer.parseInt(br.readLine());
}
while (repeat==1);
}
public static int randomWholeNumber(){
do{
value=0;//resets random number
//generates and returns a random number within user's range
value = (int) ((Math.random()*3)+1);
}
while((value>3)||(value<1));
return (value);
}
public static int determineOutcome(){
if (value == choice){
System.out.println("\nYOU'VE TIED");
do{
tiescounter+=1;
}
while (tiescounter != tiescounter);
}
else if (value == 1){ //Rock
if (choice == 2){ //Paper
System.out.println("\nYOU'VE WON");
do{
winscounter +=1;
}
while (winscounter != winscounter);
}
else if (choice == 3){ //Scissors
System.out.println("\nYOU'VE LOST");
do{
losscounter+=1;
}
while(losscounter!=losscounter);
}
}
else if (value == 2){ //Paper
if (choice == 1){ //Rock
System.out.println("\nYOU'VE LOST");
do{
losscounter+=1;
}
while(losscounter!=losscounter);
}
else if (choice == 3){ //Scissors
System.out.println("\nYOU'VE WON");
do{
winscounter +=1;
}
while (winscounter != winscounter);
}
}
else if (value == 3){ //Scissors
if (choice == 1){ //Rock
System.out.println("\nYOU'VE WON");
do{
winscounter +=1;
}
while (winscounter != winscounter);
}
else if (choice == 2){ //Paper
System.out.println("\nYOU'VE LOST");
do{
losscounter+=1;
}
while(losscounter!=losscounter);
}
}
return(tiescounter);
return(winscounter);
return(losscounter);
}
}
You cannot return 3 different variables from one method, all at the same time. In this case, the return(tiescounter); always executes, terminating the method then and there. Hence the next 2 lines become unreachable.
Declare determineOutcome() method as void i.e. public static void determineOutcome(), and remove all return statements inside it. Your program will work.
A function can only return one value. As soon as return(tiescounter); is executed the function exits. If you want to return all three values you will have to wrap them in a class.
By the way return(tiescounter); can be written as return tiescounter; Parenthesis around return values is not required. Both statements will have the same result.

Categories