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

huangapple go评论76阅读模式
英文:

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

问题

我正在尝试使用数组和 switch 语句创建一个计算器。

public class test1 {
    public static void main(String[] args) {
       int length;
       Scanner input = new Scanner(System.in);
       System.out.println("你想对多少个数字执行操作:");
       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("操作符: ");
          op[counter] = input.next().charAt(0);
       }
       
       System.out.println("计算中...");
       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;   
          }
       }
    }
}

我知道它会对两个数字进行加法操作,因为使用了 [f][f+1]

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

我想要类似这样的输出(被注释掉的部分)但我不知道如何像这样迭代。

我想要的输出是这样的:

你想对多少个数字执行操作:
4
1 : 6
操作符: +
2 : 5
操作符: -
3 : 2
操作符: *
4 : 5
操作符: /
计算中...
45

但是我得到的输出是:

你想对多少个数字执行操作:
4
1 : 6
操作符: +
2 : 5
操作符: -
3 : 2
操作符: *
4 : 5
操作符: /
计算中...
11
3
10

我的代码每次只计算两个数字,但我希望它可以计算数组的所有元素并只打印一个答案。这可能吗?

英文:

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(&quot;how many numbers do you wanna perform operation on: &quot;);
length = input.nextInt();
int[] num = new int[length];
char[] op = new char[length];
for (int counter = 0; counter &lt; length; counter++) {
System.out.print((counter + 1) + &quot; : &quot;);
num
0
+
网站访问量
= input.nextInt(); System.out.print(&quot;Operator: &quot;); op
0
+
网站访问量
= input.next().charAt(0); } System.out.println(&quot;Calculating...&quot;); for (int f = 0; f &lt; length; f++) { switch (op[f]) { case &#39;+&#39;: System.out.println(num[f] + num[f + 1]); break; case &#39;-&#39;: System.out.println(num[f] - num[f + 1]); break; case &#39;*&#39;: System.out.println(num[f] * num[f + 1]); break; case &#39;/&#39;: 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&lt;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??

答案1

得分: 0

你只需要更改最后的for循环部分:

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);
英文:

You only have to change your last for statement:

   for (int f = 0; f &lt; length; f++) {
switch (op[f]) {
case &#39;+&#39;: System.out.println(num[f] + num[f + 1]); break; 
case &#39;-&#39;: System.out.println(num[f] - num[f + 1]); break;
case &#39;*&#39;: System.out.println(num[f] * num[f + 1]); break;
case &#39;/&#39;: 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 &lt; length; f++) {
switch (op[f]) {
case &#39;+&#39;: result+= num[f + 1]; break; 
case &#39;-&#39;: result-= num[f + 1]; break;
case &#39;*&#39;: result*= num[f + 1]; break;
case &#39;/&#39;: result/= num[f + 1]; break;   
}
}
System.out.println(result);

答案2

得分: 0

你的问题在于后面的计算没有将之前计算的结果纳入其中。有很多方法可以修复这个问题。这里有一种方法,不需要对代码进行太多改动:

第一种方法,在循环中将每次操作的结果存回 num 数组,替换右操作数位置上的 num[f+1]。这个结果随后成为下一个操作的左操作数。

如果你更希望保持 num 数组的原始内容不受影响,可以这样做:

第二种方法,在循环中使用一个单独的 result 变量来存储之前的计算结果,将其作为操作的左操作数。

以下是第一种方法的示例代码:

       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]);
          }
       }

以下是第二种方法的示例代码:

       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);
          }
       }
英文:

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(&quot;Calculating...&quot;);
for (int f = 0; f &lt; length; f++) {
switch (op[f]) {
case &#39;+&#39;: num[f+1] = num[f] + num[f + 1]; break; 
case &#39;-&#39;: num[f+1] = num[f] - num[f + 1]; break;
case &#39;*&#39;: num[f+1] = num[f] * num[f + 1]; break;
case &#39;/&#39;: num[f+1] = num[f] / num[f + 1]; break;
case &#39;=&#39;: System.out.println(&quot;Result: &quot; + 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(&quot;Calculating...&quot;);
int result = num[0];
for (int f = 0; f &lt; length; f++) {
switch (op[f]) {
case &#39;+&#39;: result = result + num[f + 1]; break; 
case &#39;-&#39;: result = result - num[f + 1]; break;
case &#39;*&#39;: result = result * num[f + 1]; break;
case &#39;/&#39;: result = result / num[f + 1]; break;
case &#39;=&#39;: System.out.println(&quot;Result: &quot; + result);
}
}

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

huangapple
  • 本文由 发表于 2020年10月10日 11:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64289759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定