5 Replies - 780 Views - Last Post: 10 November 2008 - 05:42 PM Rate Topic: -----

#1 expl0si0n34   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 25-October 08

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!

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);
		
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Randoming and Repeating questions

#2 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

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

Was This Post Helpful? 1
  • +
  • -

#3 expl0si0n34   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 25-October 08

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?
Was This Post Helpful? 0
  • +
  • -

#4 Gloin   User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

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.

This post has been edited by Gloin: 10 November 2008 - 04:44 PM

Was This Post Helpful? 0
  • +
  • -

#5 expl0si0n34   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 25-October 08

Re: Randoming and Repeating questions

Posted 10 November 2008 - 04:47 PM

Ok, thanks!
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Randoming and Repeating questions

Posted 10 November 2008 - 05:42 PM

View Postexpl0si0n34, on 10 Nov, 2008 - 03:47 PM, said:

Ok, thanks!

The easiest solution for random int is to create a Random object and call its nextInt() method

Random ran = new Random();
int n = ran.nextInt(10); // returns an int between 0 and 9 inclusive
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1