From ggc
|
01 packagepackage is used to name the directory or folder a class is in intermediate;
02 //start auto-imports
03 importimport means to make the classes and/or packages available in this program sun.security.krb5.internal.ccache.*;
04 //end auto-imports
05 importimport means to make the classes and/or packages available in this program java.util.*;
06
07 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects HappyFun
08 {open braces start code blocks and must be matched with a close brace
09 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value mainThe main method is the place where applications begin executing.(String[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays args)
10 {open braces start code blocks and must be matched with a close brace
11 Scanner scanner=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Scanner(System.in);
12 intint is the type for whole numbers and it is short for integer number=this assignment operator makes the left side equal to the right side(intint is the type for whole numbers and it is short for integer)(Math.random()*100)+adds two numbers together or concatenates Strings together1;
13 System.out.println("Guess a number 1-100.");
14 intint is the type for whole numbers and it is short for integer guess=this assignment operator makes the left side equal to the right side0;
15 whilewhile is a looping structure for executing code repeatedly(guess!=this is the not equals operator which evaluates to true if both sides are differentnumber &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) guess!=this is the not equals operator which evaluates to true if both sides are different-1)
16 {open braces start code blocks and must be matched with a close brace
17 String line=this assignment operator makes the left side equal to the right sidescanner.nextLine();
18 trytry is for executing a code block that may experience exceptions (errors)
19 {open braces start code blocks and must be matched with a close brace
20 guess=this assignment operator makes the left side equal to the right sideInteger.parseInt(line);
21 ifif executes the next statement only if the condition in parenthesis evaluates to true(guess>number)
22 System.out.println("Guess lower.");
23 elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true(guess<number)
24 System.out.println("Guess higher.");
25 elseelse is what happens when the if condition is false
26 System.out.println("You guessed it!");
27 }close braces end code blocks and must match an earlier open brace
28 catchcatch means to handle an exception that may occur(NumberFormatException nfe)
29 {open braces start code blocks and must be matched with a close brace
30 System.out.println("Sorry, "+adds two numbers together or concatenates Strings togetherline+adds two numbers together or concatenates Strings together" is invalid.");
31 System.out.println("Please enter a number such as 1, 6, 67");
32 System.out.println("Enter -1 to quit.");
33 }close braces end code blocks and must match an earlier open brace
34 }close braces end code blocks and must match an earlier open brace
35 }close braces end code blocks and must match an earlier open brace
36 }close braces end code blocks and must match an earlier open brace
|
Download/View intermediate/HappyFun.java