dimanche 20 mai 2018

How do I make the code so that int base = y, exponent = z; can pull from the result within the if/else statement?

I'm trying to write a code that can solve exponential questions, and can solve exponential questions from integer inputs and word inputs (i.e one, two... etc). I've pretty much figured out the integer parts, however i'm stuck on the "Word input" part. Mainly, the final part in my code for the word part (int base = y, exponent = z), in which i can calculate the exponent doesn't work because y and z cannot be pulled from the if/else if statements

How do i make my code so that int base = y, exponent = z can pull from their respective if/else if codes?

Thanks a bunch!

public static void main(String[] args) {
    Scanner choice = new Scanner(System.in);
    System.out.println("Enter 1 for Alphabetical Input, 2 for numerical input:");
    int solution = choice.nextInt();

    if(solution == 1) {
        for(int i=1; i<=100;i++) {
            Scanner words = new Scanner(System.in);

            System.out.print("Enter your base");
            String x = words.toString();
            String x1 = "zero";
            String x2 = "one";
            String x3 = "two";
            String x4 = "three";
            String x5 = "four";
            String x6 = "five";
            String x7 = "six";
            String x8 = "seven";
            String x9 = "eight";
            String x10 = "nine";
            String x11 = "ten";

            if(x == x1) {
                int y = 0;
            }
            else if(x == x2) {
                int y = 1;
            }
            else if(x == x3) {
                int y = 2;
            }
            else if(x == x4) {
                int y = 3;
            }
            else if (x == x5) {
                int y = 4;
            }
            else if (x == x6) {
                int y = 5;
            }
            else if (x == x7) {
                int y = 6;
            }
            else if (x == x8) {
                int y = 7;
            }
            else if (x == x9) {
                int y = 8;
            }
            else if (x == x10) {
                int y = 9;
            }
            else if (x == x11) {
                int y = 10;
            }
            else {
                System.out.println("I can't read that!");
            }
            System.out.println("Enter your exponent");

            Scanner exponent = new Scanner(System.in);

            String a = exponent.toString();


            if(a == x1) {
                int z = 0;
            }
            else if(a == x2) {
                int z = 1;
            }
            else if(a == x3) {
                int z = 2;
            }
            else if(a == x4) {
                int z = 3;
            }
            else if (a == x5) {
                int z = 4;
            }
            else if (a == x6) {
                int z = 5;
            }
            else if (a == x7) {
                int z = 6;
            }
            else if (a == x8) {
                int z = 7;
            }
            else if (a == x9) {
                int z = 8;
            }
            else if (a == x10) {
                int z = 9;
            }
            else if (a == x11) {
                int z = 10;
            }
            else {
                System.out.println("I can't read that!");
            }
            int base = y, exponents = z; //this does not work, y and z aren't pulled 
            double result = Math.pow(base, exponents);
            }

    }
    else if(solution ==2 ) {
        for(int i=1;i<=100;i++){
            Scanner number = new Scanner(System.in);

            System.out.print("Enter your base:");

            int x = number.nextInt();

            Scanner up = new Scanner(System.in);

            System.out.print("Enter your exponent:");

            int y = up.nextInt();

            int base = x, exponent = y;
            double result = Math.pow(base, exponent);

            System.out.println("Answer = " + result);
        }
    }
    else {
        System.out.println("Could not read input");
    }
}

}

Aucun commentaire:

Enregistrer un commentaire