import java.util.*;
import java.math.*;
import java.text.*;
public class Easy {
public Easy() { }
public static void Easy(){
Scanner sc = new Scanner(System.in);
DecimalFormat nDF = new DecimalFormat("0");
double first, second, third;
String firstguess, secondguess, thirdguess;
String space = " ";
first = Math.floor(Math.random() * (9));
second = Math.floor(Math.random() * (9));
if (second == first) {
second = Math.floor(Math.random() * (9));
} else
second = second;
third = Math.floor(Math.random() * (9));
if (third == first || third == second) {
third = Math.floor(Math.random() * (9));
} else
third = third;
System.out.print("Make a guess (e.g. \"1 2 5\"): ");
String guess = sc.nextLine();
firstguess = guess.substring(0, guess.indexOf(space));
secondguess = guess.substring(guess.indexOf(space), guess.length()-1);
thirdguess = guess.substring(guess.indexOf(space)+2, guess.length());
double fguess=Double.parseDouble(firstguess);
double sguess=Double.parseDouble(secondguess);
double tguess=Double.parseDouble(thirdguess);
if ( fguess == first ) {
System.out.print("Bilbo");
} else if (fguess == second || fguess == third) {
System.out.print("Frodo");
} else
System.out.print("Sam");
if ( sguess == second ) {
System.out.print(" Bilbo");
} else if (sguess == first || sguess == third) {
System.out.print(" Frodo");
} else
System.out.print(" Sam");
if ( tguess == third ) {
System.out.println(" Bilbo");
} else if (tguess == first || tguess == second) {
System.out.println(" Frodo");
} else
System.out.println(" Sam");
// System.out.println("F = " + firstguess);
// System.out.println("S = " + secondguess);
// System.out.println("T = " + thirdguess);
System.out.println(first + " " + second + " " + third);
}
}
Randoming and Repeating questions
Page 1 of 15 Replies - 780 Views - Last Post: 10 November 2008 - 05:42 PM
#1
Randoming and Repeating questions
Posted 10 November 2008 - 04:26 PM
Hi couple of questions about my code, I'm not sure where I went wrong with the randoming, but It seems like it should work but doesn't. I want it to random 3 integers from 0-9 which it does, but they can't be the same which is why i have the if statements, but sometimes there will be doubles ex: 2 7 7. My second question is how do I get it to let the user keep entering guesses until he/she gets all Bilbos as a response? I tried putting a while (fguess != first && sguess != second && tguess != third) { statement in after the double tguess and before the if fguess statements, but it just repeats the loop of whatever the first response was ex: Sam Sam Frodo looped. Thanks for any help!
Replies To: Randoming and Repeating questions
#2
Re: Randoming and Repeating questions
Posted 10 November 2008 - 04:30 PM
second = Math.floor(Math.random() * (9));
if (second == first) {
second = Math.floor(Math.random() * (9));
} else
second = second;
First, you don't need the else in a case like this. second = second; <- Does nothing but use clock-cycles.
I would recommend however that you exchange the above for:
second = Math.floor(Math.random() * (9));
while (second == first) {
second = Math.floor(Math.random() * (9));
}
Same solution can be applied to the third number, keep your logic as it's all good..
This post has been edited by Gloin: 10 November 2008 - 04:31 PM
#3
Re: Randoming and Repeating questions
Posted 10 November 2008 - 04:38 PM
Thanks for the help with the randoming Gloin, the while statement does clean up a little bit of the code =] Could you tell me what you mean by the second = second does nothing but use clock cycles? should it have been second == second? Or does that last part just do nothing for the randoming?
#4
Re: Randoming and Repeating questions
Posted 10 November 2008 - 04:44 PM
It does nothing for your program. second has already been assigned a value by the random-method and there's no point in assigning it to itself. It's like writing 0 = 0, which is stating the obvious.
Using clock-cycles means it uses the power of the CPU to do pretty much nothing useful.
Using clock-cycles means it uses the power of the CPU to do pretty much nothing useful.
This post has been edited by Gloin: 10 November 2008 - 04:44 PM
#5
Re: Randoming and Repeating questions
Posted 10 November 2008 - 04:47 PM
Ok, thanks!
#6
Re: Randoming and Repeating questions
Posted 10 November 2008 - 05:42 PM
Page 1 of 1

New Topic/Question
Reply



MultiQuote





|