Need a little help with java..

C++, C#, Java, PHP, ect...
Post Reply
User avatar
Zak Zillion
Posts: 112
Joined: Thu Apr 07, 2011 12:55 am

Need a little help with java..

Post 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.");
		}
	}
}
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - Albert Einstein

Old Project(s):
The Dark Flame
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Need a little help with java..

Post 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.
Fighting for peace is declaring war on war. If you want peace be peaceful.
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Need a little help with java..

Post 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.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Need a little help with java..

Post 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
The indelible lord of tl;dr
User avatar
Zak Zillion
Posts: 112
Joined: Thu Apr 07, 2011 12:55 am

Re: Need a little help with java..

Post 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!
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - Albert Einstein

Old Project(s):
The Dark Flame
Post Reply

Return to “Coding”