Reputation: 547
I am beginning Java programming, and I have written a program which tests how many moons each planet has. Here is a shortened version with only four of the planets.
import java.util.Scanner;
import java.util.Random;
public class one {
public static void main(String args[]){
//SET VARIABLES
String zero="Mercury", one="Venus", two="Earth", three="Mars";
//SET VARIABLES
for (int x=1; x<=1000; x++){
System.out.println("Moons");
Random r = new Random();
for (int y=1; y<=1; y++){
int rI = r.nextInt(4);
if (rI == 0){
question(zero,0);
}
else if (rI == 1){
question(one, 0);
}
else if (rI == 2){
question(two, 1);
}
else if (rI == 3){
question(three, 2);
}
}
}
}
public static void question(String n, int num){
Scanner input = new Scanner(System.in);
System.out.print("How many moons does " + n + " have? ");
int ans = input.nextInt();
if (ans == num){
System.out.println("Correct!");
}
else if (ans != num){
System.out.println("Incorrect!");
question(n, num);
}
}
}
How would I go about not having to write "else if" so many times? This becomes very tedious with more statements. Keep in mind I am a beginner, and this code is about the limit of my current abilities.
Upvotes: 2
Views: 366
Reputation: 7133
I would start with forgetting that vector-thing and putting Planets and their Moons together in a POJO (Plain Old Java Object) and putting those objects into the Array:
class Planet {
int moons;
String name;
Planet(String name, int moons) {
this.name = name;
this.moons = moons;
}
public String getName() {
return this.name;
}
public getMoons() {
return this.moons;
}
public void question(){
Scanner input = new Scanner(System.in);
System.out.print("How many moons does " + this.name + " have? ");
int ans = input.nextInt();
if (ans == this.moons){
System.out.println("Correct!");
}
else {
System.out.println("Incorrect!");
}
}
}
public class one {
public static void main(String args[]){
//SET VARIABLES
List<Planet> planets = new ArrayList<Planets>();
Planet earth = new Planet("Earth", 1);
// put other planets
//SET VARIABLES
for (int x=1; x<=1000; x++) {
System.out.println("Moons");
Random r = new Random();
int rI = r.nextInt(planets.size());
Planet p = planets.get(rI);
p.question();
}
}
}
Upvotes: 0
Reputation: 1783
I would prefer enums over arrays here. See this enums tutorial (especially the planets example ;) ). Add another field (just like mass and radius) for moon count.
Upvotes: 0
Reputation: 159874
You could use arrays like so:
String[] planets = { "Mercury", "Venus", "Earth", "Mars" };
int moons[] = { 0, 0, 1, 2 };
and call:
if (rI >= 0 && rI < planets.length) {
question(planets[rI], moons[rI]);
}
Upvotes: 4
Reputation: 129572
You could use the switch
keyword:
switch (rI) {
case 0:
question(zero, 0);
break;
case 1:
question(one, 0);
break;
case 2:
question(two, 1);
break;
case 3:
question(three, 2);
break;
default:
// do something
}
The code under default
executes when no match is found.
You could also use arrays:
String[] planets = new String[]{"Mercury", "Venus", "Earth", "Mars"};
int[] nums = new int[]{0, 0, 1, 2};
...
// in the loop:
question(planets[rI], nums[rI]);
Of course, there are other ways to approach the problem, but I think for someone who is just learning these should do the trick. One thing you might want to look into, however, (once you get deeper into Java), is the concept of a Map
, much like a dictionary in Python. You could maintain a map that maps a planet (string) to the number of moons it has (int).
Upvotes: 0
Reputation: 4749
You could go with switch or if-else as long as you logic does not evolve. Otherwise you need to start the object-oriented programming. Create a class "Planet" and another class for each planet that inherits from Planet and add the planet specific information to each of it. Then you're good for the future when you may plan to add some more questions for the planets.
Upvotes: 1
Reputation: 837
A better way to write this code. It is more readable and it is very easy to make any updates.
import java.util.Scanner;
import java.util.Random;
public class one {
public static void main(String args[]){
//SET VARIABLES
String planets []=new String[4];
planets[0]="Mercury";
planets[1]="Venus";
planets[2]="Earth";
planets[3]="Mars";
int moons []=new int[4];
moons[0]=0;
moons[1]=0;
moons[2]=1;
moons[3]=2;
//SET VARIABLES
while(true){
System.out.println("Moons");
Random r = new Random();
int rI = r.nextInt(4);
question(planets[rI],moons[rI]);
}
}
public static void question(String n, int num){
Scanner input = new Scanner(System.in);
System.out.print("How many moons does " + n + " have? ");
int ans = input.nextInt();
if (ans == num){
System.out.println("Correct!");
}
else if (ans != num){
System.out.println("Incorrect!");
question(n, num);
}
}
}
Upvotes: 1
Reputation: 6555
You can try using switch
instead of the long if-else
ladder.
For more information on switch
read the java documentation
In your case it could be something like:
switch (rI) {
case 0:
question(zero, 0);
break;
case 1:
question(one, 0);
break;
case 2:
question(two, 1);
break;
case 3:
question(three, 2);
break;
default:
break;
}
Upvotes: 0
Reputation: 10184
Take a look at the switch
statement in Java. That is designed to help overcome a screwy if-elseif-elseif-elseif-else block:
switch(rI){
case 2: question(two, 1);
break;
case 3: question(three, 2);
break;
default: doSomethingClever(...);
}
There are some other cleanups you could employ, but getting that switch block in place addresses your initial comment. Good luck.
Upvotes: 0