0 Members and 2 Guests are viewing this topic.
package randomnumbergame;import java.util.Random;import java.util.Scanner;public class Main { public static void main(String[] args) { System.out.println("Enter difficulty (from 0 to 1000): "); Scanner difficulty = new Scanner(System.in); int difficultyLevel = difficulty.nextInt(); if (difficultyLevel<0 || difficultyLevel>1000) { System.out.println("Invalid Difficulty"); } System.out.println(difficulty.nextLine()); Random generator = new Random(); int randomNumber = generator.nextInt(difficultyLevel); boolean gotIt = false; while (gotIt == false) { Scanner attempt = new Scanner(System.in); int attemptNumber = attempt.nextInt(); if (attemptNumber == randomNumber) { gotIt = true; } else { if (attemptNumber > randomNumber) { System.out.println("The secret number is smaller... "); } else { System.out.println("The secret number is highter... "); } } } System.out.println("You won!"); }}
I'd avoid creating a scanner each and every time you need to read something - once is enough.
Hello, I want to use java at home, and I'm trying to download eclipse right now, unless if you guys think netbeans is better anyways, I suck at knowing all of these things how to install libraries and such, anyone know of a simple graphics library that can do 3D, and is easy to install?
package randomnumbergame;import java.util.Random;import java.util.Scanner;public class Main { public static void main(String[] args) { System.out.println("Enter difficulty (from 0 to 1000): "); Scanner inputScanner = new Scanner(System.in); int difficultyLevel = inputScanner.nextInt(); if (difficultyLevel<0 || difficultyLevel>1000) { System.out.println("Invalid Difficulty"); } System.out.println(inputScanner.nextLine()); Random generator = new Random(); int randomNumber = generator.nextInt(difficultyLevel); boolean gotIt = false; while (gotIt == false) { int attemptNumber = inputScanner.nextInt(); if (attemptNumber == randomNumber) { gotIt = true; } else { if (attemptNumber > randomNumber) { System.out.println("The secret number is smaller... "); } else { System.out.println("The secret number is highter... "); } } } System.out.println("You won!"); }}
package randomnumbergame;import java.util.Random;import java.util.Scanner;public class Main { public static void main(String[] args) { System.out.println("Enter difficulty (from 0 to 1000): "); Scanner inputScanner = new Scanner(System.in); int difficultyLevel = inputScanner.nextInt(); if (difficultyLevel<0 || difficultyLevel>1000) { System.out.println("Invalid Difficulty"); sys.exit(0); } System.out.println(inputScanner.nextLine()); Random generator = new Random(); int randomNumber = generator.nextInt(difficultyLevel); boolean gotIt = false; while (gotIt == false) { int attemptNumber = inputScanner.nextInt(); if (attemptNumber == randomNumber) { gotIt = true; } else { if (attemptNumber > randomNumber) { System.out.println("The secret number is smaller... "); } else { System.out.println("The secret number is highter... "); } } } System.out.println("You won!"); }}
Quote from: Ashbad on February 20, 2011, 02:50:47 pmHello, I want to use java at home, and I'm trying to download eclipse right now, unless if you guys think netbeans is better anyways, I suck at knowing all of these things how to install libraries and such, anyone know of a simple graphics library that can do 3D, and is easy to install?You shouldn't be using Java for anything 3D - it's just too slow/memory-intensive for that. That's my opinion though.
int difficultyLevel = inputScanner.nextInt(); while (difficultyLevel<0 || difficultyLevel>1000) { System.out.println("Invalid Difficulty"); difficultyLevel = inputScanner.nextInt(); }
Quote from: broooom on February 20, 2011, 02:51:37 pmQuote from: Ashbad on February 20, 2011, 02:50:47 pmHello, I want to use java at home, and I'm trying to download eclipse right now, unless if you guys think netbeans is better anyways, I suck at knowing all of these things how to install libraries and such, anyone know of a simple graphics library that can do 3D, and is easy to install?You shouldn't be using Java for anything 3D - it's just too slow/memory-intensive for that. That's my opinion though.well, I'm talking simple 3D, like minecraft 3D.
I'd do:Code: [Select] int difficultyLevel = inputScanner.nextInt(); while (difficultyLevel<0 || difficultyLevel>1000) { System.out.println("Invalid Difficulty"); difficultyLevel = inputScanner.nextInt(); }
Quote from: Ashbad on February 20, 2011, 02:54:14 pmQuote from: broooom on February 20, 2011, 02:51:37 pmQuote from: Ashbad on February 20, 2011, 02:50:47 pmHello, I want to use java at home, and I'm trying to download eclipse right now, unless if you guys think netbeans is better anyways, I suck at knowing all of these things how to install libraries and such, anyone know of a simple graphics library that can do 3D, and is easy to install?You shouldn't be using Java for anything 3D - it's just too slow/memory-intensive for that. That's my opinion though.well, I'm talking simple 3D, like minecraft 3D.Even though... It's just so complicated, you shouldn't start with it, never.