1.11M Members

What's wrong with this?

 
0
 
package test;
import java.util.Scanner;
public class Test {
    public static void main(String[] args){
        Scanner kbReader = new Scanner(System.in);
        System.out.print("Choose mode (\"words\" or \"symbols\"): ");
        String mode = kbReader.nextLine();
        if ("words".equals(mode))
        {
            String oa = " plus ";
            String os = " minus ";
            String om = " times ";
            String od = " divided by ";
            String e = " equals ";
        }
        else
        {
            String oa = " + ";
            String os = " - ";
            String om = " * ";
            String od = " / ";
            String e = " = ";
        }
        System.out.println("Choose your operation:");
        System.out.println("1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Multiplication");
        System.out.println("4. Division");
        System.out.print("Your choice: ");
        Scanner kbReader2 = new Scanner(System.in);
        int choice = kbReader2.nextInt();
        if (choice > 4)
        {    
            System.out.println("You must enter a 1, 2, 3, or 4.");
            return;
        }
        System.out.print("Enter first integer: ");
        int op1 = kbReader.nextInt();
        System.out.print("Enter second integer: ");
        int op2 = kbReader.nextInt();
        System.out.println("");
        switch (choice)
        {
            case 1:
                System.out.println(op1 + oa + op2 + e + (op1 + op2));
                break;
            case 2:
                System.out.println(op1 + os + op2 + e + (op1 - op2));
                break;
            case 3:
                System.out.println(op1 + om + op2 + e + (op1 * op2));
                break;
            case 4:
                System.out.println(op1 + od + op2 + e + ((double)op1 / op2));
                break;
        }
    }
}

This won't run correctly for some reason. It tells me that the variables oa, os, om, od, and e can not be found, but I declared and initialized them earlier in my first if-then statements...

 
1
 

The bottom part of your code cannot see those variables because they are enclosed in a different scope (inside different braces).

 
0
 

The bottom part of your code cannot see those variables because they are enclosed in a different scope (inside different braces).

I'm really new to Java, and not sure how to change that while preserving the function of my code. Any suggestions?

 
0
 
package operation_of_choice;
import java.util.Scanner;
public class Operation_of_choice {
    public static void main(String[] args) {
        Scanner kbReader = new Scanner(System.in);
        System.out.print("Choose mode (\"words\" or \"symbols\"): ");
        String mode = kbReader.nextLine();
        String oa;
        String os;
        String om;
        String od;
        String e;
        if ("words".equals(mode))
        {
            oa = " plus ";
            os = " minus ";
            om = " times ";
            od = " divided by ";
            e = " equals ";
        }
        else
        {
            oa = " + ";
            os = " - ";
            om = " * ";
            od = " / ";
            e = " = ";
        }
        System.out.println("Choose your operation:");
        System.out.println("1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Multiplication");
        System.out.println("4. Division");
        System.out.print("Your choice: ");
        Scanner kbReader2 = new Scanner(System.in);
        int choice = kbReader2.nextInt();
        if (choice > 4)
        {    
            System.out.println("You must enter a 1, 2, 3, or 4.");
            return;
        }
        System.out.print("Enter first integer: ");
        int op1 = kbReader.nextInt();
        System.out.print("Enter second integer: ");
        int op2 = kbReader.nextInt();
        System.out.println("");
        switch (choice)
        {
            case 1:
                System.out.println(op1 + oa + op2 + e + (op1 + op2));
                break;
            case 2:
                System.out.println(op1 + os + op2 + e + (op1 - op2));
                break;
            case 3:
                System.out.println(op1 + om + op2 + e + (op1 * op2));
                break;
            case 4:
                System.out.println(op1 + od + op2 + e + ((double)op1 / op2));
                break;
        }
    }
}

This fixed it! Thanks for telling me what was wrong, Thines01!

Question Answered as of 2 Years Ago by thines01
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: