简体   繁体   中英

Is there any way to iterate a var in- System.out.println(here);? //(what Im tryina explain is written after my code.)

I am trying to create a calculator using array and switch Case.

public class test1 {
    public static void main(String[] args) {
       int length;
       Scanner input = new Scanner(System.in);
       System.out.println("how many numbers do you wanna perform operation on: ");
       length = input.nextInt();
       
       int[] num = new int[length];
       char[] op = new char[length];
       
       for (int counter = 0; counter < length; counter++) {
          System.out.print((counter + 1) + " : ");
          num[counter] = input.nextInt();
          System.out.print("Operator: ");
          op[counter] = input.next().charAt(0);
       }
       
       System.out.println("Calculating...");
       for (int f = 0; f < length; f++) {
          switch (op[f]) {
             case '+': System.out.println(num[f] + num[f + 1]); break; 
             case '-': System.out.println(num[f] - num[f + 1]); break;
             case '*': System.out.println(num[f] * num[f + 1]); break;
             case '/': System.out.println(num[f] / num[f + 1]); break;   
          }
       }
    }
}

i know it adds 2 nums bcz [f] and [f+1];

    //System.out.println(for(int x=0;x<length;x++) 
    //{ 
    //System.out.println(num[f+x]);}); 

wanted something like this(commented part) but dont know how we can iterate like this.

The output I want is something like...

    how many numbers do you wanna perform operation on:
    4
    1 : 6
    Operator: +
    2 : 5
    Operator: -
    3 : 2
    Operator: *
    4 : 5
    Operator: =
    Calculating...
    45
//but the output i am getting is
how many numbers do you wanna perform operation on: 
    4
    1 : 6
    Operator: +
    2 : 5
    Operator: -
    3 : 2
    Operator: *
    4 : 5
    Operator: =
    Calculating...
    11
    3
    10

my code is only calculating 2nums ata time ,but I want it to calculate all the elements of the array and print only 1 answer. is it possible??

You only have to change your last for statement:

   for (int f = 0; f < length; f++) {
      switch (op[f]) {
         case '+': System.out.println(num[f] + num[f + 1]); break; 
         case '-': System.out.println(num[f] - num[f + 1]); break;
         case '*': System.out.println(num[f] * num[f + 1]); break;
         case '/': System.out.println(num[f] / num[f + 1]); break;   
      }
   }

You should not print in every loop, instead you should calculate the result in the loop and print the final result after the loop. like this;

double result=num[0];
for (int f = 0; f < length; f++) {
      switch (op[f]) {
         case '+': result+= num[f + 1]; break; 
         case '-': result-= num[f + 1]; break;
         case '*': result*= num[f + 1]; break;
         case '/': result/= num[f + 1]; break;   
      }
   }
System.out.println(result);

Your problem is that later calculations are not incorporating the results of earlier calculations. There are lots of ways you could fix this. Here's one way that doesn't require you to change things around very much:

       System.out.println("Calculating...");
       for (int f = 0; f < length; f++) {
          switch (op[f]) {
             case '+': num[f+1] = num[f] + num[f + 1]; break; 
             case '-': num[f+1] = num[f] - num[f + 1]; break;
             case '*': num[f+1] = num[f] * num[f + 1]; break;
             case '/': num[f+1] = num[f] / num[f + 1]; break;
             case '=': System.out.println("Result: " + num[f]);
          }
       }

We store the result of each operation back into the num array, in place of the right-hand operand at `num[f+1]. That result then becomes the left-hand operand of the next operation.

If you'd rather keep the original contents of num untouched, you could do it like this:

       System.out.println("Calculating...");
       int result = num[0];
       for (int f = 0; f < length; f++) {
          switch (op[f]) {
             case '+': result = result + num[f + 1]; break; 
             case '-': result = result - num[f + 1]; break;
             case '*': result = result * num[f + 1]; break;
             case '/': result = result / num[f + 1]; break;
             case '=': System.out.println("Result: " + result);
          }
       }

Here we use a separate result variable to store the previous results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM