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.
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 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)
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 4 years ago.
Improve this question
I AM a beginner in java and we have learned that if two variables are not in the same block then they are independent of each other.
I don"t understand why the variable entier2 take false if entier1 is false
System.out.print("enter entier1 : ");
if (!entree.hasNextInt()) {
System.out.println("Enter a number1!");
} else {
entier1 = entree.nextInt();
System.out.println("Entier 1 = " + entier1);
}
System.out.print("Enter entier2 : ");
if (!entree.hasNextInt()) {
System.out.println("Enter a number2!");
} else {
entier2 = entree.nextInt();
System.out.println("Entier 2 = " + entier2);
}
In first it is the first bloc that is executed and after the second
And the second bloc take the 'value' of the first if the first is false
For a correct example we have :
Enter number1 : 45
Entier 1 = 45
Enter number2 : 15
Entier 2 = 15
So all is just is perfect beacause I have two number
But if entier1 = "txt" (so one string) then in result entier2 = Enter a number2!
For exemple the result is :
Enter number1 : txt
> Enter a number 1 !
Enter number2 : Enter a number 2 !
So, the program doesn't verif value of number2 because it take false, because number1 is false (which is incorrect)
So, my question is Why number2 take false if number1 is false?
Ps : if the question is unclear, please tell me what can clarify it
If I understand it correctly, You only check for integer input with hasNextInt() but If condition is not true, you didn't throw away incorrect input and so next condition will check again for that incorrect input for second time and return same result as before. in my code I use in.nextLine() so I will empty buffer whether input is correct or not. you can do same with this technique too.
if you want to get correct number and then get second number, you can use this:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num1 = getInt(in);
int num2 = getInt(in);
System.out.println(num1 + " " + num2);
}
public static int getInt(Scanner in) {
System.out.print("Enter number: ");
while (true) {
String line = in.nextLine();
try {
int num = Integer.parseInt(line);
return num;
} catch (NumberFormatException ex) {
System.out.print("Enter correct input!!\nEnter nubmer: ");
}
}
}
I hope this help you. We try to get input until only contains number (nothing more)
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 want to use scanner to make a user use a number in the switch code I wrote:
public class NewClass1 {
public static void main(String[] args) {
Scanner abd=new Scanner(System.in);
System.out.println("Enter a number");
int k = 5;
int k = i.nextInt();
switch (k) {
case 0:
System.out.println("You chosed 0");
break;
case 1:
System.out.println("You chosed 1");
break;
case 3:
System.out.println("You chosed 3");
break;
case 9:
System.out.println("You chosed 9");
break;
default:
System.out.println("Please enter something is in the list, Which is: 0,1,3,9.");
break;
}
}
I think there's something wrong or missing in my code, I will appreciate the help.
Your code will not even compile..
int k = 5;
int k=i.nextInt(); // same variable already define in the scope.
Next point is your referring i, But your Scanner is initialize as abd. Remove line int k = 5 and you should change your code to
int k = abd.nextInt();
Your scanner is named abd, but you call i.nextInt(). That is the problem.
EDIT
int k = 5;
k = abd.nextInt();