2

I was wondering how I should initialize int c from the switch statement. I can't figure out how to "extract" the random switch-argument into the final answer. I am a beginner so maybe I am missing something obvious here. Here is the code that I have so far:

import java.util.*;
public class whileTest{
public static void main (String args[]){
    Scanner sc = new Scanner(System.in);

    String a;

        do{

        String operatorSwitch;
        int b;
        int c;

        Random number = new Random();
        int d = number.nextInt(100) +1;
        int e = number.nextInt(100) +1;

        Random operatorChoice = new Random();
        int operator = operatorChoice.nextInt(4);

        switch (operator){

            case 0: operatorSwitch= "+";
                c = d+e;
                break;
            case 1: operatorSwitch= "-";
                c = d-e;
                break;
            case 2: operatorSwitch= "*";
                c = d*e;
                break;
            case 3: operatorSwitch= "/";
                c = d/e;
                break;
            default: operatorSwitch= "";
        }
        System.out.println("What is: "+d+operatorSwitch+e+"?");
        b = sc.nextInt();


        if(b!=c)
            System.out.println("Wrong answer! Right answer is: "+c);
        else{if(b==c)
            System.out.println("Right answer!"+c);
        }
            System.out.println("Continue? y/n");
            a = sc.next();

        }while(a.equals("y"));
    }
}
  • 1
    Duplicate of http://stackoverflow.com/questions/363681/generating-random-number-in-a-range-with-java?rq=1 – Aaron Kurtzhals Jan 18 '13 at 20:25
  • 3
    Please can you evaluate on what do you exactly want ? Is your problem what @Perception answered or are you looking for something else ? – Fallup Jan 18 '13 at 20:38

2 Answers2

3

As a practice, unless you have a good reason not too, you should always explicitly initialize your variables. In this case you want to change this:

String operatorSwitch;
int b;
int c;

To this:

String operatorSwitch = null;
int b = 0;
int c = 0;

The problem with your current code is a compilation one, due to the variable 'c' possibly not being initialized.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • Optionally, move `c = 0` to the `default` part of the switch. – A--C Jan 18 '13 at 20:34
  • That would work as well. The key thing to keep in mind being that at some point, over any possible branch of execution, that variable needs to be initialized before being accessed. – Perception Jan 18 '13 at 20:36
  • @Perception Yup. I was just putting an alternative since the switch is used before `c` being printed and it has a default case; I usually stick with inits at the top - it's cleaner. – A--C Jan 18 '13 at 20:38
  • 2
    Yes, that was totally it. I will remember to initialize my variables at the beginning from now on. Thank you for your time. –  Jan 18 '13 at 20:51
1

Not sure how pretty you wish to make this but you could write an Operator interface with anonymous subclasses and pick a random index from those.

interface Operator{
    public int operateOn( int a, int b );
    public char getDisplayChar();
}

Operator[] operators = {new Operator(){
        public int operateOn(int a, int b){
            return a + b;
        }

        public char getDisplayChar(){
            return '+';
        }
    }, new Operator(){
        public int operateOn(int a, int b){
            return a - b;
        }

        public char getDisplayChar(){
            return '-';
        }
    } // etc.
    };
flup
  • 26,937
  • 7
  • 52
  • 74