coded a Random number Guessing game
This commit is contained in:
parent
f19b97c13b
commit
0f1b64f5d4
3 changed files with 83 additions and 6 deletions
|
|
@ -4,10 +4,10 @@
|
|||
<option name="autoReloadType" value="SELECTIVE" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="8658cbac-7347-4046-83e8-57065c86d2b8" name="Changes" comment="Made a calculator using Switches">
|
||||
<change afterPath="$PROJECT_DIR$/practice_projects/src/WhileLoops.java" afterDir="false" />
|
||||
<list default="true" id="8658cbac-7347-4046-83e8-57065c86d2b8" name="Changes" comment="made a basic calculator">
|
||||
<change afterPath="$PROJECT_DIR$/practice_projects/src/GuessANumber.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/out/production/java_practice/Calculator.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/java_practice/Calculator.class" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/out/production/java_practice/WhileLoops.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/java_practice/WhileLoops.class" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
|
|
@ -62,6 +62,7 @@
|
|||
"keyToString": {
|
||||
"Application.Calculator.executor": "Run",
|
||||
"Application.EnhancedSwitches.executor": "Run",
|
||||
"Application.GuessANumber.executor": "Run",
|
||||
"Application.Main.executor": "Run",
|
||||
"Application.TempConverter.executor": "Run",
|
||||
"Application.WhileLoops.executor": "Run",
|
||||
|
|
@ -121,7 +122,7 @@
|
|||
<workItem from="1767557093354" duration="14440000" />
|
||||
<workItem from="1767678323979" duration="16130000" />
|
||||
<workItem from="1767837943314" duration="237000" />
|
||||
<workItem from="1767838192168" duration="5894000" />
|
||||
<workItem from="1767838192168" duration="7995000" />
|
||||
</task>
|
||||
<task id="LOCAL-00001" summary="updated module output folders">
|
||||
<option name="closed" value="true" />
|
||||
|
|
@ -283,7 +284,23 @@
|
|||
<option name="project" value="LOCAL" />
|
||||
<updated>1767840813715</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="21" />
|
||||
<task id="LOCAL-00021" summary="Explored while and do while loops">
|
||||
<option name="closed" value="true" />
|
||||
<created>1767853449206</created>
|
||||
<option name="number" value="00021" />
|
||||
<option name="presentableId" value="LOCAL-00021" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1767853449206</updated>
|
||||
</task>
|
||||
<task id="LOCAL-00022" summary="made a basic calculator">
|
||||
<option name="closed" value="true" />
|
||||
<created>1767853460560</created>
|
||||
<option name="number" value="00022" />
|
||||
<option name="presentableId" value="LOCAL-00022" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1767853460560</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="23" />
|
||||
<servers />
|
||||
</component>
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
|
|
@ -319,7 +336,9 @@
|
|||
<MESSAGE value="updated java version to 17 so I can use switched (Introduced in 14" />
|
||||
<MESSAGE value="Did a few base cases using enhanced switches" />
|
||||
<MESSAGE value="Made a calculator using Switches" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="Made a calculator using Switches" />
|
||||
<MESSAGE value="Explored while and do while loops" />
|
||||
<MESSAGE value="made a basic calculator" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="made a basic calculator" />
|
||||
</component>
|
||||
<component name="XSLT-Support.FileAssociations.UIState">
|
||||
<expand />
|
||||
|
|
|
|||
BIN
out/production/java_practice/GuessANumber.class
Normal file
BIN
out/production/java_practice/GuessANumber.class
Normal file
Binary file not shown.
58
practice_projects/src/GuessANumber.java
Normal file
58
practice_projects/src/GuessANumber.java
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import java.util.Scanner;
|
||||
import java.util.Random;
|
||||
|
||||
public class GuessANumber {
|
||||
|
||||
public static void main(String[] args){
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
// Intro at top of screen
|
||||
System.out.println("=== !! NUMBER GUESSING GAME !! ===");
|
||||
System.out.println("Guess a number between 1-100");
|
||||
System.out.println("--- You have 10 guesses --- ");
|
||||
|
||||
// have computer pick a random number 1-100
|
||||
Random random = new Random();
|
||||
int number = random.nextInt(1, 101); // end range is not included. so we use 101
|
||||
|
||||
// Declare guess so we can use it for our while loop
|
||||
int guess = 0;
|
||||
int chances = 10; // Only give them 10 chances to guess it
|
||||
|
||||
// While logic to process guesses
|
||||
while(guess != number){
|
||||
System.out.print("\nEnter a guess: ");
|
||||
guess = scanner.nextInt();
|
||||
|
||||
if(chances == 1){
|
||||
System.out.println("\n####### YOU LOSE!!! #######");
|
||||
System.out.println("--- The number was [ " + number + " ] ---");
|
||||
break;
|
||||
} else if(guess > number){
|
||||
System.out.println("\nTOO HIGH. Try again");
|
||||
chances--;
|
||||
System.out.println("-- " + chances + " guesses remaining. --");
|
||||
continue;
|
||||
} else if(guess < number){
|
||||
System.out.println("\nTOO LOW. Try again");
|
||||
chances--;
|
||||
System.out.println("-- " + chances + " guesses remaining. --");
|
||||
continue;
|
||||
} else {
|
||||
// Winner message with the correct number
|
||||
System.out.println("---------------------------------------");
|
||||
System.out.println("\n***** WINNER * WINNER * WINNER *****");
|
||||
System.out.println("********* YOU GUESSED IT! ********* \n");
|
||||
System.out.println("\n----- The number was [ " + number + " ] -----");
|
||||
System.out.println(" -- You had " + chances + " guesses remaining --");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
scanner.close();
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue