我正在学习一些ASP并完成一些练习。
我有一个任务,我要有5个TextBox和一个提交按钮。
单击提交按钮时,网页将返回最大值。
例如:
该脚本将返回: “最大值是:88”
到目前为止,这是我的代码:
<%@ Page Language="VB" %>
<!DOCTYPE html>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
submit.Text = "Submit"
returnLabel.Text = ""
End Sub
Protected Sub submit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'Not sure what to place here
End Sub
</script>
<html>
<head id="Head1" runat="server">
<title>Exercise 7</title>
</head>
<body>
<form id="webform" runat="server">
<div>
<p><asp:TextBox ID="number1" runat="server"></asp:TextBox></p>
<p><asp:TextBox ID="number2" runat="server"></asp:TextBox></p>
<p><asp:TextBox ID="number3" runat="server"></asp:TextBox></p>
<p><asp:TextBox ID="number4" runat="server"></asp:TextBox></p>
<p><asp:TextBox ID="number5" runat="server"></asp:TextBox></p>
<p><asp:Button ID="submit" runat="server"></asp:Button></p>
<p><asp:Label ID="returnLabel" runat="server"></asp:Label></p>
</div>
</form>
</body>
</html>
我不知道如何编写一个可以检测5个值之间最高值的脚本。
谢谢,
彼得
答案 0 :(得分:1)
假设它们都包含数字。您可以检查每个文本框并逐个找出其中最大的文本框。
首先获取第一个数字,假设这是最大的数字 然后将该数字与第二个数字进行比较,如果它更大,那么我们有一个新的最大数字 像这样继续,直到比较所有数字。
Dim largestValue As Integer
largestValue = Int32.Parse(number1.Text)
If Int32.Parse(number2.Text) > largestValue Then
largestValue = Int32.Parse(number2.Text)
End If
If Int32.Parse(number3.Text) > largestValue Then
largestValue = Int32.Parse(number3.Text)
End If
If Int32.Parse(number4.Text) > largestValue Then
largestValue = Int32.Parse(number4.Text)
End If
If Int32.Parse(number5.Text) > largestValue Then
largestValue = Int32.Parse(number5.Text)
End If
答案 1 :(得分:0)
检查一下:
<%@ Page Language="VB" %>
<!DOCTYPE html>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
submit.Text = "Submit"
returnLabel.Text = ""
End Sub
Protected Sub submit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim i As Integer
i=number1.text
If (i > number2.Text) Then
If (i > number3.Text) Then
If (i > number4.Text) Then
If (i > number5.Text) Then
returnLabel.text="The largest value is: " & i
End If
End If
End If
Else
If ( number2.Text > number3.Text) Then
If ( number2.Text> number4.Text) Then
If ( number2.Text > number5.Text) Then
returnLabel.text="The largest value is: " & number2.Text
End If
End If
Else
If ( number3.Text > number4.Text) Then
If ( number3.Text > number5.Text) Then
returnLabel.text="The largest value is: " & number3.Text
End If
else
If ( number4.Text > number5.Text) Then
returnLabel.text="The largest value is: " & number4.Text
Else
returnLabel.text="The largest value is: " & number5.Text
End If
End If
End If
End If
End Sub
</script>
<html>
<head id="Head1" runat="server">
<title>Exercise 7</title>
</head>
<body>
<form id="webform" runat="server">
<div>
<p><asp:TextBox ID="number1" runat="server"></asp:TextBox></p>
<p><asp:TextBox ID="number2" runat="server"></asp:TextBox></p>
<p><asp:TextBox ID="number3" runat="server"></asp:TextBox></p>
<p><asp:TextBox ID="number4" runat="server"></asp:TextBox></p>
<p><asp:TextBox ID="number5" runat="server"></asp:TextBox></p>
<p><asp:Button ID="submit" runat="server"></asp:Button></p>
<p><asp:Label ID="returnLabel" runat="server"></asp:Label></p>
</div>
</form>
</body>
</html>
答案 2 :(得分:0)
当您将所需的文本框写为数组时,另一种方式更灵活:
int maxValue = (new TextBox[] { TextBox1, TextBox2, TextBox3, TextBox4, TextBox5 }).
ToList().ConvertAll(t => Int32.TryParse(t.Text, out curValue) ? new int?(curValue) : (int?)null).
Max(m => (!m.HasValue) ? 0 : m.Value);