How can i make the calculator have multiple numbers stored, while calculating?

Here are my codes but it seems to only store one number at a time. Hope to upgrade it to 2 or more numbers. I'm not to sure if if it's alright to modify "else if" statement? Open to ideas, Thank you!


else if sender.tag == 16 {

if operation == 12{ //Divide

label.text = String(previousNumber / numberOnScreen)

}

else if operation == 13{ //Multiply

label.text = String(previousNumber * numberOnScreen)

}

else if operation == 14{ //Subtract

label.text = String(previousNumber - numberOnScreen)

}

else if operation == 15{ //Add

label.text = String(previousNumber + numberOnScreen)

}

}

else if sender.tag == 11{

label.text = ""

previousNumber = 0;

numberOnScreen = 0;

operation = 0;

Please:


post the complete func, not a small part.


And Edit the code with formatter tool (<>)

       else if sender.tag == 16 {
            if operation == 12 { //Divide
                  label.text = String(previousNumber / numberOnScreen)
            } else if operation == 13 { //Multiply
                  label.text = String(previousNumber * numberOnScreen)
             }  else if operation == 14 { //Subtract
                   label.text = String(previousNumber - numberOnScreen)
            } else if operation == 15 { //Add
                   label.text = String(previousNumber + numberOnScreen)
            }
         }  else if sender.tag == 11 {
            label.text = ""
            previousNumber = 0;
            numberOnScreen = 0;
            operation = 0;



You asked :

it seems to only store one number at a time. Hope to upgrade it to 2 or more numbers


What do you mean ? Do you want to see all the labels that were entered ?


If so, what you should do adding text to the existing label instead of just setting.


At some point you need also to clear the label

// When starting

label.text = ""          // Clear it

// Then, during operations

       else if sender.tag == 16 {
            if operation == 12 { //Divide
                  label.text = label.text! + String(previousNumber / numberOnScreen)
            } else if operation == 13 { //Multiply
                  label.text = label.text! +String(previousNumber * numberOnScreen)
             }  else if operation == 14 { //Subtract
                   label.text = label.text! +String(previousNumber - numberOnScreen)
            } else if operation == 15 { //Add
                   label.text = label.text! +String(previousNumber + numberOnScreen)
            }
         }  else if sender.tag == 11 {
            label.text = ""
            previousNumber = 0;
            numberOnScreen = 0;
            operation = 0;

When you press "MS" -- short for Memory Store -- the calculator will save the number currently on the screen. This memory lasts while you perform other functions, and even if you press "C" or "Clear" to start a new calculation, but not if you turn off the machine entirely.

Multiples of a number are the result of multiplying a number by a whole number. For example, multiply 2.5 (not a whole number) by 5 (a whole number). The result is 12.5, which means that 12.5 is a multiple of 2.5 since it was multiplied by 5 (a whole number). Compare this to multiplying 2.5 by 5.5.

I made a calculator that does multiple things (adding consecutive numbers, adding multiple numbers, etc) but I am having trouble making it so that the calculator can multiply multiple numbers. So far, I've basically copied the code that adds minus the vat multiple numbers, but I can't figure out how to make it multiply instead of add. Here is my code: import java.util.Scanner;

public class SumOfNumbers { public static void main(String arg[]) { int n; int sum = 0; https://vatonlinecalculator.co.uk/ Scanner s = new Scanner(System.in);

System.out.print("Please enter how many numbers you want to add up to: "); n = s.nextInt();

System.out.println("you entered: " + n + ""); sum = addConsecutiveNumbers(n);

System.out.println("sum of 1 to "+n+" = "+sum);

//following code is sum of any numbers you entered from console //store the numbers into an array int num; int sumOfNums=0;

System.out.print("Please enter how many numbers you want to sum: "); num=s.nextInt(); System.out.println("you want to sum "+num+" numbers ");

sumOfNums = addNumbers(num);

System.out.println("sum of "+num+" numbers = "+sumOfNums); }

//Define a method which add consecutive numbers based on user's input and return the sum of the numbers private static int addConsecutiveNumbers (int number) { int sum = 0; for (int i = 1; i <= number; i++) { sum = sum + i; }

return sum;

}

//Define a method which add numbers based on user's input and return the sum of the numbers

private static int addNumbers (int num) { Scanner s = new Scanner(System.in); int a[] = new int[num]; int sumOfNums = 0;

for(int k = 0; k < num; k++) { System.out.println("enter number "+(k+1)+":"); a[k] = s.nextInt();

System.out.println("The array of a[" + k + "] = " + a[k]); }

for(int j = 1;j < num ; j++) { sumOfNums += a[j]; }

return sumOfNums; } //below is the part of code that I am having trouble with.

public static int multiplyNumbers(int num) { int Area = 0; Scanner s = new Scanner(System.in); int a[] = new int[num]; System.out.println("Please enter how many numbers you want to multiply:"); num=s.nextInt(); for(int l = 0; l < num; l++) { System.out.println("enter number "+(l+1)+":"); a[l] = s.nextInt(); System.out.println("The array of a[" + l + "] = " + a[l]); } return Area; } }

How can i make the calculator have multiple numbers stored, while calculating?
 
 
Q