2016-10-25 15 views
-1

私は最初に3つの乱数を生成し、それらの正方形と立方体をプリントアウトするプログラムを作成したいと思います。ランダムな整数を持つ配列

enter image description here

私はすでに計算と出力を持っているが、私は乱数を生成する方法を知っているドント。あなただけのforループを使用したい場合は

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個の異なる乱数と整数のリストを埋め:

(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

私は、例えば、whileの代わりにforを使用することを意味します –

関連する問題