vb任意输入3个整数,按照由小到大排序输出。 (VB小编程)输入三个数,按照从大到小的顺序排序输出

vb\u7f16\u7a0b,\u4efb\u610f\u8f93\u51653\u4e2a\u6574\u6570,\u6309\u7167\u7531\u5c0f\u5230\u5927\u6392\u5e8f\u8f93\u51fa.\uff08\u4ee3\u7801\u7ea0\u9519\uff09

\u5982\u679c\u4f60\u7684\u4ee3\u7801\u6ca1\u6709\u5f3a\u5236\u58f0\u660e\u53d8\u91cf\uff0c\u90a3\u4e48\uff0c\u53bb\u6389
Dim a, b, c As Integer

\u5982\u679c\u6709\uff0c\u6539\u6210
Dim a, b, c As Integer
Dim d As String
a = CInt(Text1.Text)
b = CInt(Text2.Text)
c = CInt(Text3.Text)

\u731c\u4f60\u662f\u60f3\u663e\u793a\u6392\u5e8f\u7684\u7ed3\u679c
d="c,b,a"
\u5e94\u8be5\u6539\u6210
d=c1 & "," & b1 & "," & a1

\u53e6\u5916\uff0c\u6700\u540e\u4e00\u884c
d = Text4.Text

\u5e94\u8be5\u6539\u4e3a
Text4.Text=d


\u8fd8\u6709\uff0c\u4e0d\u7528\u8fd9\u4e48\u9ebb\u70e6
\u53ef\u7528
a1=workshrrtfunction.max(a,b,c)
c1=worksheetfunction.min(a,b,c)
b1=a+b+c-a1-c1
d=c1 & "," & b1 & "," & a1

Private Sub Form_Load()
Dim x As Integer, y As Integer, z As Integer
Dim diyige As Integer
Dim dierge As String, disange As String, disige As String
diyige = InputBox("\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u6570\u636e:", "\u6570\u636e\u8f93\u5165\u7a97\u53e3")
dierge = InputBox("\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u6570\u636e", "\u6570\u636e\u8f93\u5165\u7a97\u53e3")
disange = InputBox("\u8bf7\u8f93\u5165\u7b2c\u4e09\u4e2a\u6570\u636e", "\u6570\u636e\u8f93\u5165\u7a97\u53e3")
x = Val(diyige)
y = Val(dierge)
z = Val(disange)
If x > y And y > z Then
Print z, y, x
ElseIf y > z And z > x Then
Print x, z, y
ElseIf z > x And x > y Then
Print y, x, z
End If
Print "\u4f60\u8f93\u5165\u7684\u6570\u636e\u6309\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u662f:"
End Sub

用户的这一行 If a > b > c Then是错误的,任何一门语言都是按照运算符优先级从左至右依次计算得出结果的,显然两个>运算符优先级是相同的,所以从左至右依次算。

可以参考以下代码:

Dim a As Single, b As Single, c As Single

a = Text1.Text

b = Text2.Text

c = Text3.Text

'无外乎就六种情况

'a>b>c

'a>c>b

'b>a>c

'b>c>a

'c>a>b

'c>b>a

If a > b And b > c Then 'a比b大,且b比c大

Text1.Text = c

Text2.Text = b

Text3.Text = a

ElseIf a > c And c > b Then 'a比c大,且c比b大

Text1.Text = b

Text2.Text = c

Text3.Text = a

'下面可以参照以上函数

End If

扩展资料:

VB常用函数

1、Time 

返回系统时钟的当前时间

2、Date 

返回系统时钟的当前日期

3、Now 

返回代表当前日期和时间的编码值,该函数经常用作其它系统时钟函数的参数

4、Hour(Time) 

返回指定时间的小时部分(0到23)

5、Minute(Time) 

返回指定时间的分钟部分(0到59)

6、Second(Time) 

返回指定时间的秒部分(0到59)

7、Day(Date) 

返回月份中的日期数(1到31)

8、Month(Date) 

返回日期中的月份(1到12)

9、Year(Date) 

返回日期中的年度

10、Weekday(Date) 

返回日期中代表一周中的星期几

参考资料来源:百度百科-Visual Basic



解释一下为什么错了吧!

看这一行 If a > b > c Then。

任何一门语言都是按照运算符优先级从左至右依次计算得出结果的。

显然两个 > 运算符优先级是相同的,所以从左至右依次算。

先算 a > b,如果 a 真的比 b 大,那么得出结果 True;否则得出 False。

再算 True/False > c,一个是布尔值,一个是单精度浮点,当然不能比较。

就好像算 3 + 4 + 5,是先算 3 + 4 = 7,再算 7 + 5 = 12 一样,计算机很笨的,只能依次运算。

 

Dim a As Single, b As Single, c As Single
a = Text1.Text
b = Text2.Text
c = Text3.Text
 
'无外乎就六种情况
'a>b>c
'a>c>b
'b>a>c
'b>c>a
'c>a>b
'c>b>a
 
If a > b And b > c Then 'a比b大,且b比c大
   Text1.Text = c
   Text2.Text = b
   Text3.Text = a
ElseIf a > c And c > b Then 'a比c大,且c比b大
   Text1.Text = b
   Text2.Text = c
   Text3.Text = a
'下面类似,不写了
End If

 

P.S. 楼下的利用了中间变量 t,通过冒泡排序来进行的运算,也很简单。



任何程序设计语言都没有类似 a > b > c 这样的表达式的,必须拆分为两部分: a > b And b > c


你的代码可以简化为:

Private Sub Command1_Click()
Dim a As Single, b As Single, c As Single, k As Single
a = Text1.Text
b = Text2.Text
c = Text3.Text
If a > b Then k = a: a = b: b = k
If b > c Then k = b: b = c: c = k
If a > b Then k = a: a = b: b = k
Text1.Text = a
Text2.Text = b
Text3.Text = c
End Sub


方法1
Private Sub Command1_Click()
Dim N(2) As Long
Dim I As Long, J As Long, T As Long
N(0) = CLng(Text1.Text): N(1) = CLng(Text2.Text): N(2) = CLng(Text3.Text)
For I = 0 To 1
For J = I + 1 To 2
If N(I) > N(J) Then
T = N(I): N(I) = N(J): N(J) = T
End If
Next
Next
Text1.Text = N(0): Text2.Text = N(1): Text3.Text = N(2)
End Sub

方法2
Private Sub Command2_Click()
Dim B(2) As Object
Dim I As Long, J As Long, T As String
Set B(0) = Text1: Set B(1) = Text2: Set B(2) = Text3
For I = 0 To 1
For J = I + 1 To 2
If CLng(B(I).Text) > CLng(B(J).Text) Then
T = B(I).Text: B(I).Text = B(J).Text: B(J).Text = T
End If
Next
Next
End Sub

Private Sub Command1_Click()
Dim a As Single, b As Single, c As Single,t as single
a = val(Text1.Text)
b = Teval(xt2.Text)
c = val(Text3.Text)
If a > b Then t=b:b=a:a=t
If a > c Then t=c:c=a:a=t
If b > c Then t=c:c=b:b=t

Text1.Text = a
Text2.Text = b
Text3.Text = c
end sub

  • vb浠绘剰杈撳叆3涓暣鏁,鎸夌収鐢卞皬鍒板ぇ鎺掑簭杈撳嚭銆
    绛旓細鐢ㄦ埛鐨勮繖涓琛 If a > b > c Then鏄敊璇殑锛屼换浣曚竴闂ㄨ瑷閮芥槸鎸夌収杩愮畻绗︿紭鍏堢骇浠庡乏鑷冲彸渚濇璁$畻寰楀嚭缁撴灉鐨勶紝鏄剧劧涓や釜>杩愮畻绗︿紭鍏堢骇鏄浉鍚岀殑锛屾墍浠ヤ粠宸﹁嚦鍙充緷娆$畻銆傚彲浠ュ弬鑰冧互涓嬩唬鐮侊細Dim a As Single, b As Single, c As Single a = Text1.Text b = Text2.Text c = Text3.Text '鏃...
  • vb浠绘剰杈撳叆3涓暣鏁,鎸夌収鐢卞皬鍒板ぇ鎺掑簭杈撳嚭銆
    绛旓細Text2.Text = b Text3.Text = c end sub
  • vb缂栫▼,浠绘剰杈撳叆3涓暣鏁,鎸夌収鐢卞皬鍒板ぇ鎺掑簭杈撳嚭.(浠g爜绾犻敊)
    绛旓細a = Text1.Text 锛岃繖鏃跺VB鑷姩璁や负浣犲畾涔変簡a涓哄瓧绗︿覆鍙橀噺銆傛墍浠ワ紝褰撲綘杩涜澹版槑 Dim a, b, c As Integer 鐨勬椂鍊欙紝灏卞彂鐢熶簡鈥滃綋鍓嶈寖鍥村唴鐨勫0鏄庨噸澶嶁濓紝鍥犱负a宸茬粡闅愬紡澹版槑杩囦簡銆俠銆乧涔熶竴鏍枫傝В鍐冲姙娉曞氨鏄細灏嗗0鏄庤鍙ユ斁鍦ㄥ彉閲忎娇鐢ㄤ箣鍓嶃傛敼鎴愯繖鏍凤細Dim a, b, c As Integer a = Text1.Text ...
  • 鐢VB缂栧啓,杈撳叆涓変釜鏁存暟浣夸箣鎸夌収浠庡皬鍒板ぇ椤哄簭杈撳嚭鐨勪唬鐮
    绛旓細include <stdio.h> int main(){ int t,a,b,c;scanf("%d%d%d",&a,&b,&c);if(a
  • VB璇█缂栧啓:浠庨敭鐩樹笂杈撳叆涓変釜鏁存暟x,y,z,鐒跺悗鎶婅繖涓変釜鏁鐢卞皬鍒板ぇ杈撳嚭
    绛旓細Private Sub Command1_Click()Dim x As Integer, y As Integer, z As Integer, t As Integer x = Val(InputBox("璇杈撳叆x:"))y = Val(InputBox("璇疯緭鍏:"))z = Val(InputBox("璇疯緭鍏:"))If x > y Then t = x: x = y: y = t If x > z Then t = x: x = z: ...
  • VB绋嬪簭璁捐棰:鐢↖nputbox鍑芥暟杈撳叆涓変釜浠绘剰鏁存暟,鎸浠庡皬鍒板ぇ鐨勯『搴忚緭鍑...
    绛旓細Private Sub Command1_Click()Dim a(2), b, i, j As Double For i = 0 To 2 a(i) = InputBox("璇杈撳叆绗" & i + 1 & "涓暣鏁锛")Next i For i = 0 To 1 For j = i + 1 To 2 If a(i) > a(j) Then b = a(i)a(i) = a(j)a(j) = b End If Next j ...
  • 缂栧啓VB绋嬪簭:杈撳叆涓変釜鏁,鐒跺悗鎸変粠澶у埌灏忕殑椤哄簭鍦ㄧ獥浣撲笂鏄剧ず鍑烘潵_鐧惧害鐭 ...
    绛旓細Private Sub Command1_Click()Dim a As Integer, b As Integer, c As Integer Dim max As Integer, min As Integer a = Val(InputBox("杈撳叆绗1涓暣鏁", "Input"))b = Val(InputBox("杈撳叆绗2涓暣鏁", "Input"))c = Val(InputBox("杈撳叆绗3涓暣鏁", "Input"))max = a If b > ...
  • 鐢VB缂栧啓绋嬪簭,浣垮緱浠绘剰杈撳叆3涓暣鏁版寜澶у埌灏忕殑椤哄簭杈撳嚭銆傝兘鐢╥f宓屽...
    绛旓細褰撶劧鑳界敤If宓屽銆傚湪绐椾綋涓婃坊鍔4涓枃鏈鍜1涓寜閽紝Text1銆乀ext2鍜孴ext3杈撳叆锛Text4杈撳嚭銆侾rivate Sub Command1_Click()Dim a As Integer Dim b As Integer Dim c As Integer a = Val(Text1.Text)b = Val(Text2.Text)c = Val(Text3.Text)If a > b Then If a > c Then If b > ...
  • VB绋嬪簭璁捐棰:鐢↖nputbox鍑芥暟杈撳叆涓変釜浠绘剰鏁存暟,鎸浠庡皬鍒板ぇ鐨勯『搴忚緭鍑...
    绛旓細= c Then Print "杈撳叆鏁版嵁鏃犳硶杩愮畻锛岃閲嶆柊杈撳叆"GoToi End If If a < b Then x = b If x < c Then x = c y = b z = a End If Else x = a If b > c Then y = b z = c Else y = c z = b End If End If Print "鑷ぇ鑷冲皬鎺掑簭涓猴細"; x, y, z End Sub ...
  • vb绋嬪簭鐢╥nputbox鍑芥暟杈撳叆涓変釜浠绘剰鏁存暟,鎸変粠澶у埌灏忕殑椤哄簭杈撳嚭
    绛旓細3 <> minFT Then Lx = L3 MsgBox "浣杈撳叆鐨勬槸: " & L1 & ", " & L2 & ", " & L3 & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "浠庡ぇ鍒板皬鐨勯『搴忔槸: " & MaxI & ", " & Lx & ", " & MinI, , "杈撳嚭缁撴灉"Debug.Print MaxI, Lx, MinI End Sub ...
  • 扩展阅读:从小c到大 ... 输入3个整数给abc ... 输入xyz从小到大输出c语言 ... vb怎么判断是否是整数 ... c#输入一个整数判断奇偶 ... vb如何输入正整数 ... 从键盘上输入三个整数 ... python依次输入多个整数 ... 输入3个整数 按由小到大 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网