Page 1 of 1

Need a little help with java..

Posted: Fri Oct 14, 2011 4:47 am
by Zak Zillion
So I am currently making a small java app that spits out random Einstein quotes. Help please!

Code: Select all

import java.util.*;

class HelloWorld {
	public static void main(String[] args) {
		int rn = 1 + (int)(Math.random()*5);
		System.out.println(rn);
		if (rn == '1') {
			System.out.println("I am convinced that He (God) does not play dice.");
		}
		else if (rn == '2') {
			System.out.println("Reality is merely an illusion, albeit a very persistent one.");
		}
		else if (rn == '3') {
			System.out.println("The only thing that interferes with my learning is my education.");
		}
		else if (rn == '4') {
			System.out.println("Imagination is more important than knowledge.");
		}
		else if (rn ==  '5') {
			System.out.println("The secret to creativity is knowing how to hide your sources.");
		}
	}
}

Re: Need a little help with java..

Posted: Fri Oct 14, 2011 7:29 am
by Chris
Whats the problem? The only thing i can see really wrong with it is that you are comparing rn to strings rather than integers

Code: Select all

if( rn  == 1 ) { .... }
Rarher than

Code: Select all

if( rn  == '1' ) { .... }
Also you might want to use a switch rather than a load of ifs.

Re: Need a little help with java..

Posted: Sat Oct 15, 2011 7:38 pm
by Xaleph
Several things go wrong: you are checking an int with a string, fails. And the random is an arbitrary number somewhere between:
0,00000000000000001 and 0,9999999999999999999

So to get a random integer number between 1 and 6 use this: Math.Floor(Math.Random() * 5); which returns an int value.

Oh casting the Math.Random ( Double i believe ) by default makes it 0.

Now, check them like this:

if(rn == 1) or if ( rn == 2 ) et cetera.

Re: Need a little help with java..

Posted: Sat Oct 15, 2011 7:56 pm
by Jackolantern
It sounds like the other posters have touched on the biggest problems, but I wanted to tell you about one of the most powerful debugging features of Java (outside of an actual debugger), which really more languages should add (C# I am looking at you lol): Assertions! They are almost like little unit tests you can add to your code which will throw exceptions (aka errors) if what you put in the assertions are not true. You don't even have to remove them from your finished application, because they don't do anything unless you explicitly turn them on in the compiler! Assertions are very powerful and well-worth learning more about! Below is Oracle's tutorial on how to use them:

Oracle's tutorial/documentation on Assertions

Re: Need a little help with java..

Posted: Sun Oct 16, 2011 3:20 am
by Zak Zillion
Thanks guys for all your help! I feel kinda dumb now that I realize I was comparing it to a string :) . This is the finished code.

Code: Select all

import java.util.*;

class HelloWorld {
	public static void main(String[] args) {
		int rn = 1 + (int)(Math.random()*5);
		if (rn == 1) {
			System.out.println("I am convinced that He (God) does not play dice.");
		}
		else if (rn == 2) {
			System.out.println("Reality is merely an illusion, albeit a very persistent one.");
		}
		else if (rn == 3) {
			System.out.println("The only thing that interferes with my learning is my education.");
		}
		else if (rn == 4) {
			System.out.println("Imagination is more important than knowledge.");
		}
		else if (rn ==  5) {
			System.out.println("The secret to creativity is knowing how to hide your sources.");
		}
	}
}
P.S. @jackolantern I plan on reading up on it but im just getting into basic I/O so its gonna take me a little bit before I can get to it. Thanks for the link though!