编写一个网页,当在文本框中输入一个整数并单击“计算”按钮时,可以计算n+(n-1)+(n-2)+(n-3)……….+1,并弹出如图2所示的信息窗口显示计算的结果
时间: 2024-05-22 12:12:01 浏览: 18
<!DOCTYPE html>
<html>
<head>
<title>计算n的阶乘</title>
<meta charset="utf-8">
<script>
function calculate() {
var num = document.getElementById("num").value;
var result = 1;
for (var i = num; i >= 1; i--) {
result *= i;
}
alert("结果为:" + result);
}
</script>
</head>
<body>
<h1>计算n的阶乘</h1>
<p>请输入一个整数n:</p>
<input type="number" id="num">
<button onclick="calculate()">计算</button>
</body>
</html>
相关问题
编写程序,设计一个窗体及相关控件,在第一个文本框中输入一个整数,单击按钮时在第二个文本框中显示该数的类型:完全数、不足数或丰沛数。
以下是一个用Python Tkinter模块编写的程序,用于判断输入整数的类型:
```python
import tkinter as tk
def classify_num():
# 获取输入的整数
num = int(num_entry.get())
# 判断整数类型
divisors_sum = sum([i for i in range(1, num) if num % i == 0])
if divisors_sum == num:
result_label.config(text="完全数")
elif divisors_sum < num:
result_label.config(text="不足数")
else:
result_label.config(text="丰沛数")
# 创建窗口
window = tk.Tk()
window.title("整数类型判断程序")
# 创建控件
num_label = tk.Label(window, text="请输入一个整数:")
num_entry = tk.Entry(window)
classify_button = tk.Button(window, text="确定", command=classify_num)
result_label = tk.Label(window, text="")
# 控件布局
num_label.grid(row=0, column=0)
num_entry.grid(row=0, column=1)
classify_button.grid(row=1, column=0, columnspan=2)
result_label.grid(row=2, column=0, columnspan=2)
# 运行窗口
window.mainloop()
```
运行程序后,会显示一个窗口,用户可以在第一个文本框中输入一个整数,单击按钮后会在第二个文本框中显示该数的类型:完全数、不足数或丰沛数。
实验 2:编写图形用户界面的应用程序。 要求:编制求两个正整数的最小公倍数的程序。(1)两个文本框 txt、txt2,用来输入整型数 据;(2)一个按钮;一个不可编辑的文本组件 txt3。当单击按钮时,在 txt3 中显示两个整数 的最小公倍数。
好的,这是一个编写图形用户界面的应用程序的实验题目,要求编写一个程序来求两个正整数的最小公倍数。具体的实现要求如下:
1. 首先添加两个文本框,用来输入两个整数。
2. 再添加一个按钮,当用户单击这个按钮时,程序会计算这两个整数的最小公倍数。
3. 最后添加一个不可编辑的文本组件,用来显示计算结果。
下面是这个程序的 Python 代码实现:
```python
import tkinter as tk
# 计算两个整数的最小公倍数
def lcm(x, y):
# 获取两个整数中较大的数
greater = max(x, y)
# 循环判断最小公倍数
while True:
if greater % x == 0 and greater % y == 0:
lcm = greater
break
greater += 1
return lcm
# 创建主窗口
root = tk.Tk()
root.title("求最小公倍数")
# 创建文本框组件
txt1 = tk.Entry(root, width=10)
txt2 = tk.Entry(root, width=10)
txt1.pack(side=tk.LEFT, padx=5, pady=5)
txt2.pack(side=tk.LEFT, padx=5, pady=5)
# 创建按钮组件
btn = tk.Button(root, text="计算最小公倍数", command=lambda: txt3.insert("end", str(lcm(int(txt1.get()), int(txt2.get()))) + "\n")
btn.pack(side=tk.LEFT, padx=5, pady=5)
# 创建文本组件
txt3 = tk.Text(root, width=20, height=5)
txt3.pack(side=tk.LEFT, padx=5, pady=5)
# 运行主程序
root.mainloop()
```
在这个程序中,我们首先定义了一个函数 `lcm`,用来计算两个整数的最小公倍数。然后我们创建了一个主窗口,并在主窗口中添加了两个文本框、一个按钮和一个文本组件。
当用户单击按钮时,程序会调用 `lcm` 函数来计算两个整数的最小公倍数,并将结果添加到文本组件中显示出来。
这个程序可以直接运行,并且可以在图形界面中输入两个整数,然后单击按钮来计算它们的最小公倍数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)