英文:
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);
英文:
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("Enter value of n: ");
nValue = input.nextInt();
for (int x = 1; x <= nValue; x++){
System.out.print("Enter number " + x + ": ");
num1 = input.nextInt();
num2 = num1 + num1;
total = num2 - num1;
}
System.out.println("Answer: " + 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("Enter the value of n");
int n=obj.nextInt();
System.out.println("Enter"+1+"Number : ");
int num=obj.nextInt();
int sum=num;
for(int i=2;i<=n;i++){
System.out.println("Enter"+i+"Number : ");
num=obj.nextInt();
if(i%2==0){
sum=sum+num;
}
else{
sum=sum-num;
}
}
System.out.println("Answer: "+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("Enter value of n: ");
nValue = input.nextInt();
//variable for total value before loop
int total = 0
for (int x = 1; x <= nValue; x++){
System.out.print("Enter number " + x + ": ");
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("Answer: " + 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("Enter value of n: ");
int n =input.nextInt();
System.out.print("Enter " + n + " integers: \n");
Scanner input = new Scanner(System.in);
System.out.print("Enter value of n: ");
int n =input.nextInt();
System.out.print("Enter " + n + " integers: \n");
System.out.print("Enter number 1:");
int num1 = input.nextInt();
System.out.print("Enter number 2:");
int num2 = input.nextInt();
System.out.print("Enter number 3:");
int num3 = input.nextInt();
System.out.print("Enter number 4:");
int num4 = input.nextInt();
System.out.print("Enter number 5:");
int num5 = input.nextInt();
int a = num1 + num2;
int b = a - num3;
int c = b + num4;
int d = c - num5;
System.out.print("Answer: " + 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 <= nValue; x++){
System.out.print("Enter number " + x + ": ");
num1 = input.nextInt();
if (x % 2 == 0 || x == 1) {
total += num1;
} else {
total -= num1;
}
}
System.out.println("Answer: " + total);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论