隨機產生20個學生的成績6
① [vb]隨機產生20個學生的成績,統計各分數段人數。
Option Explicit
Dim d(1 To 20) As Integer
Private Sub Command1_Click()
Dim i As Integer
Picture1.Cls
For i = 1 To 20
d(i) = Int(Rnd() * 100) + 1
Picture1.Print Format(d(i), " 0");
If i Mod 4 = 0 Then Picture1.Print
Next i
End Sub
Private Sub Command2_Click()
Dim b(5) As Integer
Dim i As Integer
Picture2.Cls
For i = 1 To 20
Select Case d(i)
Case 0 To 59
b(1) = b(1) + 1
Case 60 To 69
b(2) = b(2) + 1
Case 70 To 79
b(3) = b(3) + 1
Case 80 To 89
b(4) = b(4) + 1
Case 90 To 100
b(5) = b(5) + 1
End Select
Next
Picture2.Print "0~:"; b(1); "個"
Picture2.Print "60~79:"; b(2); "個"
Picture2.Print "70~89:"; b(3); "個"
Picture2.Print "80~99:"; b(4); "個"
Picture2.Print "90~100:"; b(5); "個"
End Sub
Private Sub Form_Load()
Command1.Caption = "產生並顯示數據"
Command2.Caption = "統計分數段人數"
End Sub
② VB設計:隨機產生20個學生的成績,並顯示.統計各分數段人數,即50~59, 60~69, 70~79,80-89,90-100。
Option Explicit
Private Sub showScore()
Dim score(1 To 20) As Integer
Dim an(1 To 5) As Integer
Dim i As Integer
Randomize
For i = 1 To UBound(score)
score(i) = CInt(Rnd * 100)
Select Case score(i)
Case 0 To 59
an(1) = an(1) + 1
Case 60 To 69
an(2) = an(2) + 1
Case 70 To 79
an(3) = an(3) + 1
Case 80 To 89
an(4) = an(4) + 1
Case 90 To 100
an(5) = an(5) + 1
End Select
Next
For i = 1 To 5
Select Case i
Case 1
Print "0 - 59 :",
Case 2
Print "60 - 69:",
Case 3
Print "70 - 79:",
Case 4
Print "80 - 89:",
Case 5
Print "90 - 100:",
End Select
Print CStr(an(i))
Next
End Sub
Private Sub Form_Click()
Cls
showScore
End Sub
Private Sub Form_Load()
Me.AutoRedraw = True
showScore
End Sub
③ java題,用類方法隨機生成20名學生成績成績並計算平均分和總分,按總分排序
④ vb編程:隨機產生20個學生課程分數並顯示,統計各分數段人數,顯示統計結果
Private Sub Command1_Click()
Dim a(0 To 4) As Integer, i As Integer, iScore As Integer
Randomize
For i = 1 To 20
iScore = Rnd * 100
If i Mod 4 = 0 Then Print '每4個一行
Print "學生 " & i & " 成績: " & iScore,
If iScore >= 90 Then
a(4) = a(4) + 1
ElseIf iScore >= 80 Then
a(3) = a(3) + 1
ElseIf iScore >= 70 Then
a(2) = a(2) + 1
ElseIf iScore >= 60 Then
a(1) = a(1) + 1
Else
a(0) = a(0) + 1
End If
Next i
Print '換行
Print "0-59共有: " & a(0) & " 人"
Print "60-69共有: " & a(1) & " 人"
Print "70-79共有: " & a(2) & " 人"
Print "80-89共有: " & a(3) & " 人"
Print "90-100共有: " & a(4) & " 人"
End Sub
⑤ 編一VB應用程序。要求如下: ⑴ 隨機產生20個學生的VB程序設計課程的成績,存入一維數組中,並顯示結果;
這道題的完整答案:所需控制項:一個text1,一個command1按鈕
其屬性設置:text1的multiline屬性設為true(即可以接受多行)
command1是數組控制項(共四個,見上)。
Option Base 1
Dim x() As Integer
Dim i, j, k, p, t, y, z, s As Integer
Dim a, b, c, d, e As Double
Private Sub Command1_Click(Index As Integer)
ReDim Preserve x(21) '重新定義一個有21個元素的一維數組x
k = UBound(x) - 1 '把一維數組x的下標上界減1剩下就是20個元素(學生)了
Randomize '隨機數的初始化
Select Case Index
Case 0
Text1 = "隨機產生" & Trim(Str(Val(k))) & "個學生的成績:"
Text1 = Text1 & vbNewLine
For i = 1 To k
x(i) = Int(Rnd * 100)
Text1.Text = Text1.Text & x(i) & " "
Next i
Text1 = Text1 & vbNewLine
Text1 = Text1 & vbNewLine
Case 1
Text1 = Text1 & "20個學生中最高分的是:"
y = x(1) '假設第一學生的成績最高
For i = 1 To k
If y < x(i) Then '把第一個學生的成績與其餘的19學生兩兩對比找出比第一個學生的成績要高的學生
y = x(i) '如果找到要高的就互換
End If
Next i
Text1 = Text1 & Str(Val(y)) & "分"
Text1 = Text1 & vbNewLine
Text1 = Text1 & "20個學生中最低分的是:"
y = x(1) '同樣的思維,假設第一個學生的成績最低
For i = 1 To k
If y > x(i) Then '把第一個學生的成績與其餘的19學生兩兩對比找出比第一個學生的成績要低的學生
y = x(i) '如果找到要低的就互換
End If
Next i
Text1 = Text1 & Str(Val(y)) & "分"
Text1 = Text1 & vbNewLine
Text1 = Text1 & "20個學生中的平均分數是:"
j = 0
For i = 1 To k
j = j + x(i) '先求20個學生的總分
Next i
s = j / (k) '總分數除於人數得平均數
Text1 = Text1 & Str(Val(s)) & "分"
Text1 = Text1 & vbNewLine
Text1 = Text1 & vbNewLine
Case 2
For i = 1 To k
y = x(i) '將所有同學們的分數逐一賦給變數y
Select Case y '在所有分數中查找符合條件的分數並進行計算人數
Case 0 To 59
e = e + 1
Case 60 To 69
d = d + 1
Case 70 To 79
c = c + 1
Case 80 To 89
b = b + 1
Case 90 To 100
a = a + 1
End Select
Next i
Text1 = Text1 & "0~59分的人有:"
Text1 = Text1 & Str(Val(e)) & "個人"
Text1 = Text1 & vbNewLine
Text1 = Text1 & "60~69分的人有:"
Text1 = Text1 & Str(Val(d)) & "個人"
Text1 = Text1 & vbNewLine
Text1 = Text1 & "70~79分的人有:"
Text1 = Text1 & Str(Val(c)) & "個人"
Text1 = Text1 & vbNewLine
Text1 = Text1 & "80~89分的人有:"
Text1 = Text1 & Str(Val(b)) & "個人"
Text1 = Text1 & vbNewLine
Text1 = Text1 & "90~100分的人有:"
Text1 = Text1 & Str(Val(a)) & "個人"
Text1 = Text1 & vbNewLine
Text1 = Text1 & vbNewLine
Case 3
For i = 1 To 19
For j = i + 1 To 20
If x(i) < x(j) Then
t = x(i)
x(i) = x(j)
x(j) = t
End If
Next j
Next i
Text1 = Text1 & "成績由高到低;"
Text1 = Text1 & vbNewLine
For i = 1 To 20
Text1 = Text1 & x(i) & " "
Next i
Text1 = Text1 & vbNewLine
Text1 = Text1 & vbNewLine
End Select
End Sub
⑥ VB程序,隨機產生20個學生的成績,並顯示.統計各分數段人數,即0~59,60~69,70~79,8
Private Sub Command1_Click()
Dim a(20) As Integer
Cls
c1 = 0: c2 = 0: c3 = 0: c4 = 0: c5 = 0: c6 = 0
Randomize
Print "20個成績:"
For i = 1 To 20
a(i) = Int(Rnd * 101)
Print a(i);
If i Mod 10 = 0 Then Print
Next i
For i = 1 To 20
If a(i) = 100 Then
c1 = c1 + 1
Else
If a(i) > 89 Then
c2 = c2 + 1
Else
If a(i) > 79 Then
c3 = c3 + 1
Else
If a(i) > 69 Then
c4 = c4 + 1
Else
If a(i) > 59 Then
c5 = c5 + 1
Else
c6 = c6 + 1
End If
End If
End If
End If
End If
Next i
Print: Print "統計結果:"
Print " 100分:"; c1
Print "90~99分:"; c2
Print "80~89分:"; c3
Print "70~79分:"; c4
Print "60~69分:"; c5
Print " 0~59分:"; c6
End Sub
或者換一種寫法(運行效果完全一樣):
Private Sub Command1_Click()
Dim a(20) As Integer
Dim cj(5 To 10) As Integer
Cls
Randomize
Print "20個成績:"
For i = 1 To 20
a(i) = Int(Rnd * 101)
Print a(i);
t = a(i) 10
If t < 5 Then t = 5
cj(t) = cj(t) + 1
If i Mod 10 = 0 Then Print
Next i
Print: Print "統計結果:"
Print " 100分:"; cj(10)
Print "90~99分:"; cj(9)
Print "80~89分:"; cj(8)
Print "70~79分:"; cj(7)
Print "60~69分:"; cj(6)
Print " 0~59分:"; cj(5)
End Sub
⑦ 隨機產生20個學生的成績(整數),統計各分數段人數。即0~59、60~69、70~79、80~89、90~100,
Dim sj(19), i As Integer
Private Sub command1_Click()
Dim sj(19), i As Integer
For i = 0 To 19
sj(i) = Int(Rnd * 101)
Picture1.Print sj(i);
If (i + 1) Mod 4 = 0 Then Picture1.Print
Next i
End Sub
Private Sub Command2_Click()
Dim a, b, c, d, e As Integer, s(9), sj(19), i As Integer
For i = 0 To 19
sj(i) = Int(Rnd * 101)
Picture1.Print "";
If (i + 1) Mod 4 = 0 Then Print ""
Next i
a = 0
b = 0
c = 0
d = 0
e = 0
For i = 0 To 19
If sj(i) <= 59 Then a = a + 1
Next i
For i = 0 To 19
If sj(i) >= 60 And sj(i) <= 69 Then b = b + 1
Next i
For i = 0 To 19
If sj(i) >= 70 And sj(i) <= 79 Then c = c + 1
Next i
For i = 0 To 19
If sj(i) >= 80 And sj(i) <= 89 Then d = d + 1
Next i
For i = 0 To 19
If sj(i) >= 90 And sj(i) <= 100 Then e = e + 1
Next i
Picture2.Print "s(5)"; a; Chr(13);
Picture2.Print "s(6); b; ; Chr(13);
Picture2.Print "s(7); c; "??"; Chr(13); "s(8)"; d; ; Chr(13); "s(9)"; e;
End Sub
⑧ 編程 隨機生成20個學生的成績(60分—100分)(用數組存放數據) 輸出最高分和最低分,平均分 利用數組
#include<stdio.h>
main()
{ double a[20],s=0.0;
int i ,j ,t;
for(i=0;i<20;i++)
{ scanf(a[i]);
s=s+a[i];
}
priintf(''平均分是%f '',s/20);
for(i=0;i<19;i++)
for(j=i+1;j<20;j++)
{ if(a[i]>a[j])
{ t=a[i];
a[i]=a[j];
a[j]=t;
}
}
printf("最大值為專%f,最小屬值為%f",a[19],a[0]);
}
⑨ java編程實現隨機生成20個百分製成績並輸出,統計出平均分並輸出前五名同學的成績
1、可以使用lambda來實現
int[] ints = new Random().ints(1, 100).distinct().limit(20).sorted().toArray();
//--統計信息
LongSummaryStatistics stats = Arrays.stream(ints)
.mapToLong((x) -> x)
.summaryStatistics();
System.out.println(stats);
//--輸出
IntStream.range(0, 5)
.forEach(i -> System.out.println(ints[i]));//前五版權
IntStream.range(0, ints.length)
.forEach(i -> System.out.println(ints[i]));//所有