-
June 17th, 2013, 11:02 PM
#1
Making a int read multiple ints (Scanners)?
Im not too sure, I did some googling, and I think this method is called "Scanner", where one int is able to take more than 1 variable, however, whenever I try to use scanner it always says "Scanner class file not found"
Code:
import java.util.Scanner;
public class SimpleMath
{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
int num1;
int num2;
int num3;
int num4;
int num5;
System.out.print( "Enter five positive integers, with a single space in between each.");
}
}
^ Taken off of a site, same method tho... Not my code
So anyway, heres my question:
I am trying to make a program where it first generates 4 random numbers (lets say for example reasons the numbers are 5 9 2 3)
So the user now has to guess what these numbers are, however, heres how I done it (if there is a better approach, im open to ideas):
there are 4 ints, each int is named after a placement in the number, like int one int two int three int four
then, the user can type in an int called code
so now what I want to do is have it so if they sat 5923 then it will take 5 from code and see if it matches int one, then 9 from code, see if it matches int two, and so on...
(Also just a side issue, how would I make code incorporate spaces, so that way people can enter 5923 or 5 9 2 3, and it all works the same)
Thanks for any help anyone can provide.
My code:
Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Random;
public class PINDecoder
{
public static void write (String text)
{
System.out.print (text);
}
public static void writeLine (String text)
{
System.out.println (text);
}
public static void write (int number)
{
System.out.print (number);
}
public static void writeLine (int number)
{
System.out.println (number);
}
public static void write (char character)
{
System.out.print (character);
}
public static void writeLine (char character)
{
System.out.println (character);
}
public static String readString ()
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
String var = "";
try
{
var = br.readLine ();
}
catch (IOException ioe)
{
System.out.println (ioe.toString ());
}
return var;
}
public static int readInt ()
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
String tempVar = null;
try
{
tempVar = br.readLine ();
}
catch (IOException ioe)
{
System.out.println (ioe.toString ());
}
int var = Integer.parseInt (tempVar);
return var;
}
public static char readChar ()
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
char var = 0;
try
{
var = (char) br.read ();
}
catch (IOException ioe)
{
System.out.println (ioe.toString ());
}
return var;
}
public static void main (String args[])
{
char decode = 0;
char exit = 0;
int counter;
int code = 0;
int trys = 0;
Random rand = new Random();
int one = rand.nextInt(10);
int two = rand.nextInt(10);
int three = rand.nextInt(10);
int four = rand.nextInt(10);
{
write ("PIN (Personal Identification Number) Decoder");
write ("");
write ("You are given unlimited time to attempt to decode the randomly selected 4 digit code.");
writeLine ("");
writeLine ("After you get it right, it will tell you how many guesses it took you... Have fun...");
write ("");
while(exit != 'N' || exit != 'n') {
writeLine ("");
writeLine ("Type 'Crack' to begin decoding the 4 randomly selected numbers");
writeLine ("");
decode = readChar ();
if (decode == 'C' || decode == 'c') {
writeLine ("Generating PIN...");
try{
Thread.currentThread().sleep(3000);
}
catch(Exception e){
}
writeLine (one + " | " + two + " | " + three + " | " + four);
code = readInt();
}
while (code != one && code != two && code != three && code != four)
{trys = trys + 1;
writeLine ("Incorrect. Try Again. (" + trys + ")");
code = readInt();
}
if (code == one && code == two && code == three && code == four)
{
writeLine ("Code has been broken");
writeLine ("Decoded PIN: " + one + " | " + two + " | " + three + " | " + four);
}
PS: I am still learning Java, I showed this code to a friend, and he told me it was weak, just a little FYI incase anyone cares...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|