+ 1

How can I achieve this system?

So I recetly wanted to create a simple system with a simple purpose: When the person read the code, they would be able to run the code and give an input, which ranges from 0-5, as review on the code. On each rating, it outputs a different message. I'll put the the code in the comments. My problem is that I don't fully know how to use the Scanner option to get userinput or use it with integers. Please give some suggestion.I'm very new to coding

2nd Feb 2017, 10:03 AM
CarnoFred
CarnoFred - avatar
3 Antworten
+ 2
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner rating = new Scanner(System.in); String answer = rating.next(); switch(answer) { case "1": System.out.println("Kek you."); break; case "2": System.out.println("Oh no"); break; case "3": System.out.println("Heh heh."); break; case "4": System.out.println("Thanks"); break; case "5": System.out.println("Thank you so much."); break; } } } That is, the scanner is assigned to the variable 'rating', which you must use to call the method next(), in order to get the user's input. This input is a string, so you should also change the values that you check in the switch statement: instead of 1, write "1", and so on.
2nd Feb 2017, 11:28 AM
Álvaro
+ 1
Thank you so much. I understand it now. :D
2nd Feb 2017, 1:19 PM
CarnoFred
CarnoFred - avatar
0
I also realised I can't use the switch command. It doesn't add up. This is a very incorrect code, 'tis why I ask help. import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner rating = new Scanner(System.in); System.out.println(sc.next()); switch(Rating) { case 1: System.out.println("Kek you."); break; case 2: System.out.println("Oh no"); break; case 3: System.out.println("Heh heh."); break; case 4: System.out.println("Thanks"); break; case 5: System.out.println("Thank you so much."); break; } } }
2nd Feb 2017, 10:06 AM
CarnoFred
CarnoFred - avatar