Sup guys, I'm learning how to code in java with uni but I have no prior experience of coding. I'm struggling with finding certain information to use because I just don't know what to search. Basically I'm trying to make a simple game where the user inputs a 1, they win, put in any other number they lose. (Might try and randomise the winning number out of 10 but I don't even know where to start with that one.) Here's what I have so far, the game works but it's just poorly executed, I don't know how to make the if and else statements work properly. import java.util.Scanner; import java.util.*; import java.io.*; public class Oi { public static void main(String[] args) { int winningNumber; System.out.println("Please Enter 1 or 2"); Scanner scanner = new Scanner( System.in ); winningNumber = scanner.nextInt(); if (winningNumber <=1) { System.out.println("You've won mate gratz"); } else if (winningNumber >=2) { System.out.println("fuck you suck dick"); } } } The <=1 and >=2 were just cop outs because = wouldn't work
I have no idea how to solve your problem, but check this website out: https://www.codecademy.com/ I used it a few years ago to try coding, and its actually pretty effective, and easy to use. Also, try pasting the code onto this site: http://jshint.com/ It should show you all the errors etc. Good luck with your coding adventures!
Follow up question though. I've tried doing to error handling, how would I make it return something if the user put in something that wasn't 1 or 2 but wasn't a number I've tried else if (winningNumber <2) else if (winningNumber ==0) for the numbers and it works. How about characters?
Can you do something like != 1 gives a loss instead of specifying it as a number? Just anything that isn't the winning number is a loss?
as in; else if (winningNumber != 1) { System.out.println("dude what the fuck did I say"); || (or* hee hee) if (winningNumber !=1) { System.out.println("you suck dick bro"); Putting in a string still fucks it up
Hello world! Code: import java.util.*; import java.lang.*; import java.io.*; class Oi { public static void main (String[] args) throws java.lang.Exception { System.out.println("Please Enter 1 or 2"); Scanner scanner = new Scanner(System.in); int number; try { number = Integer.parseInt(scanner.nextLine()); } catch (NumberFormatException e) { System.out.println("Are you a retard? That's not a number"); return; } if (number == 1) { System.out.println("You've won mate gratz"); } else if (number == 2) { System.out.println("fuck you suck dick"); } else { System.out.println("loser"); } } }
That still gives me Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at Oi.main(Oi.java:13) When typing in a string
You can catch multiple types of exceptions in one try/catch block: Code: try { number = Integer.parseInt(scanner.nextLine()); } catch (InputMismatchException | NumberFormatException e) { System.out.println("Are you a retard? That's not a number"); return; }
import java.util.Scanner; import java.util.*; import java.io.*; public class Oi { public static void main(String[] args) { int winningNumber; System.out.println("Please Enter 1 or 2"); Scanner scanner = new Scanner( System.in ); winningNumber = scanner.nextInt(); if (winningNumber ==1) { System.out.println("You've won mate gratz"); } else if (winningNumber ==2) { System.out.println("fuck you suck dick"); } else if (winningNumber <2) { System.out.println("dude what the fuck did I say"); } else if (winningNumber ==0) { System.out.println("dude what the fuck did I say"); } } }
Methods in Java can throw exceptions. If you don't handle them in your code, the application crashes. Code: import java.util.Scanner; import java.util.*; import java.io.*; public class Oi { public static void main(String[] args) { int winningNumber; System.out.println("Please Enter 1 or 2"); Scanner scanner = new Scanner(System.in); try { winningNumber = scanner.nextInt(); } catch (InputMismatchException e) { System.out.println("Error! Let's bail!"); return; } if (winningNumber == 1) { System.out.println("You've won mate gratz"); } else if (winningNumber == 2) { System.out.println("fuck you suck dick"); } else if (winningNumber < 2) { System.out.println("dude what the fuck did I say"); } else if (winningNumber == 0) { System.out.println("dude what the fuck did I say"); } } } Why the hell are they using Java to tech people how to code?
When you call scanner.nextInt(), scanner throws an "exception" if what it reads isn't an int - you've told it you want to read one, but there isn't one there. When you're using something that might cause exceptions, you can surround it in a try-catch block like this: Code: try { winningNumber = scanner.nextInt(); } catch (InputMismatchException e) { System.out.println("Are you a retard? That's not a number"); return; } finally { scanner.close() } The finally block just ensures that you stop reading from the source - which frees it up for other programs to use.
By the way, how do you know when to expect an exception? Answer: the documentation should have it. For example, the documentation for Scanner.nextInt().
I would go through Oracle's tutorials https://docs.oracle.com/javase/tutorial/ . I would suggest going through things in order, though there is a tutorial on using Scanner at https://docs.oracle.com/javase/tutorial/essential/io/scanning.html If you're someone that learns better from watching videos rather than reading then I can't help you. Edit: If you can, stick to a text editor (something like notepad++ is fine) rather than using an IDE to start with. It's much better to get the fundamentals down properly without relying on an IDE to do lots of things automatically without your understanding early on.