我的代码如下。我的目标是编写一个javascript代码,提示用户输入1表示加法,2表示减法,或0表示退出。当您输入1或2进行加法或减法时,它应该写入屏幕"您已选择加法(或减法)",然后提示您输入第一个数字,然后输入第二个数字,然后显示结果" (num1)添加到(num2),结果是(sum)",所有都在蓝色标题下,带有一个黄花背景(我发现的部分)。我是一个新手javascript。发生的事情是,当我输入最后一个号码时,它正在将所有内容一次性写入屏幕。如何在输入时将每个部分写入屏幕?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Jeremy's Assignment 4 Solution</title>
<div style = "background-color:lightgoldenrodyellow; color: blue; text-align: center;">
<script type = "text/javascript">
<!--
var now = new Date(); //current date and time
var hour = now.getHours(); // current hour (0-23)
// determine whether it's morning
if (hour < 12 )
document.write( "<h2>Good Morning, " );
// determine whether the time is PM
if ( hour >= 12 )
{
// convert to a 12-hour clock
hour = (hour - 12);
//determine whether it is before 6 PM
if ( hour < 6 )
document.write( "<h2>Good Afternoon, " );
// determine whether it is after 6 PM
if ( hour >= 6 )
document.write( "<h2>Good Evening, " );
} // end if
document.writeln( " Welcome to Jeremy's Assignment 4 Solution.</h2>" );
// -->
</script>
</div>
<div style = "text-align:center">
<script type = "text/javascript">
var choice = choice = window.prompt("Enter 1 for Addition, 2 for Subtraction, or 0 to Quit", "0");
while (choice != 0) {
if (choice == 1) {
document.write("<p style='text-align:center;'>You have selected to perform Addition</p>");
// read in first number from user as a string
num1 = window.prompt("Enter first integer");
// read in second number from user as a string
num2 = window.prompt("Enter second integer");
//convert numbers from strings to integers
number1 = parseInt(num1);
number2 = parseInt(num2);
sum = number1 + number2; // add the numbers
//display the results
document.writeln("<p style='text-align:center;'> " + num1 + " added to " + num2 + " the result is " + sum + "</p>");
}
else if (choice == 2)
{
document.write("<p style='text-align:center;'>You have selected to perform Subtraction</p>");
// read in first number from user as a string
num1 = window.prompt("Enter first integer");
// read in second number from user as a string
num2 = window.prompt("Enter second integer");
//convert numbers from strings to integers
number1 = parseInt(num1);
number2 = parseInt(num2);
sum = number1 - number2; // add the numbers
//display the results
document.writeln("<p style='text-align:center;'> " + num1 + " subtracted from " + num2 + " the result is " + sum + "</p>");
}
else
(choice == 0)
{
document.write("<p style='text-align:center;'>You have selected to exit</p>");
break;
}
}
</script>
</div>
</head>
<body>
<!-- Button to go to home page -->
<p style="text-align: center">
<button style="height:25px;width:100px" onclick="window.location='index.html'">HOME</button>
</p>
</body>
</html>
&#13;