我用简单的代码编写了这个程序,但是我对Java语言的了解不够,无法正确使用所有语法。我正在开始编程,请帮助我更正我的代码。它目前根本没有运行。我们仍然在循环,这确实给我带来了麻烦。这是大学班。 (我知道这格式不正确,所以我要寻求帮助)

function main() {
alert("Welcome to the program");
var fatGrams = getFatGrams(fatGrams);
var calories = getCalories(fatGrams,calories);
var percentage = caloriesPercentage(fatGrams,calories);
displayPercentage(percentage);
}

function getFatGrams(fatGrams) {
fatGrams = prompt("Enter the number of fat grams in your food item");
while(fatGrams < 0){
  alert("Error, the fat grams cannot be less than 0.");
  fatGrams = prompt("Enter the new number of fat grams.");
 }
return fatGrams;
}

function getCalories(fatGrams,calories) {
calories = prompt("Enter the number of calories in your food item.");
while (calories < 9 || calories > (fatGrams * 9)){
  alert("Error, the calories cannot be less than 9 or exceed the fat    grams * 9");
  calories = prompt("Enter the new number of calories");
}
return calories;
}

function caloriesPercentage(fatGrams,calories){
percentage = (fatGrams * 9) / calories;
alert("The amount of calories that come from fat is, " + percentage);
return percentage;
}

function displayPercentage(percentage){
  if(percentage < 0.3){
  alert("The food is low in fat.");
 }
  else{
 alert("The food is not too low in fat.");
 }
}

main();
alert("End of program");

最佳答案

您的代码中的错误:


不需要function关键字来调用函数。在调用函数之前删除关键字function,只需将函数调用为getFatGrams(fatGrams);
您没有将参数传递给function caloriesPercentage。将其更改为caloriesPercentage(fatGrams,calories);
while循环的表达式应放在括号中,并以while(fatGrams < 0)括起来。
OR应写为||
使用+连接2个字符串(或一个字符串和一个变量)

"The amount of calories that come from fat is, " + percentage

while的表达式也应放在括号中,作为if(percentage < 0.3),并且不需要thenif的大括号应在else之前关闭。

if(percentage < 0.3){
    alert("The food is low in fat.");
}
else {
    alert("The food is not too low in fat.");
}

prompt中的值分配给变量为

fatGrams = prompt("Enter the new number of fat grams.");



您的最终代码应如下所示:

function main() {
  alert("Welcome to the program");
  var fatGrams;
  var calories;
  var percentage;
  getFatGrams(fatGrams);
  getCalories(fatGrams,calories);
  caloriesPercentage(fatGrams,calories);
  displayPercentage(percentage);
}

function getFatGrams(fatGrams) {
  fatGrams = prompt("Enter the number of fat grams in your food item");
  while(fatGrams < 0){
    alert("Error, the fat grams cannot be less than 0.");
    fatGrams = prompt("Enter the new number of fat grams.");
  }
  return fatGrams;
}

function getCalories(fatGrams,calories) {
  calories = prompt("Enter the number of calories in your food item.");
  while (calories < 9 || calories > (fatGrams * 9)){
    alert("Error, the calories cannot be less than 9 or exceed the fat    grams * 9");
    calories = prompt("Enter the new number of calories");
  }
  return calories;
}

function caloriesPercentage(fatGrams,calories){
  percentage = (fatGrams * 9) / calories;
  alert("The amount of calories that come from fat is, " + percentage);
  return percentage;
}

function displayPercentage(percentage){
  if(percentage < 0.3){
    alert("The food is low in fat.");
  }
  else{
    alert("The food is not too low in fat.");
  }
}

main();
alert("End of program");

关于javascript - JavaScript初学者语法错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36298422/

10-11 18:06