2017-11-16 3 views
1

사용자에게 10 개의 숫자를 사용하는 프로그램을 작성하도록 지시 받았습니다. 그럼 그것을 표시하고 평균 (입력 10 숫자)에서 그 차이를 찾으십시오. 나는 잠시 동안 코드를 엉망으로 했어.하지만 내가 뭘 잘못하고 있는지 모르겠다. 브라우저에서 열 때 계산이 올바르지 않습니다.배열의 프롬프트에서 여러 입력을 함께 추가하려면 어떻게합니까?

var number = new Array(10); 
for(var i=0; i<10; i++) { 
var number = prompt("PLEASE ENTER A NUMBER"); 
total = parseInt(number); 
average = total/10; 
document.write("The difference from the average for the number, " + number + 
"is equal to " + (number - average) + "<br>"); 
} 
+3

글쎄, '합계'가 합계가 아닙니다. 그러므로'average'는 평균이 아니며'number'는 코드가 실행 된 후 배열이 아닙니다. –

답변

0
var number = new Array(10); 
    var total; //create a variable for your total 
    var average; //create a variable for your average 
    for(var i=0; i<10; i++) 
    { 
    var number[i] = prompt("PLEASE ENTER A NUMBER"); //make sure all the 
                //numbers have their 
                //own ids which is 'i' 
                //in this case 
    total += parseInt(number); //this basically means total = previoustotal   
      //+ newly parsed number 
    } 

    average = total/10; //leave this outside because you want to do the 
         //calculations at the end 

    for(var i=0; i < number.length ; i++) 
    { 
    document.write("The difference from the average for the number," + 
    number[i] + "is equal to " + (number[i] - average) + "<br>"); 
    } //this will print out a difference output for all 10 numbers you 
     //inputted 

당신의 CSS와 HTML이 그래서 난 당신의 모든 입력하면 다른 번호를 얻기도 전에 평균을 계산하기 시작 그것 때문에이

0

에서 BR을 떠날거야 만들어진 방법 확실하지. 모든 입력을 기다린 다음 계산을 수행하는 것이 좋습니다.

그리고 총 숫자를 추가해야합니다. 아마 당신이 필요로하는 것은 당신의 수학을 올바르게 얻는 것입니다. 내가 먼저 나의 친애하는 친구에게

var number = new Array(10); 
 
var total = 0; 
 
for(var i=0; i<10; i++) { 
 
    number[i] = parseInt(prompt("PLEASE ENTER A NUMBER\n")); 
 
    total += parseInt(number[i]); 
 
} 
 
var average = total/number.length 
 
for(var i=0; i<10; i++) { 
 
    alert("The difference from the average for the number, " + number[i] + 
 
"is equal to " + (number[i] - average)); 
 
}

0

안녕하세요, 당신은 콘솔 또는 웹에 대한 코드 할 질문이 있습니다?

var number = new Array(10); 
var total = 0; 
for(var i=0; i<10; i++) { 
var number = console.log("PLEASE ENTER A NUMBER\n"); 
total = total + parseInt(readline()); 
} 
average = total/10; 
console.log("The difference from the average for the number, " + number + 
"is equal to " + (number - average)); 

를하고 방법을 당신은 단순히 당신의 코드를 변경할 수 있습니다 : 당신이 콘솔하고있는 경우

당신은 다음과 같이 작성해야합니다

var number = new Array(10); 
var total = 0 ; 
    for(var i=0; i<5; i++) { 
    var number = prompt("PLEASE ENTER A NUMBER"); 
    total = total + parseInt(number); 
    } 
    average = total/5; 
    document.write("The difference from the average for the number, " + number + 
    "is equal to " + (number - average) + "<br>"); 

var number = new Array(10); 
 
var total = 0 ; 
 
    for(var i=0; i<5; i++) { 
 
    var number = prompt("PLEASE ENTER A NUMBER"); 
 
    total = total + parseInt(number); 
 
    } 
 
    average = total/5; 
 
    document.write("The difference from the average for the number, " + number + 
 
    "is equal to " + (number - average) + "<br>");

+1

OP는'prompt()'를 사용하므로 대답을 알 수 있습니다 :) – msanford

+0

OP는 무엇입니까? 내 친구??? – shahabvshahabi

+1

원본 포스터를 의미합니다. 질문을 한 사람. – msanford

2

처음에는 배열이 전혀 필요하지 않습니다. 단지 합계가 필요합니다. 요약 :

var total = 0; 
 

 
for (var count = 1; count <= 10; count++) { 
 
    const number = parseInt(prompt("A number (again):")); 
 
    //increase total by the inputted number 
 
    total += number; 
 
    const average = total/count; 
 
    document.body.innerHTML += `The ${count} number is ${number}, the average is ${average} and they differ by ${average - number}`; 
 
}

당신은 (일정한 일을 처리하기 위해 더 나은 원인),뿐만 아니라 템플릿 문자합니다 (${..}로 나는 가능한 한 많은 const를 사용한 것을 알 수 있습니다 물건) 그것은 내 의견으로는 더 읽기 쉽게 만든다.

+1

나는 자유를 실행 가능 스 니펫으로 변환하는 데 신경을 쓰지 않았 으면 좋겠다. – msanford

+0

@ msanford thanks;) 나는 누군가가 모바일 장치의 버튼을 감추게하는 원인을 만들 수 없었다 ... –

+1

나는 당신이 모바일 장치에 쓰는 것을 +10 할 수 있었으면 좋겠다. 스 니핑 편집기를 사용할 수 없다는 것이 좋을 것입니다. 모바일에서는 끔찍한 일입니다. – msanford

관련 문제