Goal: Give unlimited chances for someone to enter their PIN.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Enter Your PIN
/// File Name: EnterPin.java
/// Date Finished: Nov 09, 2015
import java.util.Scanner;
public class EnterPin {
public static void main( String[] args )
{
Scanner input = new Scanner(System.in);
int pin = 12345;
System.out.println("WELCOME TO THE BANK OF JOSHUA.");
System.out.print("ENTER YOUR PIN: ");
int entry = input.nextInt();
while (entry != pin)
{
System.out.println("\nINCORRECT PIN. TRY AGAIN.");
System.out.print("ENTER YOUR PIN: ");
entry = input.nextInt();
}
System.out.println("\nPIN ACCEPTED. YOU NOW HAVE ACCESS TO YOUR ACCOUNT.");
}
}
Goal: Give unlimited chances for someone to guess a number.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Keep Guessing
/// File Name: KeepGuessing.java
/// Date Finished: Nov 09, 2015
import java.util.Random;
import java.util.Scanner;
public class KeepGuessing {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
int correct = 1 + rand.nextInt(10), guess;
System.out.print("I have chosen a random number between 1-10. Guess the number.\n> ");
guess = input.nextInt();
while (guess != correct) {
System.out.print("Incorrect try again.\n> ");
guess = input.nextInt();
}
System.out.println("Correct!");
}
}
Goal: Keep rolling virtual dice until doubles are rolled.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Dice Doubles
/// File Name: RollDoubles.java
/// Date Finished: Nov 09, 2015
import java.util.Random;
public class RollDoubles {
public static void main(String[] args) {
Random rand = new Random();
int r1 = 1 + rand.nextInt(6), r2 = 1 + rand.nextInt(6);
System.out.println("HERE COMES THE DICE!");
System.out.println();
System.out.println("Roll #1: " + r1);
System.out.println("Roll #2: " + r2);
System.out.println("The total is " + (r1 + r2) + "!");
while (r1 != r2) {
r1 = 1 + rand.nextInt(6);
r2 = 1 + rand.nextInt(6);
System.out.println();
System.out.println("Roll #1: " + r1);
System.out.println("Roll #2: " + r2);
System.out.println("The total is " + (r1 + r2) + "!");
}
}
}
Goal: Allow the user to configure how a loop functions.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Counting in a While Loop
/// File Name: WhileLoopCounting.java
/// Date Finished: Nov 09, 2015
import java.util.Scanner;
public class WhileLoopCounting {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String msg;
int iterations;
System.out.println("Type in a message.");
System.out.print("> ");
msg = input.nextLine();
System.out.println("How many times do you want it to be printed?");
System.out.print("> ");
iterations = input.nextInt();
int n = 1;
while (n <= iterations) {
System.out.println((n * 10) + ". " + msg);
// n++ increases the value of n by one. Without this, the conditional
// in the while loop will never evaluate to true, since n never can
// be less than 10.
n++;
}
}
}
Goal: Give limited tries for somone to input their PIN.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: PIN Lockout
/// File Name: PinLockout.java
/// Date Finished: Nov 09, 2015
import java.util.Scanner;
public class PinLockout {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int pin = 12345, tries = 0, maxTries = 4;
System.out.println("WELCOME TO THE BANK OF JOSHUA.");
System.out.print("ENTER YOUR PIN: ");
int entry = keyboard.nextInt();
tries++;
while (entry != pin && tries < maxTries) {
System.out.println("\nINCORRECT PIN. TRY AGAIN.");
System.out.print("ENTER YOUR PIN: ");
entry = keyboard.nextInt();
tries++;
}
if (entry == pin) {
System.out.println("\nPIN ACCEPTED. YOU NOW HAVE ACCESS TO YOUR ACCOUNT.");
} else if (tries >= maxTries) {
System.out.println("\nYOU HAVE RUN OUT OF TRIES. ACCOUNT LOCKED.");
}
}
}
Goal: Number-guessing now tracking how many times you fail.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Number Guessing with a Counter
/// File Name: EvenMoreNumberGuessing.java
/// Date Finished: Nov 09, 2015
import java.util.Random;
import java.util.Scanner;
public class EvenMoreNumberGuessing {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
int correct = 1 + rand.nextInt(10), count = 1, guess;
System.out.print("I have chosen a random number between 1-10. Guess the number.\n> ");
guess = input.nextInt();
while (guess != correct) {
System.out.print("Incorrect try again.\n> ");
guess = input.nextInt();
count++;
}
System.out.println("Correct, and it took you " + count + " tries!");
}
}
Goal: Guessing a number with hints and limited tries.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Hi-Lo with Limited Tries
/// File Name: HiLoLimited.java
/// Date Finished: Dec 04, 2015
import java.util.Random;
import java.util.Scanner;
public class HiLoLimited {
public static void main(String[] args) {
Random rand = new Random();
Scanner input = new Scanner(System.in);
int correct = rand.nextInt(100) + 1, attempts = 0, guess;
System.out.println("I'm thinking of a number between 1-100.");
while (attempts < 7) {
guess = input.nextInt();
if (guess > correct) {
System.out.println("Too high");
} else if (guess < correct) {
System.out.println("Too low.");
} else {
System.out.println("You guessed it!");
attempts = 7;
}
attempts++;
if (attempts == 7) {
System.out.println("You couldn't guess it in 7 tries. The correct number was " + correct + ".");
}
}
}
}
Goal: Keep adding to a number until told to stop.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Adding Values in a Loop
/// File Name: AddingValues.java
/// Date Finished: Dec 04, 2015
import java.util.Scanner;
public class AddingValues {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int total = 0, current;
System.out.println("I will add up all of the numbers you give me (type 0 to stop).");
while ((current = input.nextInt()) != 0) {
total += current;
System.out.println("Total: " + total);
}
System.out.println("Final Total: " + total);
}
}
Goal: Give the computer hints towards guessing your random number.
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Reverse Hi-Lo
/// File Name: RevHiLo.java
/// Date Finished: Dec 04, 2015
import java.util.Scanner;
public class RevHiLo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int high = 1000, low = 0, guess;
String in = "";
System.out.println("Think of a number from 1-1000. I'll try to guess it.");
while (!in.equals("c")) {
guess = (high - low) / 2 + low;
System.out.println("My guess is " + guess + ". Am I too (l)ow, (h)igh, or (c)orrect?");
in = input.nextLine();
if (in.equals("l")) {
low = guess;
} else if (in.equals("h")) {
high = guess;
} else if (!in.equals("c")) {
System.out.println("I don't know how to respond to \"" + in + "\"!");
}
}
System.out.println("Yay! I guessed it!");
}
}