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 ++;
}
Related
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 3 years ago.
Improve this question
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(i=1; i<=n; i++)
{
if(i%3==2 && i%5==0)
{
System.out.println("Bus");
}
if(i%3==1 && i%5==0)
{
System.out.println("bUs");
}
if(i%3==0 && i%5==0)
{
System.out.println("buS");
}
System.out.println(i+" ");
}
The above program is also printing the number 5,10,15 but these should not print
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(i=1; i<=n; i++)
{
if(i%3==2 && i%5==0)
{
System.out.println("Bus");
}
else if(i%3==1 && i%5==0)
{
System.out.println("bUs");
}
else if(i%3==0 && i%5==0)
{
System.out.println("buS");
}
else
{
System.out.println(i+" ");
}
}
System.out.println does not mean it will not go through the next piece of code. You need either an else as above, or a switch statement or a break after the println.
So to explain fully, when i = 5
it goes to the first if statement, prints Bus, checks the second and third if statements and fails both, then it also runs the last println because there is no if statement around it.
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String result =
IntStream.range(1, Math.min(n, 100)).mapToObj(x ->
{
if (x % 5 == 0) {
switch (x % 3) {
case 2:
return "Bus";
case 1:
return "bUs";
case 0:
return "buS";
}
}
return x + "";
}
).collect(Collectors.joining(", "));
System.out.println(result);
}
}
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 .
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.
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!");
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I made a little program in Java where the user is being asked to pick three cards, ranging from 4 to 6. It's a bit of a sloppy code, but it works. Three random numbers (4 to 6) are generated. If the user guesses these numbers in the right order, he wins. Now, there should be an option for the user to change one of his guesses if he did not get all cards right the first time. This should be done by the user by inserting 1, 2 or 3 to retry. There should be no new random number generated. I did not learn this yet at school and did try some searching on the internet. Anyone who can give me some insight in doing this?
package cardgame;
import java.util.Scanner;
public class CardGame {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int[] guess = new int[3];
int[] card = new int[3];
System.out.println("Pick three cards with numbers ranging from 4 to 6!\n");
for (int i=0; i<3; i++){
System.out.print("Card number " + (i+1) + " (4, 5 or 6): ");
guess[i] = scan.nextInt();
}
System.out.println(" ");
System.out.println("Your hand of cards: " + "[" + guess[0] + "]" + "[" + guess[1] + "]" + "[" + guess[2] + "]");
for (int i=0; i<3; i++){
card[i] = (int) (Math.random() * 3 + 2 +2);
}
System.out.println("My hand of cards: " + "[" + card[0] + "]" + "[" + card[1] + "]" + "[" + card[2] + "]\n");
int count = 0;
for (int i=0; i<3; i++){
if (card[i] == guess[i])
count++;
}
if (count == 3){
System.out.println("Congratulations, you have won!");
} else{
System.out.println("I'm sorry, you lost!");
}
}
}
How about something like this:
System.out.println("Would you like to change one of your guesses? yes/no");
if(scan.next() == "yes")
{
System.out.println("What guess would you like to change? 1/2/3");
if(scan.nextInt() == 1 || scan.nextInt() == 2 || scan.nextInt() == 3)
{
int temp = scan.nextInt();
System.out.println("What is your new guess?");
guess[temp-1] == scan.nextInt(); // -1 because array index starts at 0
}
else
System.out.println("That's not a valid guess number");
}
This is a very basic example. You could make it as complex as you want. For example, by adding a loop prompting the user to ask again if their guess was invalid.