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();
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 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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I am trying to make this code work and it keep sending the error to the scaner.
(Test.java:7: error: cannot find symbol)
class Test{
public static void main(String[] args) {
int x;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number");
x = in.nextInt();
if (x<100)
x=x +5;
if (x<500)
x=x-2;
if (x>10)
x++;
else
x--;
System.out.println(x);
}
}
Correct your code to import java.util.Scanner; class like below and also change in.nextInt() to scanner.nextInt().
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int x;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number");
x = scanner.nextInt();
if (x < 100)
{
x = x + 5;
}
if (x < 500)
{
x = x - 2;
}
if (x > 10)
{
x++;
}
else
{
x--;
}
System.out.println(x);
}
}
1. You have used Scannerclass to take input while, you haven't told java that you are using it. For This you should import this import java.util.Scanner.
2. The class which has public static void main(String[] args) should be public.why
3. I think you should use if-else-if construct instead of many if because in your case if x = 20 it will be modified in all the three cases.
If you look at the Java doc here: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
You will see that Scanner needs:
import java.util.Scanner
and not
import java.io.*