两个数字之和以及它们之间的数字

时间:2018-11-12 08:44:31

标签: javascript

询问用户输入数字。然后要求用户输入一个比第一个数字大的数字。计算一个值,该值是从第一个数字到第二个数字(包括端点)的所有数字的总和。例如,如果用户输入5和10,则计算5 + 6 + 7 + 8 + 9 + 10的总和。显示结果。 必须使用整个循环

这是我到目前为止所拥有的。我似乎无法得出正确的数字。如果从上面的示例中输入数字,则得到的数字是49,而不是45。根据我的代码,我理解哪里出错了以及为什么是49,但无法弄清楚如何正确设置。

// declare constants
const INITIAL_VALUE = 0;
const COUNTER_VALUE = 1;

// declare variables
var number1;
var number2;
var sum;
var counter;
var difference;
var middlePoint;
var middlePointSum;

// assigning values
sum = INITIAL_VALUE;
difference = INITIAL_VALUE;
counter = COUNTER_VALUE;
middlePoint = COUNTER_VALUE;
middlePointSum = INITIAL_VALUE;

// prompt user to enter two numbers
number1 = prompt("Enter first number: ");
number2 = prompt("Enter a number bigger than first number: ");

// convert user input into numbers
number1 = Number(number1);
number2 = Number(number2);

// display number1
document.write(number1);
difference = number2 - number1;
middlePoint += number1;

while (counter < difference) {
   document.write(" + " + middlePoint);
   middlePoint = middlePoint + 1;
   counter++;
   middlePointSum += middlePoint;
}

// calculate the sum
sum = number1 + middlePointSum + number2;
 
// display number1, middle points, number2, and sum
document.write(" + " + number2 + " = " + sum);

7 个答案:

答案 0 :(得分:1)

欢迎使用StackOverflow!

错误原因在代码的这一部分

while (counter < difference) {
    document.write(" + " + middlePoint);
    middlePoint = middlePoint + 1; // you are already increasing the middlePoint number
    counter++;
    middlePointSum += middlePoint;
}

因此,总和始终为number of counter - 1。您应该做的是先执行middlePointSum += middlePoint;,然后再增加middlePointcounter的值。

while (counter < difference) {
    document.write(" + " + middlePoint);
    middlePointSum += middlePoint;
    middlePoint = middlePoint + 1;
    counter++;
}

答案 1 :(得分:1)

NAME
lseek - move the read/write file offset
SYNOPSIS
#include <unistd.h>

off_t lseek(int fildes, off_t offset, int whence);

答案 2 :(得分:1)

此代码应为您工作。 Variabel sum应该包含您想要的金额。

var sum = 0;
var firstNumber = 0;
var secondNUmber = 0;
var firstNumberStr = '';
var secondNUmberStr = '';

// prompt user to enter two numbers
firstNumberStr = prompt("Enter first number: ");

while(isNaN(firstNumberStr)){
    firstNumberStr = prompt("Enter Correct first number: ");
}

secondNUmberStr = prompt("Enter a number bigger than first number: ");

while(isNaN(secondNUmberStr) || parseInt(firstNumberStr) >= parseInt(secondNUmberStr)){
    secondNUmberStr = prompt("Enter a number bigger than first number: ");
}

firstNumber = parseInt(firstNumberStr); 
secondNUmber = parseInt(secondNUmberStr);

while(firstNumber <= secondNUmber){
    sum += firstNumber;
    firstNumber++
}

答案 3 :(得分:1)

您在代码中犯了一个很小的错误。该错误位于while循环中。

jdbc_driver_library => "//path_to_jar/mysql-connector-java-5.1.46.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"

您要在计算MiddlePointSum之前递增中间点。计算middlepointSum之后,应将其递增。

 while (counter < difference) {
        document.write(" + " + middlePoint);
        middlePoint = middlePoint + 1;
        counter++;
        middlePointSum += middlePoint;
    }

答案 4 :(得分:1)

您的middlePointSum从1 intead到0开始,所以您得到了5 + (7 + 8 + 9 + 10) + 10来将代码集MiddlePointSum修复为INITIAL_VALUE

为简化起见,只需忽略数字之间的差异,将数字1递增,直到达到数字2

var number1 = prompt("Enter first number: ");
var number2 = prompt("Enter a number bigger than first number: ");
var incNumber;
var strResult = [];

number1 = incNumber = Number(number1);
// use incNumber if you need to preserve number1 value or just use number 1 value instead
number2 = Number(number2);

var sum = 0;
while(incNumber <= number2) {
  strResult.push(incNumber);
  sum += incNumber++;
  // this means add incNumber to sum then increment it by 1
}

document.write(`${strResult.join(' + ')} = ${sum}`);

答案 5 :(得分:-1)

一个问题是您的middlePointSum没有考虑到number1之后的第一个数字,例如您的示例中的6。另一个问题是您的counter < difference测试不准确-您必须迭代直到counter + 1 < difference

// declare constants
const INITIAL_VALUE = 0;
const COUNTER_VALUE = 1;

// declare variables
var number1;
var number2;
var sum;
var counter;
var difference;
var middlePoint;
var middlePointSum;

// assigning values
sum = INITIAL_VALUE;
difference = INITIAL_VALUE;
counter = COUNTER_VALUE;
middlePoint = COUNTER_VALUE;
middlePointSum = INITIAL_VALUE;

// prompt user to enter two numbers
number1 = prompt("Enter first number: ");
number2 = prompt("Enter a number bigger than first number: ");

// convert user input into numbers
number1 = Number(number1);
number2 = Number(number2);

// display number1
document.write(number1);

difference = number2 - number1;
middlePoint += number1;

middlePointSum = middlePoint;
while (counter + 1 < difference) {
  document.write(" + " + middlePoint);
  middlePoint = middlePoint + 1;
  counter++;
  console.log(middlePointSum);
  middlePointSum += middlePoint;
}

// calculate the sum
sum = number1 + middlePointSum + number2;

// display number1, middle points, number2, and sum
document.write(" + " + number2 + " = " + sum);

但这似乎远比需要的复杂得多-为什么不简单地从number1迭代到number2,并一路添加到sum

const number1 = Number(prompt("Enter first number: "));
const number2 = Number(prompt("Enter a number bigger than first number: "));
document.write(number1);
let sum = number1;
for (let i = number1 + 1; i <= number2; i++) {
  sum += i;
  document.write(' + ' + i);
}
document.write(' = ' + sum);

或者,用加号创建数字数组join,然后用reduce计算总和:

const number1 = Number(prompt("Enter first number: "));
const number2 = Number(prompt("Enter a number bigger than first number: "));

const nums = Array.from(
  { length: number2 - number1 + 1 },
  (_, i) => i + number1
);
const sum = nums.reduce((a, b) => a + b);
document.write(nums.join(' + ') + ' = ' + sum);

答案 6 :(得分:-1)

var num1 = Number(prompt("Enter a number"));
var num2 = Number(prompt("Enter a larger number"));


document.write(num1 + "+" + num2 + "=" + (num1+num2));



// now lets find the sum of numbers between these two numbers.
document.write("<br>");  // new line

var counter = num1;   // we will start countring from the smaller number
var sum = 0;

//keep looping until we reach the bigger number (num2)
while(counter<=num2) {
  sum += counter;
  // print the current number
  document.write(counter);
  //print a plus sign
  if(counter<num2) { 
    // only put a (plus sign) if we are not yet reached num2
    document.write("+"); 
   }
   
   //increment to control the loop
  counter ++;
  
}


//finally put the result
document.write("=" + sum);

您正在将counter设置为常数COUNTER_VALUE,即1

然后,在您的程序中,您输入56作为输入。区别在于6-5,即1

您的while loop具有仅在counter<difference时执行的条件。就您而言,counter=1difference=1。这意味着它们相等,因此循环将不会执行。

我希望这可以阐明您需要如何解决此错误。