lundi 5 octobre 2015

How can I use Strings outside of if statements?

I'm trying to program a simple calculator. I know how to divide and multiply, but here's my problem. I have a boolean, num1, showing that the user is entering in the first digit. Then I say that the String numb1 is what the user enters in. I then have a series of if statements that just see if there is a number. Then it says that num1 is now false, but num2 is true. Then I repeat this, except it sees if num2 is true, and now numb2 is input. My problem is that outside of these if statements (the ones seeing if num2 or num1 is true) it says "Enter your operation: div, mul, add, sub" Now I have if statements asking if the user used each of those operations. I start with div. I say:

 int divresult = numb1/numb2;

Unfortunately, because this operation is outside the previous if statements, it cannot find the numb1 or numb2 variables. If I put it in one of the if statements, it cannot find the other. If I try to say the numb1 and numb2 are both the user input inside of the div thing, I get an error.

        if (num1 = true) {
            String numb1 = input;
        System.out.println("Enter your first number.");
    if (input.contains("1")) {
     num1 = false;
     num2 = true;   

     System.out.println(numb1);
    }
    if (input.contains("2")) {
         num1 = false;
         num2 = true;

         System.out.println(numb1);
        }

Aucun commentaire:

Enregistrer un commentaire