| View previous topic :: View next topic |
| Author |
Message |
elpacco Grandmaster Cheater Supreme
Reputation: 30
Joined: 16 Oct 2007 Posts: 1267
|
Posted: Thu Oct 28, 2010 5:45 pm Post subject: Anyone wanna reorganize this evenly |
|
|
between the class and tester?
I wrote 99% of it in the class, 'cus that's how I do.
| Code: | import java.util.Random;
import java.util.Scanner;
public class NumberGuesser
{
public NumberGuesser()
{
rand = 0;
guess = 0;
state = 0;
guesses = 0;
level = 0;
maxrand = 1;
input = new Scanner(System.in);
}
public void promptLevel()
{
while(level>5 || level<=0)
{
System.out.println("What level would you like to play?\n1:\t0-20\n2:\t0-100\n3:\t0-500\n4:\t0-25000\n5:\t0-1000000");
level = input.nextInt();
if(level>5 || level<=0)
System.out.println("Invalid level!\nPlease enter a number between 1 and 5.");
}
switch(level)
{
case 1: maxrand = 21; break;
case 2: maxrand = 101; break;
case 3: maxrand = 501; break;
case 4: maxrand = 25001; break;
case 5: maxrand = 1000001; break;
default: break;
}
playGame();
}
private void playGame()
{
Random gen = new Random();
while(true)
{
rand = 0;
guess = 0;
guesses = 0;
state = 0;
rand = gen.nextInt(maxrand);
System.out.println("Number generated!");
for(guesses = 0; state != 3; guesses++)
{
System.out.println("Guess number!");
while(true)
{
guess = input.nextInt();
if(guess>=maxrand || guess<0)
System.out.println("Your guess was not in the range for Level "+level+"!\nGuess a number INSIDE the range for Level "+level);
else break;
}
if(guess==rand) state = 3;
else if(guess>rand) state = 1;
else state = 2;
switch(state)
{
case 1: System.out.println("Too high!"); break;
case 2: System.out.println("Too low!"); break;
case 3: System.out.println("Correct!"); break;
default: break;
}
}
System.out.println("YOU WIN!\nYou guessed the correct number of "+rand+" after "+guesses+" tries!");
System.out.print("Play again? (Y/N)");
if(input.next().equalsIgnoreCase("y"))
{
System.out.print("Change level? (Y/N)");
if(input.next().equalsIgnoreCase("y")) level = 0;
promptLevel();
}
else break;
}
}
private int rand;
private int guess;
private int state;
private int guesses;
private int level;
private int maxrand;
private Scanner input;
} |
Tester:
| Code: | public class PlayNumberGuesser
{
public static void main(String[] args)
{
NumberGuesser game = new NumberGuesser();
game.promptLevel();
}
} |
_________________
| [AM]Misery wrote: |
| FangBanger wrote: | | What is the best way for a lv19 Soldier to solo Sledge on Borderlands? | Shoot him. |
|
|
| Back to top |
|
 |
Aniblaze Grandmaster Cheater Supreme
Reputation: 138
Joined: 23 Apr 2006 Posts: 1757 Location: The Netherlands
|
Posted: Thu Oct 28, 2010 6:49 pm Post subject: |
|
|
Why? I'm only seeing three functions here, one of which is the constructor. If it's the size of the playGame() function you are concerning yourself with, I would suggest making a string variable which contains all of the text, and returning that string to the main. That way you can replace the if/else statements with ternary operators more easily. Considering a lot of the function is System.out.println and if/else statements.
As a secondary option you might consider splitting the randomizing and states from the actual printlns. But all in all I thing that would only create a surplus in code, albeit the code will be more readable. Or just use Netbeans and do some automated refactoring: see what happens.
|
|
| Back to top |
|
 |
elpacco Grandmaster Cheater Supreme
Reputation: 30
Joined: 16 Oct 2007 Posts: 1267
|
Posted: Thu Oct 28, 2010 7:24 pm Post subject: |
|
|
| Augustine wrote: | Why? I'm only seeing three functions here, one of which is the constructor. If it's the size of the playGame() function you are concerning yourself with, I would suggest making a string variable which contains all of the text, and returning that string to the main. That way you can replace the if/else statements with ternary operators more easily. Considering a lot of the function is System.out.println and if/else statements.
As a secondary option you might consider splitting the randomizing and states from the actual printlns. But all in all I thing that would only create a surplus in code, albeit the code will be more readable. Or just use Netbeans and do some automated refactoring: see what happens. | All it's going to do is this. Do you not agree that it's literally ten times more efficient this way than splitting it into a class and tester? Initially I wrote it one hundred percent in the tester file. My teacher didn't like that. So then I put it all in a class file (which did nothing but double the number of lines and make it twice as complex and much less efficient), and she didn't like that either. Apparently all System.outs and the Scanner have to be in the test file, everything else I'm planning on keeping in the class.
_________________
| [AM]Misery wrote: |
| FangBanger wrote: | | What is the best way for a lv19 Soldier to solo Sledge on Borderlands? | Shoot him. |
|
|
| Back to top |
|
 |
|