PDA

View Full Version : My java pick 3 simulator



nmanpure
June 25th, 2010, 01:55
This is my pick 3 simulator its pretty simple code but its still code. All you do is enter 3 numbers and if they match the random generated numbers then you win......

You can un-comment the " ("The numbers are "+"("+num+", "+numa+", "+numb+")" ); ", doing so will show the the answers so you can cheat.

To play: enter 1 number press space enter another number press space and another number, hit enter.
Or enter 1 number press enter enter another number press enter and another number then press enter.

you dont have to type 1 you can type any number 1-9

here is the code


package lab2b;
import java.util.Scanner;
import java.util.Random;

/**
*
* @author Noah Schweibinz
*/
public class Main
{


public static void main(String[] args)
{
Scanner keybd = new Scanner(System.in);
Random r1 = new Random();
int pick;
int num,numa,numb, num1, num2, num3;


for( ; ; )
{

num = 1 + r1.nextInt(3); //Test
// System.out.println(num); //Test

numa = 1 + r1.nextInt(3); //Test
// System.out.println(numa); //Test

numb = 1 + r1.nextInt(3); //Test
// System.out.println(numb); //Test

System.out.println("Please type three numbers in for your pick 1-9 each: ");
//System.out.println("The numbers are "+"("+num+", "+numa+", "+numb+")" );

num1 = keybd.nextInt();
num2 = keybd.nextInt();
num3 = keybd.nextInt();


if(num1 == num && num2 == numa && num3 == numb)
{
System.out.println("You have won!");
System.out.println("Your Number were: "+num1 + " , " +num2 + " , "+num3 + " , ");

}else
System.out.println("You lose.");
System.out.println("The Loterry numbers were: "+num + " , "+numa + " , "+numb);
System.out.println("You chose: "+ "(" + num1 + ", "+num2+", "+num3+")");
}
}


}

samuraiblood2
June 25th, 2010, 03:32
Shitty code, but I used to love making small little programs like this. It can be a pretty good way to test yourself. I remade this mostly out of boredom,


import java.util.Scanner;
import java.util.Random;

public class Main {

private static Random random;

public static void main(String[] args) {
boolean verbose = false;
if (args.length == 1) {
verbose = args[0].equalsIgnoreCase("-verbose");
}

Scanner in = new Scanner(System.in);
random = new Random();
int attempts = 0, wins = 0, loses = 0;
boolean retry;
do {
int randOne = getRandomNumber();
int randTwo = getRandomNumber();
int randThree = getRandomNumber();
if (verbose) {
System.out.println("[debug] Generated numbers: " + randOne + ", " + randTwo + ", " + randThree);
}
System.out.print("Pick three numbers from 1 - 9: ");
String[] numbers = in.nextLine().split(" ");
if (numbers.length != 3) {
System.out.println("Need three numbers, try again!");
retry = true;
continue;
}
attempts++;
int one = 0, two = 0, three = 0;
try {
one = Integer.parseInt(numbers[0]);
two = Integer.parseInt(numbers[1]);
three = Integer.parseInt(numbers[2]);
} catch (NumberFormatException e) {
System.out.println("Not a number, try again!");
retry = true;
continue;
}
if (one == randOne && two == randTwo && three == randThree) {
System.out.println("You win!");
wins++;
} else {
System.out.println("You lose!");
loses++;
}
System.out.println("You chose: " + one + ", " + two + ", " + three);
System.out.println("Generated numbers: " + randOne + ", " + randTwo + ", " + randThree);
System.out.println();
System.out.print("Play again? (y/N) ");
String answer = in.nextLine();
retry = answer.equalsIgnoreCase("y");
} while (retry);
System.out.println("attempts: " + attempts);
System.out.println("\twins: " + wins);
System.out.println("\tloses: " + loses);
}

private static int getRandomNumber() {
return (random.nextInt(9) + 1);
}
}

nmanpure
June 25th, 2010, 04:23
Very nice samuria, I just started CS112 so i can learn real Java. im only on week 2.

Austin
June 25th, 2010, 05:10
Good job mate. :3.