How do I use the remainder? [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 8 years ago.
Improve this question
I have a problem to solve!
Create a program that asks the user for a number and tells whether the number is even or odd.
Type a number: 2
Number 2 is even.
Type a number: 7
Number 7 is odd.
Hint: The number's remainder when dividing by 2 tells whether the number is even or odd. The remainder can be obtained with the % operator.
I wrote this code but I'm stuck:
import java.util.Scanner;
public class EvenOrOdd {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
// Type your program here
System.out.print("Type a number: ");
int number = Integer.parseInt(reader.nextLine());
System.out.println("");
if (number % 2 == 0);
System.out.println("Number" + number + " is even.");
else {
System.out.println("Number" + number + " is odd.");
}

The ; terminates the statement. You have mistakenly used it in your if. Fix it to:
if (number % 2 == 0) {
System.out.println("Number" + number + " is even.");
} else {
System.out.println("Number" + number + " is odd.");
}

Related

How do i end a loop using a different character like "x" or so [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 12 months ago.
This post was edited and submitted for review 12 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have this code which is a minigame, you type a number and if is divisible by 5 you get a "Fizz" / if divisible by 3 you get a "Buzz" / and if divisible by both, you get a "FizzBuzz".
Now, i need help to end the "while(true)" loop, peacefully with a mesage like... "goodbye" whenever you enter the letter "X".
I would apreciate if you can add the aswer to the full code and a little explain of the code.
public class CLass {
public static void main(String[] args) {
int number;
Scanner scan = new Scanner(System.in);
while (true) {
System.out.print("number: ");
number = scan.nextInt();
if (number % 5 == 0 && number % 3 == 0)
System.out.println("FizzBuzz");
else if (number % 5 == 0)
System.out.println("Fizz");
else if (number % 3 == 0)
System.out.println("Buzz");
else
System.out.println(number);
}
}
}
You can do something like:
final Scanner scan = new Scanner(System.in);
String input;
do {
input = scan.next();
if(!Objects.equals(input, "x")){
System.out.println("your input is: " + input);
...
}
} while(!Objects.equals(input, "x"));
System.out.println("Goodbye");
And if you need to convert the string into int, take a look on some solutions here:
How to check if a String is numeric in Java

While statement isn't excuting inside if statement? [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 1 year ago.
Improve this question
I am trying to make it print out a range of specified numbers. So if they select option 1 and put in 1 and 15, I want it to print out 1 to 15. Once it gets to the while statement though it just prints nothing.
import java.util.Scanner;
public class Lab4 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.print("Please choose your choice from the following menu");
System.out.print("\n1) Print through all integer numbers between two given integers");
System.out.print("\n2) Display a right triangular pattern of stars");
System.out.println("\n3) Quit");
int userInput = in.nextInt();
if (userInput == 1) {
System.out.print("Enter the start number: ");
int firstInteger = in.nextInt();
System.out.print("Enter the second number: ");
int secondInteger = in.nextInt();
while (firstInteger < secondInteger);
System.out.print(firstInteger);
firstInteger++;
} else if (userInput == 2) {
System.out.print("Enter the height: ");
int triangleHeight = in.nextInt();
} else if (userInput == 3);{
System.exit(userInput);
}
in.close();
}
}
You should change :
while (firstInteger < secondInteger);
System.out.print(firstInteger);
firstInteger++;
to
while (firstInteger < secondInteger) {
System.out.print(firstInteger);
firstInteger++;
}
while (firstInteger < secondInteger);
This will be treated as two executable lines of instructions,
which is similar like
while (firstInteger < secondInteger)
;
Try to remove the ; after while statement

Can do-while loops only run with boolean variables? Java [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 2 years ago.
Improve this question
I am having some trouble with this small segment. Can do-while loops only run using boolean variables? I am trying to ask the "user" to enter "0" or "1" so that the program will either loop or end.
Error message:
Chapter4Practice.java:23: error: incompatible types: int cannot be
converted to boolean
} while (choice = 1);
^ 1 error
My code:
import java.util.Scanner;
public class Chapter4Practice
{
public static void main(String[] args)
{
int choice, num1, num2;
String input;
Scanner sc = new Scanner(System.in);
do {
System.out.print("Enter a number: ");
num1 = sc.nextInt();
System.out.print("Enter another number: ");
num2 = sc.nextInt();
System.out.println("The sum is " + (num1 + num2));
System.out.println("Do you want to do this again?");
System.out.println("(1 = yes, 0 = no)");
sc.nextLine();
choice = sc.nextInt();
} while (choice = 1);
} //End Main
} //End Class
Short answer: Yes, you can have simple or complex statements inside your do-while but at the end it will have to evaluate to either true or false
Also your statement should be == ( single = means assign, where == is evaluate)

need to determine the positive, negative and zero numbers in program and add all the positive and negative numbers separately [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to determine the positive, negative and zero numbers in a program and add all the positive and negative numbers separately. I'm using while loop (can use do-while) because for loop and array is not allowed. Badly need your help. Here's my code. The code should allow entering 10 numbers before determining.
public class Mix22 {
public static void main(String[] args) {
Scanner ety = new Scanner(System.in);
int count=0;
int positive=0;
int negative =0;
int num=0;
System.out.println("Enter a number: ");
num = ety.nextInt();
while(num!=10){
if(num<0)
negative++;
if (num>0)
positive++;
System.out.println("Enter a number: ");
num = ety.nextInt();
}
System.out.println("Negative numbers in the program: " + negative);
System.out.println("Positive numbers in the program: " + positive);
}
}
Is the problem that you want to run the loop 10 times? You've got a count variable that you are not otherwise using. The loop should look something like:
int count=0;
while (count != 10) {
...
++count;
}
Conventionally a for loop is used for this (if allowed):
for (int count=0; count<10; ++count) {
...
}

How could I add a while loop into my code when there's already if statements [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 5 years ago.
Improve this question
Have seen other posts, but I don't know when to enter the while loop or for loop for my program run until it meets conditions, which is enter a number between 1 and 20 and the run the code.
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number, total;
System.out.println("Please enter an integer from 1 to 20:");
number = scanner.nextInt();
if (number >= 21){
System.out.println("Your integer must be between 1 and 20.");
}
else if ( number <= 0){
System.out.println("Your integer must be between 1 and 20.");
}
else {
for(int i = 1; i<=20; i++){
total = number * i;
System.out.println(i + " X " + number + " = " + total);
}
}
}
}
Put a while loop around the scanner input
while(number < 0 || number > 20)
{
number = scanner.nextInt();
// if statements here
}
With this, you will need to initialize number so that you get into the loop
int number = -1;

Categories