1

First Ever Java "Game"

TheGrimGamer14's Avatar TheGrimGamer145/28/15 7:55 am
1 emeralds 744 6
5/29/2015 12:53 pm
Pixel's Avatar Pixel
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!");
}

}
Posted by TheGrimGamer14's Avatar
TheGrimGamer14
Level 1 : New Network
1

Create an account or sign in to comment.

6

1
05/29/2015 12:53 pm
Level 42 : Master Mage
Pixel
Pixel's Avatar
thenubslayer2000
ThePixelPony
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
05/29/2015 12:34 pm
Level 46 : Master Button Pusher
Leeberator
Leeberator's Avatar
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
05/29/2015 12:26 pm
Level 21 : Expert Ninja
zoecGxhbmV0bWluZWNyYWZ0
zoecGxhbmV0bWluZWNyYWZ0's Avatar
ThePixelPony
StackOverflow is a great place to learn.

You mean the first result that comes up when you google 'java how to x'
1
05/29/2015 11:17 am
Level 42 : Master Mage
Pixel
Pixel's Avatar
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.
1
05/29/2015 11:10 am
Level 18 : Journeyman Warrior
serdarminecraft
serdarminecraft's Avatar
RoboShadowHey! I've made that game on my calculator!



Me too!
1
05/28/2015 10:29 am
Level 33 : Artisan Engineer
RoboShadow
RoboShadow's Avatar
Hey! I've made that game on my calculator!
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}
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome