如何解决如何在此输出中获得加法>减法>加法>反之亦然的公式?
由于[5 + 23-3 + 14-(-8)],我们总共得到47。我的问题是,我应该使用什么公式从示例中得出47个数字?
这是我的代码,但是由于某种原因,我总是得到-8的答案。任何帮助深表感谢!
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);
解决方法
package com.company;
import java.util.Scanner; 公共课程主要{
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);
}
}
,似乎您要添加一个重置而不是添加值的额外步骤。这是一种不断增加总价值,仅跟踪立即输入的方法。
已为将来的任何路人更新
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);
,
不使用循环,但这确实可以提供您要的输出
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);
,
要确定是增加还是减少总数,您只需要检查const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const keys = require('../config/keys');
passport.use(
new GoogleStrategy(
{
clientID: process.env.clientID,clientSecret: process.env.clientSecret,callbackURL: "http://localhost:1111/api/users/auth/google/callback",//passReqToCallback: true
},(accessToken,refreshToken,profile,callback) => {
console.log("access token",accessToken);
console.log("refresh token",refreshToken);
console.log("profile",profile);
console.log("callback",callback);
}
)
);
的值即可。
如果body = req.body;
body.googleId = req.body.profile.id;
body.firstName = req.body.profile.name.givenName;
body.lastName = req.body.profile.name.familyName;
body.email = req.body.profile.emails[0].value;
body.photoUrl = req.body.profile.photos[0].value;
等于或等于1,则将总数相加。否则,您减去:
x
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。