How to get a formula of add > subtract > add > vice versa in this output?

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

How to get a formula of add > subtract > add > vice versa in this output?

问题

We get a total of 47 because of [5 + 23 - 3 + 14 - (-8)]. My question is what formula should I use to get a total of 47 from the example?

这是我的代码,但是我总是得到-8的答案,原因是什么我不清楚。非常感谢任何帮助!

int nValue, total = 0, num1, num2;
Scanner input = new Scanner(System.in);

System.out.print("输入 n 的值:");
nValue = input.nextInt();

for (int x = 1; x <= nValue; x++){
    System.out.print("输入数字 " + x + ":");
    num1 = input.nextInt();

    num2 = num1 + num1;
    total = num2 - num1;
}
System.out.println("答案:" + total);
英文:

How to get a formula of add > subtract > add > vice versa in this output?

We get a total of 47 because of [5 + 23 - 3 + 14 - (-8)]. My question is what formula should I use to get a total of 47 from the example?

This is my code but I always get an answer of -8 for some reason. Any help is much appreciated!

        Scanner input = new Scanner(System.in);

        System.out.print(&quot;Enter value of n: &quot;);
        nValue = input.nextInt(); 

        for (int x = 1; x &lt;= nValue; x++){
            System.out.print(&quot;Enter number &quot; + x + &quot;: &quot;);
            num1 = input.nextInt();

            num2 = num1 + num1;
            total = num2 - num1;
        }
        System.out.println(&quot;Answer: &quot; + total);


</details>


# 答案1
**得分**: 1

```java
package com.company;
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner obj = new Scanner(System.in);
        System.out.println("请输入 n 的值");
        int n = obj.nextInt();
        System.out.println("请输入第 1 个数字 : ");
        int num = obj.nextInt();
        int sum = num;
        for (int i = 2; i <= n; i++) {
            System.out.println("请输入第 " + i + " 个数字 : ");
            num = obj.nextInt();
            if (i % 2 == 0) {
                sum = sum + num;
            } else {
                sum = sum - num;
            }
        }
        System.out.println("答案: " + sum);
    }
}
英文:
package com.company;

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
    Scanner obj=new Scanner(System.in);
    System.out.println(&quot;Enter the value of n&quot;);
    int n=obj.nextInt();
    System.out.println(&quot;Enter&quot;+1+&quot;Number : &quot;);
    int num=obj.nextInt();
    int sum=num;
    for(int i=2;i&lt;=n;i++){
        System.out.println(&quot;Enter&quot;+i+&quot;Number : &quot;);
        num=obj.nextInt();
        if(i%2==0){
            sum=sum+num;
        }
        else{
            sum=sum-num;
        }
    }
    System.out.println(&quot;Answer: &quot;+sum);
}

}

答案2

得分: 0

看起来你多做了一步重置而不是添加值。以下是一种方法,可以持续将值添加到总值中,只需跟踪即时输入。

已更新,供未来经过的人使用

        Scanner input = new Scanner(System.in);

        System.out.print("输入 n 的值:");
        nValue = input.nextInt(); 
        // 在循环之前的总值变量
        int total = 0;
        for (int x = 1; x <= nValue; x++){
            System.out.print("输入第 " + x + " 个数字:");
            num = input.nextInt();
            // 每次迭代时添加或减去总值(已更新逻辑)
            if (x%2==0){
               total += num;
            }
            else {
               total -= num;
            }
        }
        System.out.println("答案:" + total);
英文:

It appears you are adding an extra step that is resetting instead of adding values. Here is a way to keep adding to the total value and only keeping track of the immediate input.

Updated for any future passer-bys

    Scanner input = new Scanner(System.in);

    System.out.print(&quot;Enter value of n: &quot;);
    nValue = input.nextInt(); 
    //variable for total value before loop
    int total = 0
    for (int x = 1; x &lt;= nValue; x++){
        System.out.print(&quot;Enter number &quot; + x + &quot;: &quot;);
        num = input.nextInt();
        //add or subtract to total with each iteration (updated logic)
        if (x%2==0){
           total += num;
        }
        else {
           total -= num;
        }
    }
    System.out.println(&quot;Answer: &quot; + total);

答案3

得分: 0

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter value of n: ");
        int n = input.nextInt();
        System.out.print("Enter " + n + " integers: \n");

        int[] numbers = new int[n];
        for (int i = 0; i < n; i++) {
            numbers[i] = input.nextInt();
        }

        int sum = numbers[0] + numbers[1];
        sum -= numbers[2];
        sum += numbers[3];
        sum -= numbers[4];

        System.out.print("Answer: " + sum);
    }
}
英文:

Not using loops but this does give the output you're asking for

Scanner input = new Scanner(System.in);
        
        System.out.print(&quot;Enter value of n: &quot;);
        int n =input.nextInt();
        System.out.print(&quot;Enter &quot; + n + &quot; integers: \n&quot;);
        
        Scanner input = new Scanner(System.in);
        
        System.out.print(&quot;Enter value of n: &quot;);
        int n =input.nextInt();
        System.out.print(&quot;Enter &quot; + n + &quot; integers: \n&quot;);
        
        System.out.print(&quot;Enter number 1:&quot;);
        int num1 = input.nextInt();
        System.out.print(&quot;Enter number 2:&quot;);
        int num2 = input.nextInt();
        System.out.print(&quot;Enter number 3:&quot;);
        int num3 = input.nextInt();
        System.out.print(&quot;Enter number 4:&quot;);
        int num4 = input.nextInt();
        System.out.print(&quot;Enter number 5:&quot;);
        int num5 = input.nextInt();
        
        int a = num1 + num2;
        int b = a - num3;
        int c = b + num4;
        int d = c - num5;
        
        System.out.print(&quot;Answer: &quot; + d);

答案4

得分: 0

确定是应该对总数进行加法还是减法,您只需要检查 x 的值。

如果 x 为偶数或等于1,则进行加法操作。否则,进行减法操作:

for (int x = 1; x <= nValue; x++){
    System.out.print("输入第 " + x + " 个数字: ");
    num1 = input.nextInt();
    if (x % 2 == 0 || x == 1) {
        total += num1;
    } else {
        total -= num1;
    }
}
System.out.println("答案: " + total);
英文:

To determine whether you should add to, or subtract from the total, you just need to check the value of x.

If x is even or equal to 1, you add to the total. Otherwise you subtract:

    for (int x = 1; x &lt;= nValue; x++){
        System.out.print(&quot;Enter number &quot; + x + &quot;: &quot;);
        num1 = input.nextInt();
        if (x % 2 == 0 || x == 1) {
            total += num1;
        } else {
            total -= num1;
        }
    }
    System.out.println(&quot;Answer: &quot; + total);

huangapple
  • 本文由 发表于 2020年9月20日 10:13:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63974941.html
匿名

发表评论

匿名网友

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

确定