1
First Ever Java "Game"
So I decided to learn java, and this is my first project after a few tutorial videos.
Click to reveal
import java.util.Random;
import java.util.Scanner;
public class Main {
public static int number, guess, maxValue;
public static Scanner scan;
public static Random rand;
public static void main(String args[]) {
scan = new Scanner(System.in);
rand = new Random();
System.out.println("Please enter a maxiume value too guess from!: ");
maxValue = scan.nextInt();
number = rand.nextInt(maxValue);
System.out.print("Please Enter a guess: ");
while (guess != number) {
guess = scan.nextInt();
if (guess > number) {
System.out.println("Your guess is too high!");
}
if (guess < number) {
System.out.println("Your guess is too low!");
}
}
System.out.println("You Won!");
}
}
import java.util.Scanner;
public class Main {
public static int number, guess, maxValue;
public static Scanner scan;
public static Random rand;
public static void main(String args[]) {
scan = new Scanner(System.in);
rand = new Random();
System.out.println("Please enter a maxiume value too guess from!: ");
maxValue = scan.nextInt();
number = rand.nextInt(maxValue);
System.out.print("Please Enter a guess: ");
while (guess != number) {
guess = scan.nextInt();
if (guess > number) {
System.out.println("Your guess is too high!");
}
if (guess < number) {
System.out.println("Your guess is too low!");
}
}
System.out.println("You Won!");
}
}
Create an account or sign in to comment.
6
1
thenubslayer2000ThePixelPony
StackOverflow is a great place to learn.
You mean the first result that comes up when you google 'java how to x'
Yes, although the "How to" is unnecessary. You should be searching using this format: "<subject and sub-subjects> <problem in as few words as possible>". E.g. "minecraft java modding tutorial".
1
This makes me want to find out how to program on my calculator. I could make some neat programs on my Ti-83 Plus but this new Nspire CX is a bit more involved, it seems.
1
ThePixelPony
StackOverflow is a great place to learn.
You mean the first result that comes up when you google 'java how to x'
1
You've started learning Java; great!
I'll teach you some things.
"Access" modifiers:
public - Can be accessed from any class
private - Can only be accessed from inside the current class
protected - Can be accessed inside this class and any of its children (classes that extend it) and from inside the same package
nothing - Can be accessed from the same package (folder)
Other modifiers:
final - Once defined, it cannot be changed, use it when possible; it improves performance
static - There is only one of these in the class, try to avoid using this when you can
abstract - It exists but has to be defined by child classes, you won't need this for a while yet
transient - Uhh, you won't need this for ages so forget about it
When you have fields in a class (e.g. "Scanner scanner;") you usually want to make them private. It's good practise. When another class needs them, you create a method called getScanner() that returns the private Scanner object.
If you have any questions, feel free to PM!
http://stackoverflow.com/questions/2154 ... nd-private
StackOverflow is a great place to learn.
I'll teach you some things.
"Access" modifiers:
public - Can be accessed from any class
private - Can only be accessed from inside the current class
protected - Can be accessed inside this class and any of its children (classes that extend it) and from inside the same package
nothing - Can be accessed from the same package (folder)
Other modifiers:
final - Once defined, it cannot be changed, use it when possible; it improves performance
static - There is only one of these in the class, try to avoid using this when you can
abstract - It exists but has to be defined by child classes, you won't need this for a while yet
transient - Uhh, you won't need this for ages so forget about it
When you have fields in a class (e.g. "Scanner scanner;") you usually want to make them private. It's good practise. When another class needs them, you create a method called getScanner() that returns the private Scanner object.
If you have any questions, feel free to PM!
http://stackoverflow.com/questions/2154 ... nd-private
StackOverflow is a great place to learn.
1
RoboShadowHey! I've made that game on my calculator!
Me too!
1
Hey! I've made that game on my calculator!
But GJ OP, you're going places.
But GJ OP, you're going places.
:C->0
:Disp "Minimum:"
:Prompt X
:Disp "Maximum:"
:Prompt Y
:randInt(X,Y)->A
:Lbl 1
:Disp "Guess"
:Prompt B
:If B>A:Disp "Guess too high":Pause:C+1->C:Goto 1
:If B<A:Disp "Guess too low":Pause:C+1->C:Goto 1
:If B=A:Disp "Correct!":C+1->C:Goto 2
:Lbl 2
:Disp "Number of guesses":Disp {C}