-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 798d866
Showing
7 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Static functions used to check console input for validity. | ||
* | ||
* Use: Place CheckInput class in the same project folder as your code. | ||
* Call CheckInput functions from your code using "CheckInput." | ||
* | ||
* Example: int num = CheckInput.getInt(); | ||
* | ||
* @author Shannon Cleary 2020 | ||
*/ | ||
public class CheckInput { | ||
|
||
/** | ||
* Checks if the inputted value is an integer. | ||
* @return the valid input. | ||
*/ | ||
public static int getInt() { | ||
Scanner in = new Scanner( System.in ); | ||
int input = 0; | ||
boolean valid = false; | ||
while( !valid ) { | ||
if( in.hasNextInt() ) { | ||
input = in.nextInt(); | ||
valid = true; | ||
} else { | ||
in.next(); //clear invalid string | ||
System.out.println( "Invalid Input." ); | ||
} | ||
} | ||
return input; | ||
} | ||
|
||
/** | ||
* Checks if the inputted value is an integer and | ||
* within the specified range (ex: 1-10) | ||
* @param low lower bound of the range. | ||
* @param high upper bound of the range. | ||
* @return the valid input. | ||
*/ | ||
public static int getIntRange( int low, int high ) { | ||
Scanner in = new Scanner( System.in ); | ||
int input = 0; | ||
boolean valid = false; | ||
while( !valid ) { | ||
if( in.hasNextInt() ) { | ||
input = in.nextInt(); | ||
if( input <= high && input >= low ) { | ||
valid = true; | ||
} else { | ||
System.out.println( "Invalid Range." ); | ||
} | ||
} else { | ||
in.next(); //clear invalid string | ||
System.out.println( "Invalid Input." ); | ||
} | ||
} | ||
return input; | ||
} | ||
/** | ||
* Checks if the inputted value is a non-negative integer. | ||
* @return the valid input. | ||
*/ | ||
public static int getPositiveInt( ) { | ||
Scanner in = new Scanner( System.in ); | ||
int input = 0; | ||
boolean valid = false; | ||
while( !valid ) { | ||
if( in.hasNextInt() ) { | ||
input = in.nextInt(); | ||
if( input >= 0 ) { | ||
valid = true; | ||
} else { | ||
System.out.println( "Invalid Range." ); | ||
} | ||
} else { | ||
in.next(); //clear invalid string | ||
System.out.println( "Invalid Input." ); | ||
} | ||
} | ||
return input; | ||
} | ||
|
||
/** | ||
* Checks if the inputted value is a double. | ||
* @return the valid input. | ||
*/ | ||
public static double getDouble() { | ||
Scanner in = new Scanner( System.in ); | ||
double input = 0; | ||
boolean valid = false; | ||
while( !valid ) { | ||
if( in.hasNextDouble() ) { | ||
input = in.nextDouble(); | ||
valid = true; | ||
} else { | ||
in.next(); //clear invalid string | ||
System.out.println( "Invalid Input." ); | ||
} | ||
} | ||
return input; | ||
} | ||
|
||
/** | ||
* Takes in a string from the user. | ||
* @return the inputted String. | ||
*/ | ||
public static String getString() { | ||
Scanner in = new Scanner( System.in ); | ||
String input = in.nextLine(); | ||
return input; | ||
} | ||
|
||
/** | ||
* Takes in a yes/no from the user. | ||
* @return true if yes, false if no. | ||
*/ | ||
public static boolean getYesNo(){ | ||
boolean valid = false; | ||
while( !valid ) { | ||
String s = getString(); | ||
if( s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y") ) { | ||
return true; | ||
} else if( s.equalsIgnoreCase("no") || s.equalsIgnoreCase("n") ) { | ||
return false; | ||
} else { | ||
System.out.println( "Invalid Input." ); | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import java.util.Random; | ||
|
||
class Main { | ||
public static void main(String[] args) { | ||
|
||
// initializing variables | ||
Random random = new Random(); | ||
|
||
// random number between 0 - 100 | ||
int randomNumber = random.nextInt(101); | ||
|
||
int maxRange = 100; | ||
int guess = 0; | ||
int minRange = 0; | ||
int count = 0; | ||
|
||
// initialize boolean guess | ||
Boolean notGuessed = true; | ||
|
||
// prompt the user to enter a number | ||
System.out.print("I’m thinking of a number. Guess a value (1-100): "); | ||
|
||
while (notGuessed){ | ||
// check if input is valid | ||
guess = CheckInput.getIntRange(minRange, maxRange); | ||
|
||
// increment counter | ||
count ++; | ||
|
||
// check if guess is correct | ||
if (guess == randomNumber){ | ||
notGuessed = false; | ||
} | ||
|
||
// if the guess is not correct, tell the user | ||
else{ | ||
String message = ""; | ||
// if guess is too high, store message | ||
if (guess > randomNumber){ | ||
message = "Too High."; | ||
//else if guess is too low, store message | ||
} else{ | ||
message = "Too Low. "; | ||
} | ||
// prompt the user to ask again | ||
System.out.print(message + " Guess again: "); | ||
} | ||
} | ||
// tell the user they guessed the number correct | ||
System.out.println("Correct! You got it in " + count + " tries."); | ||
} | ||
} |