boolean is assigned but never accessed [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm just trying to create a GuessMyNumber game and here is my code:
import java.util.Random;
import java.util.Scanner;
public class classic {
public static void main(String[] args) {
Random rand = new Random();
boolean beaten = false;
int number;
int randn = rand.nextInt(99);
//System.out.println(randn);
System.out.println("What is my number? Guess it!");
Scanner input = new Scanner(System.in);
int counter = 0;
int counter2 = 0;
while(beaten = true){
number = input.nextInt();
if (number == randn) {
System.out.println("Correct!");
beaten = true;
}
if (number < randn) {
System.out.println(number + " is too low");
}
if (number > randn) {
System.out.println(number + " is too high");
}
}
}
}
In the while loop I set beaten = true but still, while loop continues
What am I doing wrong here?

while(beaten = true)
As = is an assignment operator, so true is assigned into beaten first, then while loop condition becomes,
beaten = true
while(true) {
//.... hence, loop executed.
}
Use comparison operator == for condition checking. Moreover, not ! operation can be used also for boolean variable.
Say,
beaten = true
Then
while(!beaten)
means
while(!true) // read while not true
//implies
while(false) {
//.... loop will not going to execute.
}

import java.util.Random;
import java.util.Scanner;
public class Guess {
public static void main(String[] args) {
Random rand = new Random();
boolean beaten = false;
int number;
int randn = rand.nextInt(99);
//System.out.println(randn);
System.out.println("What is my number? Guess it!");
Scanner input = new Scanner(System.in);
int counter = 0;
int counter2 = 0;
while(beaten == false ){
number = input.nextInt();
if (number == randn) {
System.out.println("Correct!");
beaten = true;
}
if (number < randn) {
System.out.println(number + " is too low");
counter++;
}
if (number > randn) {
System.out.println(number + " is too high");
counter++;
}
}
System.out.print("It took you " + counter + " tries to gues the number");
}
}
Now it works. You set "beaten" to false originally so it wasn't working and I also changed your while loop condition too "beaten == false" instead of "beaten = false" Hope this helps. I didn't know what you wanted to do with the counter so I just added that into the bottom to count how many tries it took.

Related

A java program to prompt the user to guess a number chosen by the computer at random [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am a beginner in Java and I was trying to make this program to choose a random number between 1 and 1000 and prompt the user to guess that number but the program doesn't seem to work and I don't know what's wrong with it.
package com.company;
import java.util.Scanner;
public class GradeBookTest {
public static void main(String[] args) {
int num;
Scanner input = new Scanner(System.in);
guess();
System.out.println("Guess a number between 1 and 1000.");
num = input.nextInt();
if (num >= 1 && num <= 1000)
{
while (checkNumber(num) != true)
{
System.out.println("Guess again");
num = input.nextInt();
checkNumber(num);
}
System.out.println("Congratulations. You " +
"guessed the number!");
}
}
public static int guess() {
return ( (int) (1 + Math.random()*1000) );
}
public static boolean checkNumber(int a){
int ans = guess();
if (a < ans)
{
System.out.println("low");
return false;
}
else if (a > ans)
{
System.out.println("high");
return false;
}
else
return true;
}
}
Try this
public class Guess {
public static void main (String [] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
// Generates the Random Number
int randomNumber = (int)(Math.random() * 1000);
System.out.print("Guess a number between 1 and 1000: ");
// Get user guess
int guess = input.nextInt();
while (guess != randomNumber) {
// Informs the user whether his guess is high or low
if (guess > randomNumber) {
System.out.println("Your guess is high");
} else {
System.out.println("Your guess is low");
}
// Asks the user for another guess
System.out.print("Make another guess : ");
guess = input.nextInt();
}
System.out.println("Congratulations ! You guessed the number");
}
}

How to repeat if statements in while loops [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to have a user guess a number picked by a random number generator, but my code decides to, if it doesn't equal, stick within one if statement.
System.out.println("Enter a guess between 1 and 200: ");
String guess = input.nextLine();
int userInput = Integer.parseInt(guess);
Random rnd = new Random(seed);
int x = rnd.nextInt(200) + 1;
int currentInt = 1;
String msg = "That was impossible!";
and my while loop contains many if statements:
boolean boo = false;
while (!boo) {
if (userInput == x) {
System.out.println("Congratulations! Your guess was correct!");
System.out.println("");
System.out.println("I had chosen " + x + " as the target number.");
System.out.println("You guessed it in " + currentInt + " tries.");
if (currentInt == 1) {
//do something
} else if (currentInt >= 2) {
//do something
} else if (currentInt >= 4) {
...
} else if (currentInt >= 11) {
msg = "Maybe you should play something else";
System.out.println(msg);
}
break;
} else if (userInput > x) {
System.out.println("Your guess was too high - try again.");
System.out.println("");
System.out.println("Enter a guess between 1 and 200: ");
String highGuess = input.nextLine();
int tooHigh = Integer.parseInt(highGuess);
continue;
} else if (userInput < x) {
System.out.println("Your guess was too low - try again.");
System.out.println("");
System.out.println("Enter a guess between 1 and 200: ");
String lowGuess = input.nextLine();
int tooLow = Integer.parseInt(lowGuess);
continue;
}
currentInt = currentInt + 1;
}
My code works decently, if the answer is correct in the first try then the first if statement works, but if it's greater or less than x, then the same block runs (if greater, keeps putting greater even if next input is less than)
Update userInput with tooHigh and tooLow .

Making a guess a number game, but it won't print anything out

I was wandering, why this code doesn't print out anything?
package Main;
import java.util.Random;
import java.util.Scanner;
public class Counter {
public static void main(String[] args) {
Random rand = new Random();
int numberOfTries = 0;
int numberToGuess = rand.nextInt(1000);
Scanner input = new Scanner(System.in);
int guess;
boolean win = false;
while (win = false) {
System.out.println("Guess a number between 0 and 1000");
guess = input.nextInt(50);
numberOfTries++;
{
System.out.println("Number of Tries is " + numberOfTries);
}
if (guess == numberToGuess) {
System.out.println("You win");
win = true;
} else if (guess < numberToGuess) {
System.out.println("Number is higher");
} else if (guess > numberToGuess) {
System.out.println("Number is Lower");
}
}
}
}
There is an error in the while statement, it should be while (win == false). A single = acts as assignment operator and consequently return the same value of the assignment (false). The proper logical operator to test equality is ==.
Your code has two problems. The = in while (win = false) { is the assignment operator, not the logical operator. Instead you should have
while (win == false) {
Second, the nextInt method in the Scanner class does not take an argument. Remove the 50 from guess = input.nextInt(50);
guess = input.nextInt();
I was able to run your program after those two modifications.
Change
while (win = false) // this is not a logical operator
to
while (win == false) // this is
Also note you might be confusing
rand.nextInt(50); // generates a random number within the bound = 50
with
guess = input.nextInt(50); // expects the user input which should be of type int using radix 50
So you shall change this to
guess = input.nextInt();

Figuring out odd even and zeros [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am having problems with my java program. I have to input a value then print out the number of odds, evens, and zeroes. The odds and zeroes display fine but the evens display total digits.
import java.util.Scanner;
public class OddEvenZero
{
public static void main(String[] args)
{
int even = 0;
int odd = 0;
int zero = 0;
int placeInValue;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a Value: ");
String valueEntered = scan.nextLine();
for(placeInValue = 0; placeInValue < valueEntered.length(); placeInValue ++)
{
char value = valueEntered.charAt(placeInValue);
int numberUsedInLoop = Integer.parseInt(Character.toString(value));
if(numberUsedInLoop == 0)
{
zero ++;
}
else if(numberUsedInLoop%2 == 0);
{
even ++;
}
if(numberUsedInLoop%2 != 0 && numberUsedInLoop != 0)
{
odd ++;
}
}
System.out.println("Number of Zeroes in Number: " + zero);
System.out.println("Number of Evens in Number: " + even);
System.out.println("Number of Odds in Number: " + odd);
}
}
Output:
Enter a Value:
225500
Number of Zeroes in Number: 2
Number of Evens in Number: 6
Number of Odds in Number: 2
Any help is appreciated.
The semicolon terminates the else if immediately here
else if(numberUsedInLoop%2 == 0); // <-- terminates the else if
{ // <-- raw block
even ++;
}
change it to something like
else if(numberUsedInLoop%2 == 0)
{
even ++;
}
else // <-- just an else should satisfy your conditions
{
odd ++;
}

only last value of array is compared [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int randnum = 0;
boolean lose = false;
Random gen = new Random();
Scanner scan = new Scanner(System.in);
System.out.println("Enter desired numbers to be drawn");
int print = scan.nextInt();
System.out.println("Enter desired numbers on dice");
int dice = scan.nextInt();
System.out.println("Enter your lucky numbers");
int[] numbers = new int[print]; // int and print rhyme
for(int i=0; i<print;i++){
numbers[i] = scan.nextInt();
}
for(int counter=1; counter<=print;counter++){
randnum = 1+gen.nextInt(dice);
System.out.println(randnum + " ");
System.out.println();
if (randnum == numbers[counter - 1]){
lose = false;
} else {
lose = true;
}
if (lose == true){
System.out.println("Bad luck!");
}
if (lose == false){
System.out.println("Winner winner chicken dinner!");
}
}
}
}
I am making a simple lotto game. If I correctly guess the numbers I will still get the losing outcome unless it was the last number I guessed. How do I compare randnum to all numbers that were inputted?
If you only want one randnum, take the definition for randnum out of the for loop. You can break out of your loop as soon as you find a match.
randnum = 1 + gen.nextInt(dice);
System.out.println(randnum + " ");
System.out.println();
for(int counter=1; counter <= print; counter++){
if (randnum == numbers[counter - 1]){
lose = false;
break;
} else {
lose = true;
}
}
If you want multiple randnums, you need two loops.
for(int counter = 1; counter <= print; counter++){
randnum = 1 + gen.nextInt(dice);
System.out.println(randnum + " ");
System.out.println();
for(int i = 1; i <= print; i++){
if (lose == false){
break;
}
if (randnum == numbers[i - 1]){
lose = false;
break;
} else {
lose = true;
}
}
}
Make sure to initialize lose as true;
boolean lose = true;
You need to take the rest of the if statements out of the for loop.
if (lose == true){
System.out.println("Bad luck!");
}
if (lose == false){
System.out.println("Winner winner chicken dinner!");
}

Categories