1 / 20

情報基礎 II ( 第 10 回)

情報基礎 II ( 第 10 回). 月 曜4限 担当 :北川 晃. 例題:数字列の表示.  フォームに 0 ~ 9 のボタンを配置し,これを押した順番に 数字がテキストボックスに表示されるプログラムを作れ. ただし, AC キーを押すと,表示が消えるものとする.. 数字列の表示:考え方. 文字列 ”s” を用いて数字を表示する. 数字 ボタン を押すたびに,新たな文字列が “s” の右側に付け加えられるようプログラムする. 文字列は, ”&” もしくは ”+” の演算子で結合できる. “AC” ( =All Clear )キーを押した場合,それまで

Download Presentation

情報基礎 II ( 第 10 回)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 情報基礎II(第10回) 月曜4限 担当:北川 晃

  2. 例題:数字列の表示  フォームに0~9のボタンを配置し,これを押した順番に 数字がテキストボックスに表示されるプログラムを作れ. ただし,ACキーを押すと,表示が消えるものとする.

  3. 数字列の表示:考え方 • 文字列”s”を用いて数字を表示する. • 数字ボタンを押すたびに,新たな文字列が • “s”の右側に付け加えられるようプログラムする. • 文字列は,”&”もしくは”+”の演算子で結合できる. • “AC”(=All Clear)キーを押した場合,それまで • 記憶していた文字列”s”は消去される. • “AC”キーを押すと,テキストボックスの表示も消去される.

  4. 数字列の表示:プログラム例 Textbox1,Button1~11の貼り付け たとえば”5”のキーを押した場合, それまでの文字列をsとして, s = s & ”5”のように文字列を結合, 新たなsをテキストボックスに書き出す “AC”キーを押すと,s = “”に初期化され, テキストボックスの表示も””に初期化

  5. 数字列の表示:プログラム例(つづき) sを共通の変数として宣言 初期値は文字列無し Dim s As String = "" Private Sub Button1_Click(sender As Object, e As EventArgs) … s = s & "1" TextBox1.Text = s End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) … s = s & "2" TextBox1.Text = s End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) … s = s & "3" TextBox1.Text = s End Sub … 1のキーが押された場合,sの右側に 文字列”1”を結合し,文字列を表示 2のキーが押された場合,sの右側に 文字列”2”を結合し,文字列を表示 3のキーが押された場合,sの右側に 文字列”3”を結合し,文字列を表示

  6. 数字列の表示:プログラム例(つづき) … Private Sub Button9_Click(sender As Object, e As EventArgs) … s = s & "9" TextBox1.Text = s End Sub Private Sub Button10_Click(sender As Object, e As EventArgs) … s = s & "0" TextBox1.Text = s End Sub Private Sub Button11_Click(sender As Object, e As EventArgs) … s = "" TextBox1.Text = "" End Sub 9のキーが押された場合,sの右側に 文字列”9”を結合し,文字列を表示 0のキーが押された場合,sの右側に 文字列”0”を結合し,文字列を表示 ACキーが押された場合,sを空に設定し, 表示も空にする.

  7. 数字列の表示:応用 先の例題のプログラムを応用すれば, 簡単な電卓を作成することもできる. 製品として販売されている電卓を研究し, どのような動作をするか考えてみよう

  8. 例題:最大値と最小値 3つの数を入力し,最大値と最小値を 表示するプログラムを作れ.

  9. 最大値と最小値:プログラム例 Label1~3,Textbox1~5,Button1,2の貼り付け • 三つの数字a, b, cについて, • 最初にmax=min=aを代入 • b, cをmax,minの値と比較し, •   必要であれば書き換える

  10. 最大値と最小値:プログラム例(つづき) Dim a, b, c, max, min As Single a = Val(TextBox1.Text) b = Val(TextBox2.Text) c = Val(TextBox3.Text) max = a : min = a If b > max Then max = b If b < min Then min = b If c > max Then max = c If c < min Then min = c TextBox4.Text = max TextBox5.Text = min 計算ボタン の動作

  11. 例題:得点の評価 • 100点満点の得点を入力し, • 0点以上30点未満なら • 30点以上60点未満なら • 60点以上70点未満なら • 70点以上90点未満なら • 90点以上なら • と表示するプログラムを作れ. Terrible!! Bad! Good Great! Excellent!!

  12. 得点の評価:プログラム例 Label1,Textbox1,2,Button1の貼り付け

  13. 得点の評価:プログラム例(つづき) Dim point As Integer point = Val(TextBox1.Text) If point >= 90 Then TextBox2.BackColor = Color.Blue TextBox2.ForeColor = Color.White TextBox2.Text = "Excellent!!" ElseIfpoint >= 70 Then TextBox2.BackColor = Color.Green TextBox2.ForeColor = Color.White TextBox2.Text = "Great!" ElseIfpoint >= 60 Then TextBox2.BackColor = Color.GreenYellow TextBox2.ForeColor = Color.Black TextBox2.Text = "Good" 評価ボタン の動作

  14. 得点の評価:プログラム例(つづき) ElseIfpoint >= 30 Then TextBox2.BackColor = Color.Yellow TextBox2.ForeColor = Color.Black TextBox2.Text = "Bad!" Else TextBox2.BackColor = Color.Red TextBox2.ForeColor = Color.Black TextBox2.Text = "Terrible!!" End If 評価ボタン の動作

  15. 例題:円,球の評価  半径rの値を入力し,以下の計算のいずれかを 選択した後,結果を表示するプログラムを作れ. • 円の円周=2πr • 球の表面積=4πr2 • 円の面積=πr2 • 球の体積=4πr3/3 RadioButton GroupBox

  16. 例題:円,球の評価 Label1,2,Textbox1,2,Button1の貼り付け RadioButton1~4の貼り付け GroupBoxで囲む

  17. 円,球の評価:プログラム例(つづき) 計算ボタン の動作 Dim r, p, result r = Val(TextBox1.Text) p = Math.PI If RadioButton1.Checked Then result = 2 * p * r If RadioButton2.Checked Then result = p * r ^ 2 If RadioButton3.Checked Then result = 4 * p * r ^ 2 If RadioButton4.Checked Then result = 4 / 3 * p * r ^ 3 TextBox2.Text = Format(result, "#,###.#0") πの数値を与える 「各RadioButtonが チェックされている」 が真か偽か?

  18. 例題:金額の計算  希望する商品すべてをクリックし,おのおのの個数を入力 したとき,合計金額を計算して表示するプログラムを作れ. CheckBox

  19. 金額の計算:プログラム例 Label1~3,Textbox1~7,Button1,2の貼り付け CheckBox1~6の貼り付け

  20. 金額の計算:プログラム例(つづき) Private Sub Button1_Click(sender As Object, e As EventArgs) … Dim result As Integer = 0 If CheckBox1.Checked Then _ result = result + 150 * Val(TextBox1.Text) If CheckBox2.Checked Then _ result = result + 200 * Val(TextBox2.Text) … If CheckBox6.Checked Then _ result = result + 150 * Val(TextBox6.Text) TextBox7.Text = Format(result, "\\###,###") End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) … CheckBox1.Checked = False : TextBox1.Text = "" CheckBox2.Checked = False : TextBox2.Text = "" … CheckBox6.Checked = False : TextBox6.Text = "" End Sub 各チェックボックスが真か偽か 金額の表示の書式 各チェックボックスのチェックを外す

More Related