Você está na página 1de 2

Java notes:

public class Trap { (start every program with this)

public static void main (String []args){ (start main method with this)

Scanner object;

import java.util.Scanner; \\ tells java to scan, put before class declaration statement

Scanner scan = new Scanner(System.in) \\ standard method for creating a new scanner method. You can replace 'scan'
with whatever you want though

Scanner scan = new Scanner(new File("myNumbers"));

while (scan.hasNextInt()){

int aNumber = scan.nextInt(); \\ puts the int value scanned into the variable 'aNumber'

} \\ this creates a while loop to scan for ints in a file called 'myNumbers'

String input = "what the hell 6 am I reading 9";

Scanner s = new Scanner(input); \\ this scans a string, then letting you specify what to look for

nextInt() \\ will return the next int you scan

hasNextInt() \\ will return the next int you scan, and is a boolean (thus if the next int you scanned is the one you're
looking for, as specificed by your other parameters, then it returns true, otherwise it returns false. This is used for
setting up loops, as per above. Note that both methods can be used for things other than Int (hasNext and next by
themselves return strings, you can then specify what to look for by adding String pattern, pattern being what you're
looking for), for example nextDouble or nextFloat

while, if, else loops, and incrementing;

int numbers; \\ setup and other stuff occurs here, see above for details

while (Numbers <= 10000){ \\ this is your while loop, where you state the expression to be evaluated

Numbers = Numbers + 100; \\ executes ( and loops) this statement while the expression is true

if (Numbers < 9001) { \\ sets up a conditional if statement that loops while true

Numbers = Numbers + 100; \\ does this while the if statement is true

count++; \\ increments count (you can also write it as "count = count + 1; ''

System.out.println ("You lost the game" + count + " times"); \\ also does this

} else if (int >= 9000) { \\ another if statement, but executes preceding statement is false

System.out.println ("You won the game forever" + count + " times"); \\ does this

} else if (int <= -1) { \\ another if else statement

System.out.println ("Magnetics, they are equal to: " + count + " times"); \\ does this

}
}

Strings and characters;

char ch; \\ declare a character variable

String input; \\ declare a string input to scan

Scanner keyboard = new Scanner (System.in); \\ create scanner called 'keyboard' to register user input

System.out.println ("Please input a character:"); \\ prompt user to enter a character

input = keyboard.nextLine(); \\ read next line typed

ch = input.charAt(0); \\ place the scanned character into variable 'ch'

Random object;

import java.util.Random; \\ importing the Random generator

Random whatever = new Random(); \\ declaring a new random generator called 'whatever'

int/double nubsauce = whatever.nextInt/Double(); \\ the number inside the brackets dictates how many numbers are
generated, zero (0) inclusive

Você também pode gostar