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 8829295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:48:08+00:00 2026-06-14T07:48:08+00:00

I have a question about weather it’s legal to use a switch statement inside

  • 0

I have a question about weather it’s legal to use a switch statement inside a for loop. The way its written below Eclipse gives me an error and won’t let me use switch(n) on the second statement in the for loop. What I want to do is write…

This old man, he played 1
He played knick-knack on my thumb.

Is there a better way of solving the problem below with a for loop and switch statement? I want to write out the paragraphs 10 different with the different number and different case. So 1 thumb, 2 shoe, 3 knee…. until 10.

import acm.program.*;

public class SingSong extends ConsoleProgram {

    public void run() {
        for (int n = 1; n <= 10; n++) {
            println("This old man, he played " + n);
            println("He played knick-knack on my" + switch(n));
            println("With a knick-knack, paddy-whack,");
            println("Give your dog a bone");
            println("This old man came rolling home");

            switch (n) {            
                case 1 : println("thumb"); break;
                case 2 : println("shoe"); break;
                case 3 : println("knee"); break;
                case 4 : println("door"); break;
                case 5 : println("hive"); break;
                case 6 : println("sticks"); break;
                case 7 : println("heaven"); break;
                case 8 : println ("pate"); break;
                case 9 : println("spine"); break;
                case 10 : println("shin"); break; 
            }
        } 
    }
}
  • 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-14T07:48:09+00:00Added an answer on June 14, 2026 at 7:48 am

    What you want is a method you’ll call:

    private String which(int n) {
      switch (n) {
      case 1 : return "thumb";
      case 2 : return "show";
      case 3 : return "knee";
      case 4 : return "door";
      case 5 : return "hive";
      case 6 : return "sticks";
      case 7 : return "heaven";
      case 8 : return "pate";
      case 9 : return "spine";
      case 10 : return "shin";
      default: return "";
    
      }
    }
    

    Then replace

    println("He played knick-knack on my" + switch(n));
    

    with

    println("He played knick-knack on my" + which(n));
    

    Another, more succint approach would be to create an array of strings:

    static final String[] which = {
      "thumb","show","knee","door","hive", "sticks","heaven","pate","spine","shin"};
    

    and then just use

    println("He played knick-knack on my" + which[n]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about SqlDataReader: Is there some way to modificated the values
i have question about two different Bundle object in below methods : onSaveInstanceState(Bundle outState);
i have question about why should we use return in get,if dont use what
Today I have question about Eclipse . I use this IDE very long and
I have question about the d3 javascript libarary. I want to use the azimuthal
I have question about parsing in Html helper : I have sth like: @foreach
I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass
I have question about XSLT1.0. The task is to write out in HTML all
I have question about normalization. Suppose I have an applications dealing with songs. First
I have question about interpreting strings as packed binary data in C++. In python,

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.