+ 1
how to fix it?
import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) throws NumberFormatException{ int points = 4; Scanner s = new Scanner (System.in); int myAnswer0 = s.nextInt(); if (myAnswer0 != "points"){ myAnswer0 = s.nextInt(); }else{ String myString0 = String.valueOf(myAnswer0); System.out.println(points); } } }
7 ответов
+ 3
"points" is a string. You can't compare an integer with a string.
But you want to compare the user input with the value of your variable points:
if(myAnswer0 != points){
...
}
+ 1
nextInt() is only for integer input.
If the user can enter both (an integer or a string) you should ask for a string.
+ 1
ok I see your full code now
https://code.sololearn.com/crJoNyj46O2T
so I can modify my last answer to
String myAnswer0 = s.next();
int myAnswer0Int;
if (! myAnswer0.equals("points") ) {
myAnswer0Int = Integer.valueOf(myAnswer0);
}else{
//String myString0 = myAnswer0;
System.out.println(points);
}
0
no... i want to do that when i write the word 'points' it will show me the amount of points. and when i write an integer it will accept the input as well...
0
String myAnswer0 = s.next();
if (! myAnswer0.equals("points") )
points = Integer.valueOf(myAnswer0);
System.out.println(points);
0
no... you dont get it. i need that when i write an integer the System will work just fine and print the number, but when i write the String 'points' it will tell me the amount of points.
0
You don't need multiple Scanners;
But I have 5 tips for you.
1. Use a String to accept input from Scanner.
2. Your current code only asks one time, after that it ends at once. To make it continuous, use a do-while loop and exit if user enters something like "exit". Another else if for "exit" where it breaks the loop.
3. To store points, use an integer variable to increment whenever got correct answer.
4. Add the "points" as another if-else to show the current point or score.
5. Simplify your switch statement, put the scanner and checking of answer outside the switch. Inside the switch should only be the computation.
Here is my sample code, https://code.sololearn.com/ctL6lP807s9X/#java
Hope you get my idea. You can try to execute it here,
https://onlinegdb.com/rkkKqo80I