问题出在displayInfo函数中。我运行程序,一切正常,但它没有给我输出。输出应该都在一个盒子里,应该看起来我将提供的图片,但是在所有月份。 output example
function main() {
alert("Welcome to the program");
var endProgram = "no";
while (endProgram == "no") {
var notGreenCosts = [12];
var goneGreenCosts = [12];
var savings = [12];
var months = ["January", "February", "March ", "April ", "May ", "June ", "July ", "August", "September", "October", "November", "December"];
getNotGreen(notGreenCosts, months);
getGoneGreen(goneGreenCosts, months);
energySaved(notGreenCosts, goneGreenCosts, savings);
displayInfo(notGreenCosts, goneGreenCosts, savings, months);
endProgram = prompt("Do you want to end the program? Yes or no?");
}
}
function getNotGreen(notGreenCosts, months) {
var counter = 0;
while (counter < 12) {
notGreenCosts[counter] = parseFloat(prompt("Enter NOT GREEN energy costs for " + months[counter]));
counter++;
}
}
function getGoneGreen(goneGreenCosts, months) {
var counter = 0;
while (counter < 12) {
goneGreenCosts[counter] = parseFloat(prompt("Enter GONE GREEN energy costs for " + months[counter]));
counter++;
}
}
function energySaved(notGreenCosts, goneGreenCosts, savings) {
var counter = 0;
while (counter < 12) {
savings[counter] = parseFloat((notGreenCosts[counter] - goneGreenCosts[counter]));
counter++;
}
}
function displayInfo(notGreenCosts, goneGreenCosts, savings, months) {
var counter = 0;
var outputString = "Month \t\t\t not green \t gone green \t savings \n\n";
while (counter < 12) {
outputString += months[counter] + "\t\t\t" + notGreenCosts[counter] + "\t\t\t" + goneGreenCosts[counter] + "\t\t\t" + savings[counter] + "\r\n";
counter++;
}
}
main();
alert("End of program");
答案 0 :(得分:0)
实际上您没有在displayInfo
方法中显示任何内容:
function displayInfo(notGreenCosts, goneGreenCosts, savings, months) {
var counter = 0;
var outputString = "Month \t\t\t not green \t gone green \t savings \n\n";
while (counter < 12) {
outputString += months[counter] + "\t\t\t" + notGreenCosts[counter] + "\t\t\t" + goneGreenCosts[counter] + "\t\t\t" + savings[counter] + "\r\n";
counter++;
}
alert(outputString); // -----> add this line
}
还有另一个问题没有引起任何问题,但你似乎误解了数组声明
您的var notGreenCosts = [12];
应更改为var notGreenCosts = new Array(12);
。其他数组声明也是如此
检查:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
答案 1 :(得分:0)
我重写了你的代码并提出了这个问题。我添加了评论,可以帮助您了解正在发生的事情。我将添加原始代码中的代码段,后面带有注释。
你只有一个循环的数组,所以你只需要一个while循环。
您在这里使用相同的数值初始化数组。这不是必需的。
var notGreenCosts = [12];
var goneGreenCosts = [12];
var savings = [12];
在函数中,您尝试使用循环中传递的计数器从数组中获取值。如果使用嵌套的while循环执行此功能。
function getNotGreen(notGreenCosts, months) {
var counter = 0;
while (counter < 12) {
notGreenCosts[counter] = parseFloat(prompt("Enter NOT GREEN energy costs for " + months[counter]));
counter++;
}
}
然后你会得到:
notGreenCosts[0] // returns 12 months[0] // returns January
notGreenCosts[1] // returns undefined months[1] // returns February
notGreenCosts[2] // returns undefined months[2] // returns March
notGreenCosts[3] // returns undefined months[3] // returns April
notGreenCosts[4] // returns undefined months[4] // returns May
这些变量可以通过初始化它们来设置并传递给最顶层的函数。不将它们设置为数组。
var notGreenCosts = [12];
但是我继续进行初始化并将它们分配给一行中返回的函数值。
var notGreenCosts = getNotGreen(months, counter);
function main(){ 警报(&#34;欢迎来到该计划&#34;);
//inital "End Program" answer
var endProgram = "no";
// Array of Months
var months = ["January", "February", "March ", "April ", "May ", "June ", "July ", "August", "September", "October", "November", "December"];
var counter = 0;
//inital coounter value. Set to ero to match the zero index months array
// while loop will iterate over the months on at a time until it reaches the end of the array. index 11.
while (counter < 11) {
//if the answer to the ed progrm promt question is "no" then run the function and fet the answers.
if (endProgram === "no") {
var notGreenCosts = getNotGreen(months, counter); // run the getNotGreen function and assign the returned value to the notGreenCosts variable.
var goneGreenCosts = getGoneGreen(months, counter); // run the getGoneGreen function and assign the returned value to the goneGreenCosts variable.
var savings = energySaved(notGreenCosts, goneGreenCosts); // run the energySaved function and assign the returned value to the savings variable.
var outPit = displayInfo(notGreenCosts, goneGreenCosts, savings, months, counter); // run the displayInfo function and assign the returned value to the outPit variable.
console.log(outPit); // pint the out come to the console.
// end the program alert.
endProgram = prompt("Do you want to end the program? Yes or no?");
} else {
return // user wants to end the program. Jumps to the end alert.
}
counter++; //add 1 to the counter.
}
}
function getNotGreen(months, counter) {
var answer = parseFloat(prompt("Enter NOT GREEN energy costs for " + months[counter])); // insure that the intered number is a whole number
return answer; // return the anwser.
}
function getGoneGreen(goneGreenCosts, months, counter) {
var answer = parseFloat(prompt("Enter GONE GREEN energy costs for " + months[counter])); // insure that the intered number is a whole number
return answer; // return the anwser.
}
function energySaved(notGreenCosts, goneGreenCosts) {
return parseFloat((notGreenCosts - goneGreenCosts)); // return the calulated number and insure that is a whole number.
}
function displayInfo(notGreenCosts, goneGreenCosts, savings, months, counter) {
var outputString = "Month \t\t\t not green \t gone green \t savings \n\n";
return outputString += months[counter] + "\t\t\t\t" + notGreenCosts + "\t\t\t" + goneGreenCosts + "\t\t\t\t" + savings + "\r\n"; // return the value string.
}
//run the program.
main();
// end the program
alert("End of program");