Lesson 6〜8・練習問題2 解答例

あくまで解答は例です。正しい結果が得られていてかつ考え方が間違っていなければ問題ありません。

Lesson 6

問7

#prompt
Dim x As Double
Input x
If x>=7 Then
    Print "7以上"
End If

実行例(1)

? 8
7以上

実行例(2)

? 5

問8

#prompt
Dim x As Double, y As Double
Input x
Input y
If x+y>=7 Then
    Print "7以上"
End If

実行例(1)

? 3
? 6
7以上

実行例(2)

? 1
? 3

問9

#prompt
Dim x As Double
Input x
If x=7 Then
    Print "成功"
Else
    Print "失敗"
End If

実行例(1)

? 7
成功

実行例(2)

? 3
失敗

Lesson 7

問10

#prompt
Dim tensu As Double
Input tensu
If tensu>100 Then
    Print "テストの点数を入力してね。"
ElseIf tensu>=80 Then
    Print "あなたはすばらしい!"
ElseIf tensu>=60 Then
    Print "もう少しだ!"
ElseIf tensu>=30 Then
    Print "努力が足りないようですね。"
ElseIf tensu>=0 Then
    Print "ちゃんと勉強してますか?"
Else
    Print "テストの点数を入力してね。"
End If

実行例(1)

? 75
もう少しだ!

実行例(2)

? -30
テストの点数を入力してね。

問11

#prompt
Dim x As Double, y As Double
Input x
Input y
Select Case x
Case 0
    Print y
Case 1
    Print y*(-2)
Case -1
    Print y*2
Case Else
    Print x*y
End Select

実行例(1)

? 3
? 5
15

実行例(2)

? -1
? 4
8

Special Lesson 1

問12(実行例は略)

#prompt
Dim x As Double, y As Double
Input x
Input y
If x>0 And y>0 Or x<=0 And y<=0 Then
    Print "実験成功"
End If

【別解】

#prompt
Dim x As Double, y As Double
Input x
Input y
If Not(x>0 Xor y>0) Then
    Print "実験成功"
End If

Lesson 8

問13

Dim i As Double
For i=1 To 10
    Print Sqr(i)
Next

実行結果

1
1.4142135623731
1.7320508756887
2
2.23606797749978
2.44948974278317
2.6457513110646
2.82842712474618
3
3.162277660168371

問14

Dim i As Double
For i=10 To 1 Step -1
    Print Sqr(i)
Next

実行結果

3.162277660168371
3
2.82842712474618
2.6457513110646
2.44948974278317
2.23606797749978
2
1.7320508756887
1.4142135623731
1

問15

#prompt
Dim password As String
While password<>"sinryow" And password<>"activebasic"
    Input "パスワード>";password
Wend
If password="sinryow" Then
    Print "Sinryowさん,こんにちは"
Else
    Print "Discoversoftさん,こんにちは"
End If

実行例(1)

パスワード>? Sinryow
パスワード>? sinryoww
パスワード>? sinryow
Sinryowさん,こんにちは

実行例(2)

パスワード>? visualbasic
パスワード>? activebasic
Discoversoftさん,こんにちは

問16(実行例は省略します。Lesson 8で出した実行例と同じくなります。)

#prompt
Dim password As String
Do
While password<>"sinryow"
    If password="sinryow" Then
        Exit Do
    End If
Loop
Print "正しいパスワードが入力されました。"

練習問題2

1.

#prompt
Dim N As Long
Input N
If N Mod 2=0 Then
    Print "偶数です"
Else
    Print "奇数です"
End If

(別解)

#prompt
Dim N As Long
Input N
If N Mod 2=1 Or N Mod 2=-1 Then
    Print "奇数です"
Else
    Print "偶数です"
End If

(実行例)

? 6
偶数です
? 9
奇数です
#prompt
Dim a As Double, b As Double, c As Double
Dim D As Double

Input "aの値は";a
Input "bの値は";b
Input "cの値は";c

D=b^2-4*a*c
If D<0 Then
    Print -b/(2*a); "±"; Sqr(-D)/(2*a); "i"
Else
    Print (-b+Sqr(D))/(2*a)
    Print (-b-Sqr(D))/(2*a)
End If

実行例

aの値は? 1
bの値は? 3
cの値は? 2
-1
-2
aの値は? 1
bの値は? 1
cの値は? 1
-0.5 ± 0.866025403784439 i

3.

#prompt
Dim n As Long, i As Long
n=1
i=1

While n<=1000000
    i=i+1
    n=n*i
Wend
Print i

(別解)

#prompt
Dim n As Long, i As Long
n=1
i=1

Do Until n>1000000
    i=i+1
    n=n*i
Loop
Print i

(別解)

#prompt
Dim n As Long, i As Long
n=1
i=1

Do
    i=i+1
    n=n*i
    If n>1000000 Then
        Print i
        Exit Do
    End If
Loop

実行結果

10

戻る