使用switch语句编写程序,输入灯泡的功率值,得到其亮度并输出。假设灯泡的功率存储在变量watts中(单位是W),标准灯泡的期望亮度使用变量lumens存储(单位是lm),如下表所示。如果功率值不在表内,将变量lumens赋值为-1
时间: 2024-10-09 16:12:06 浏览: 54
你可以使用switch语句结合if条件来编写这个程序。首先,定义一个函数或代码块,接受灯泡功率值watts作为输入,并根据给定的标准亮度对应关系进行处理。以下是一个简单的示例:
```python
# 定义灯泡功率和亮度对照表
lamp_brightnesstable = {
10: 50,
20: 80,
30: 120,
40: 160,
# 可以继续添加更多的功率等级和亮度值
}
def get_brightness(watts):
lumens = -1
if watts in lamp_brightnesstable:
lumens = lamp_brightnesstable[watts]
else:
print("功率值不在表内")
return lumens
# 输入灯泡功率并获取亮度
power_value = int(input("请输入灯泡的功率值(W):"))
brightness = get_brightness(power_value)
print(f"灯泡的亮度为 {brightness} lm.")
相关问题
c语言里使用switch语句编写程序,输入灯泡的功率值,得到其亮度并输出。假设灯泡的功率存储在变量watts中(单位是W),标准灯泡的期望亮度使用变量lumens存储(单位是lm),如下表所示。如果功率值不在表内,将变量lumens赋值为-1
在C语言中,你可以使用`switch`语句结合`case`结构来实现这个功能。首先,你需要定义一个`switch`块,根据灯泡功率`watts`的不同值对应不同的亮度级别。例如:
```c
#include <stdio.h>
int main() {
int watts = ...; // 输入的灯泡功率
int lumens;
switch (watts) {
case 60: // 等于或小于60瓦特的标准灯泡
lumens = 450;
break;
case 100:
lumens = 750;
break;
case 150:
lumens = 1000;
break;
case 200:
lumens = 1250;
break;
default: // 如果功率不在上述范围
lumens = -1;
printf("功率值超出范围,亮度无法确定。\n");
break;
}
printf("灯泡功率 %d 瓦特对应的亮度为 %d 流明。\n", watts, lumens);
return 0;
}
```
在这个例子中,用户输入的灯泡功率会被程序检查,然后根据`switch`内的每个`case`分支分配相应的亮度值。如果功率不在预设的几个值中,就会执行`default`部分的代码,将`lumens`设置为-1,并显示一条消息。
3.编写一段创建一个表单的HTML 代码,表单包含以下内容: 1)一个用于输入用户名的文本小组件 2)4 个复选框,各项内容为: a)4 个25 瓦的灯泡,金额为$2.39 b)8 个25 瓦的灯泡,金额为$4.29 c)4 个25 瓦的长寿命灯泡,金额为$3.95 d)8 个25 瓦的长寿命灯泡,金额为$7.49 3)由三个单选按钮组成的一个按钮集,标签为: a)Visa b)MasterCard c)Discover 再编写一个PHP 脚本,计算订购的灯泡在加了6.2%的销售税之后的总金额。程序必须返回给顾客一个表格,表格中准确地列出订购的商品。并且php版本为5.6
以下是创建表单的 HTML 代码:
```
<form method="post" action="order.php">
<label for="username">用户名:</label>
<input type="text" id="username" name="username"><br><br>
<label for="bulbs">选择灯泡:</label><br>
<input type="checkbox" id="bulbs1" name="bulbs[]" value="4 bulbs, 25watts, $2.39">
<label for="bulbs1">4个25瓦灯泡,金额为$2.39</label><br>
<input type="checkbox" id="bulbs2" name="bulbs[]" value="8 bulbs, 25watts, $4.29">
<label for="bulbs2">8个25瓦灯泡,金额为$4.29</label><br>
<input type="checkbox" id="bulbs3" name="bulbs[]" value="4 bulbs, 25watts, long-lasting, $3.95">
<label for="bulbs3">4个25瓦长寿命灯泡,金额为$3.95</label><br>
<input type="checkbox" id="bulbs4" name="bulbs[]" value="8 bulbs, 25watts, long-lasting, $7.49">
<label for="bulbs4">8个25瓦长寿命灯泡,金额为$7.49</label><br><br>
<label>支付方式:</label><br>
<input type="radio" id="visa" name="payment" value="Visa">
<label for="visa">Visa</label><br>
<input type="radio" id="mastercard" name="payment" value="MasterCard">
<label for="mastercard">MasterCard</label><br>
<input type="radio" id="discover" name="payment" value="Discover">
<label for="discover">Discover</label><br><br>
<input type="submit" value="提交订单">
</form>
```
以下是计算订单总金额的 PHP 代码:
```
<?php
// 获取表单提交的数据
$username = $_POST['username'];
$bulbs = $_POST['bulbs'];
$payment = $_POST['payment'];
// 计算总金额
$subtotal = 0;
foreach ($bulbs as $bulb) {
preg_match('/\d+\.\d+/', $bulb, $matches); // 从字符串中提取价格
$price = floatval($matches[0]);
$subtotal += $price;
}
$tax = $subtotal * 0.062;
$total = $subtotal + $tax;
// 输出表格
echo "<table>";
echo "<tr><th>商品</th><th>金额</th></tr>";
foreach ($bulbs as $bulb) {
echo "<tr><td>" . $bulb . "</td><td>$" . number_format(floatval(preg_replace('/[^0-9\.]/', '', $bulb)), 2) . "</td></tr>";
}
echo "<tr><td>小计</td><td>$" . number_format($subtotal, 2) . "</td></tr>";
echo "<tr><td>税</td><td>$" . number_format($tax, 2) . "</td></tr>";
echo "<tr><td>总计</td><td>$" . number_format($total, 2) . "</td></tr>";
echo "</table>";
?>
```
请注意,此代码仅为演示目的,不适用于生产环境。在实际场景中,需要进行更严格的输入验证和安全措施。
阅读全文