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

  • Home
  • SEARCH
  • 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 6861331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:31:16+00:00 2026-05-27T02:31:16+00:00

I have an issue with accessing variables initialized in a switch-statement, and then using

  • 0

I have an issue with accessing variables initialized in a switch-statement, and then using them outside switch.

This is what my code should produce:
What is: X (+-* or /) X

Here’s my Java code:

import java.util.Random;
import java.util.Scanner;

public class Test2 {
        public static void main(String[]args){

        Random rand = new Random();
        Scanner sc = new Scanner(System.in);

        int svar, ans;
        int tal1 = rand.nextInt(100) + 1;
        int tal2 = rand.nextInt(100) + 1;

        String characters = "+-*/";
        int r = rand.nextInt(characters.length());
        char randomChar = characters.charAt(r);

        switch(randomChar){
        case '+':
            ans = tal1 + tal2;
            break;
        case '-':
            ans = tal1 - tal2;
            break;
        case '*':
            ans = tal1 * tal2;
            break;
        case '/':
            ans = tal1 / tal2;
            break;
        }

        System.out.println("What is: "+tal1+randomChar+tal2+"?");
        svar = sc.nextInt();

        if (svar == ans)
            System.out.println("Correct!");
        else
            System.out.println("Wrong - Right answer: "+ans);

        }
}

As you can see, “ans” has been declared and initialized. However, I want to utilize the ans variable in both the if statement (in order to compare the result) and the output.

Now I could expand each case and write both the if statements and the outputs, but the code would be too exessive as I’m trying to do a similar code, but smaller. Any tips how this work would work?

Here’s the compilation error I get:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The local variable ans may not have been initialized
    The local variable ans may not have been initialized

    at Test2.main(Test2.java:36)
  • 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-05-27T02:31:17+00:00Added an answer on May 27, 2026 at 2:31 am

    Add a default case to catch any input that isn’t recognized. In your case, this could throw a RuntimeException, since something is wrong with your code if this case is reached.

    default: 
      throw new RuntimeException("Unexpected operation: " + randomChar);
    

    The error you are getting means that there is a code path that fails to initialize ans. Do not take the easy way out and initialize ans with a meaningless value when it is declared! That will just hide this valuable warning about a real problem with your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue with an ASP.NET profile implementation. This is my profile code:
I have an issue that is driving me a bit nuts: Using a UserProfileManager
We have an issue using the PEAR libraries on Windows from PHP . Pear
I have an issue with using AWK to simply remove a field from a
I have an issue with watin test that should browse for pdf and upload
I have a really strange issue when using hibernate to connect to a MySQLDB
I have read a number of questions on this site about this issue, I
Seems like everyone have issue accessing local machine or internet etc from emulator. All
I am currently working on http://thetradinghouse.co.nz and I have an issue when accessing the
I have an uninstall issue with an app which is using sqlite: during installation

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.