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)
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 1 year ago.
Improve this question
Multiple markers at this line - Syntax error, insert ";" to complete Statement - The left-hand side of an assignment must be a variable - Syntax error, insert "AssignmentOperator Expression" to complete Assignment - Syntax error on token "else", invalid (
import java.util.Scanner;
public class NGG {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x1, x2;
System.out.println("");
System.out.println("\n Guess X from 10 - 0: ");
x1 = scan.nextInt();
System.out.println("");
x2 = ((int)(Math.random()*10));
System.out.println(x2);
if (x2 == x1) {
System.out.println("Victory");
} else if(x2 > x1 && x2 < x1) {
System.out.println("Lose");
}
}
}
I made some changes to it.
nice game :)
public class NGG {
public static void main(String[] args) {
NGG ngg = new NGG();
ngg.game();
}
private void game(){
Scanner scan = new Scanner(System.in);
int enteredNumber, guessedNumber;
System.out.println("\n Guess number between 0 - 10: ");
enteredNumber = scan.nextInt();
guessedNumber = ((int)(Math.random()*10));
System.out.println(guessedNumber);
if (guessedNumber == enteredNumber) {
System.out.println("Victory");
} else {
System.out.println("Lost: number was " + guessedNumber);
}
}
}
did some minor refactoring and updated the logic to check if the guessed number/random number is same as the entered number then you get "victory" otherwise you Lose and shows you what the guessed/random number was.
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
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
After entering gender (M or F), the programs end immediately without getting to the switch-case
I've tried changing 'string' to "string" and nothing still changed. I thought that would make a difference
import java.util.Scanner;
public class FemurHumerus {
public static void main(String[] args) {
String gender;
double FemHum;
double MaleHum;
double FemFem;
double MaleFem;
int FemHumR;
int MaleHumR;
int FemFemR;
int MaleFemR;
Scanner scan = new Scanner(System.in);
System.out.println("What is your gender? (M or F): ");
gender = scan.nextLine();
switch (gender) {
case "F":
System.out.print("Enter femur length: ");
FemFem = in.nextInt();
FemFemR = (int) (FemFem*1.94+28.7);
System.out.print("Enter humerus length: ");
FemHum = in.nextInt();
FemHumR = (int) (FemHum*2.8+28.2);
System.out.println("Height based on femur: "+FemFemR);
System.out.println("Height based on humerus: "+FemHumR);
break;
case "M":
System.out.print("Enter femur length: ");
MaleFem = in.nextInt();
MaleFemR = (int) (MaleFem*1.88+32);
System.out.print("Enter humerus length: ");
MaleHum = in.nextInt();
MaleHumR = (int) (MaleHum*2.9+27.9);
System.out.println("Height based on femur: "+MaleFemR);
System.out.println("Height based on humerus: "+MaleHumR);
break;
default:
System.out.println("Gender not sprecified.");
break;
}
}
}
The expected output should be the height for the specific gender based on femur and humerus
I agree; change your in.nextInt(); to scan.nextInt() unless your in is declared and you aren't showing what's its purpose.
you'll get your switch case then.
I think due to in.nextInt();, that cause in cannot be resolved compile error,
change all in.nextInt(); to scan.nextInt(); then should be ok.
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 have this program set up and i need help with 2 errors that i am getting
import java.util.Scanner;
public class EvenOdd {
public static void main(String[]args) {
Scanner keyboard = new Scanner (System.in);
System.out.print("---EvenOdd--- /n");
System.out.printf("Enter a whole number: ");
c = in.nextInt();
}
public static EvenOdd (int num) {
int c = num;
if (int.class)
(c/2)*2 = c;
System.out.println("is even.");
else (c)
System.out.println("is odd");
return EvenOdd;
}
}
C:\Users\Desktop\EvenOdd.java:28: error: not a statement
else (c)
C:\Users\Desktop\EvenOdd.java:28: error: 'else' without 'if'
else (c)
2 errors
Your else doesn't make sense. First of all, you are not using braces, but also your boolean logic does not make sense. Just adding braces will not make your code compile.
I think this rewrite is the closest to what you have
public static boolean EvenOdd (int num) {
// Here your calculation is done
boolean isEven = (c/2)*2 == c;
if (isEven) {
System.out.println("is even.");
} else if (!isEven) {
// Using 'else if' for a boolean parameter does not make much sense
// but i'll leave it here to explain the syntax
System.out.println("is odd");
}
return isEven;
}
However, the most common way to check for odd or even is using the modulus operator. And if i make the entire code a bit more java-ish, you'd end up with (for example method-naming)
/**
* Check if the given number is even.
* #param num the number to check
* #return whether num is an even number
*/
public static boolean isEven (int num) {
if ((x % 2) == 0) {
System.out.println("is even.");
return true;
} else {
System.out.println("is odd");
return false;
}
}
Here is a solution
public static void main(String[]args)
{
Scanner keyboard = new Scanner(System.in);
try{
System.out.println("---EvenOdd---");
System.out.print("Enter a whole number: ");
int c = keyboard.nextInt();
evenOdd(c);
}finally{
keyboard.close();
}
}
public static void evenOdd(int num)
{
int c = num;
if ((c/2)*2 == c){
System.out.println("is even.");
}else {
System.out.println("is odd");
}
}
Output:
for the input value 5
---EvenOdd---
Enter a whole number: 5
is odd
for the input value 4
---EvenOdd---
Enter a whole number: 4
is even.
Continued Reading
There are several problems with the original code and I will attempt to explain them in line order.
Original code for reference:
public class EvenOdd {
public static void main(String[]args) {
Scanner keyboard = new Scanner (System.in);
System.out.print("---EvenOdd--- /n");
System.out.printf("Enter a whole number: ");
c = in.nextInt();
}
public static EvenOdd (int num) {
int c = num;
if (int.class)
(c/2)*2 = c;
System.out.println("is even.");
else (c)
System.out.println("is odd");
return EvenOdd;
}
}
First we have this line
System.out.print("---EvenOdd--- /n");
use of the .print() method here, while not illegal is unnecessary because java provides us with .println() which automatically creates a new line so we don't have to. (i.e. with "/n")
System.out.printf("Enter a whole number: ");
Next you use the .printf() method, this prints a formatted output and accepts arguments as a parameter. You aren't using any of the exclusive features of this method so we can achieve the same functionality with .print().
c = in.nextInt();
the variable in is not defined in this scope, I presume that you meant to use keyboard.nextInt().
public static EvenOdd (int num) {
when a method has no return type and the same name as the class it resides in (case sensitive) it is a constructor. Constructors do not require a return statement and are invoked with the syntax new ObjectConstructor() usually to assign a value to a variable of the same type as the constructor.
if (int.class)
(c/2)*2 = c;
System.out.println("is even.");
else (c)
System.out.println("is odd");
This if-else block is clearly not even java syntax.
first there is no need to cast your result to an int and the semicolon at the end of your conditional doesn't belong.
removing these errors brings us to:
if (c/2)*2 = c
System.out.println("is even.");
else (c)
System.out.println("is odd");
now we need to wrap our conditional in parentheses '( and )' and rather than use the assignment operator '=' we should use the comparison operator '==' which returns a boolean. Also, the else clause does not require a condition, if you would like to use a condition look into elseif.
these changes get us to this step.
if ((c/2)*2 == c)
System.out.println("is even.");
else
System.out.println("is odd");
Now we add proper brackets and we are good to go.
if ((c/2)*2 == c){
System.out.println("is even.");
}else{
System.out.println("is odd");
}
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.");
}