Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8472183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:59:28+00:00 2026-06-10T16:59:28+00:00

I am beginning Java programming, and I have written a program which tests how

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:59:30+00:00Added an answer on June 10, 2026 at 4:59 pm

    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]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Java but not to programming. I'm reading the book Beginning Android
I am just beginning to program in Java and I noticed that String is
I have a small java program that searches the contents of all *.txt files
I'm beginning to program in Java and I'm wondering if the equivalent to the
I'm reading Beginning Java Game Programming 2/e and the book heavily emphasizes using AffineTransform.
I am new to C++ and have programming knowledge only in Java. Can anyone
I'm just beginning to learn Java and I have been very frustrated about learning
I'm beginning to work on a game server written in Java. Granted, Java is
I'm a Java novice taking a beginning programming class and I'm trying to finish
I'm currently beginning to program with Java. I tried to code the sequence in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.