2016-10-25 218 views
-1

我想寫一個程序,它首先生成三個隨機整數,然後打印出它們的正方形和立方體。隨機整數數組

enter image description here

我已經有計算和輸出,但我不知道如何生成隨機數。

Dim NumOfIntegers() As Integer = {1, 2, 3, 4, 8} 
Dim x, y As Integer 
For Each num As Integer In NumOfIntegers 
    x = num^2 
    y = num^3 
    MessageBox.Show("Square Of " & num & " = " & x & vbCrLf & "Cube Of " & num & " = " & y) 
Next 
+0

「這個答案我想隨機回答。」你的意思是你的輸入數字? –

+0

請將您的截圖作爲圖片加入,而不是鏈接。你似乎有什麼你想要的(根據截圖)。請解釋清楚你正在嘗試做什麼 – 0xDEADC0DE

+1

@ 0xDEADC0DE:如果我沒有弄錯新賬戶無法在他們的問題中發佈圖片。他們需要像10代表。或者這樣做。 –

回答

0

本規範填補了整數列表1到100

Dim NumOfIntegers As New List(Of Integer) 

While NumOfIntegers.Count < 3 
    Dim i As Integer = CInt(Math.Ceiling(Rnd() * 100)) + 1 
    If Not NumOfIntegers.Contains(i) Then NumOfIntegers.Add(i) 
End While 

NumOfIntegers.Sort() 

Dim x, y As Integer  
For Each num As Integer In NumOfIntegers 
    x = num^2 
    y = num^3 
    MessageBox.Show("Square Of " & num & " = " & x & vbCrLf & "Cube Of " & num & " = " & y) 
Next 

代碼之間的3張不同的隨機數,如果你只需要使用一個for循環:

(It's未經測試的代碼並且在3個整數內可能是重複的)

Dim NumOfIntegers() As New Integer = {(CInt(Math.Ceiling(Rnd() * 100)) + 1), (CInt(Math.Ceiling(Rnd() * 100)) + 1), (CInt(Math.Ceiling(Rnd() * 100)) + 1)} 

Dim x, y As Integer  
For Each num As Integer In NumOfIntegers 
    x = num^2 
    y = num^3 
    MessageBox.Show("Square Of " & num & " = " & x & vbCrLf & "Cube Of " & num & " = " & y) 
Next 
+0

謝謝先生,但我想要另一種方式,可能是最簡單的方法。 –

+0

你用「另一種方式」究竟是什麼意思? – FatTony

+0

我的意思是,例如,使用的,而不是同時 –