200 likes | 208 Views
סוגי מידע / משתנים. ד"ר אבי רוזנפלד. דוגמה פשוטה. Module Module1 Sub Main() Dim x As Integer x = 10 Console.WriteLine ((x + 2) / 10) Console.ReadKey () End Sub End Module. מה זה יעשה?. Module Module1 Sub Main() Dim x As Integer x = 10
E N D
סוגי מידע / משתנים ד"ר אבי רוזנפלד
דוגמה פשוטה Module Module1 Sub Main() Dim x As Integer x = 10 Console.WriteLine((x + 2) / 10) Console.ReadKey() End Sub End Module
מה זה יעשה? Module Module1 Sub Main() Dim x As Integer x = 10 Console.WriteLine(x + 2 * 10 + 1) Console.ReadKey() End Sub End Module
מה יהיה הפלט? Module Module1 Sub Main() Dim x, y, z As Integer x = 10 y = x / 3 z = x / 4 Console.WriteLine("x is " & x) Console.WriteLine("y is " & y) Console.WriteLine("z is " & z) Console.ReadKey() End Sub End Module
num count הצבה (השמה) -assignment Dim num as integer Dim count as integer =1 6 5 num = 5 num = num + count 1 מבוא לתכנות למע"ס - מאיר קומר - סמסטר
אופרטורים - + * / Mod \ מבוא לתכנות למע"ס - מאיר קומר - סמסטר
יש גם קבועים Const piAs single = 3.1415 מבוא לתכנות למנע"ס - שבוע מספר 2 - מאיר קומר - סמסטר ב' - תשס"ו
סוגים שונים של מידע (long) Module Module1 Sub Main() Dim x, y, z As Long x = 10 y = x / 3 z = x / 4 Console.WriteLine("x is " & x) Console.WriteLine("y is " & y) Console.WriteLine("z is " & z) Console.ReadKey() End Sub End Module
סוגים שונים של מידע (short) Module Module1 Sub Main() Dim x, y, z As Short x = 10 y = x / 3 z = x / 4 Console.WriteLine("x is " & x) Console.WriteLine("y is " & y) Console.WriteLine("z is " & z) Console.ReadKey() End Sub End Module
סוגים שונים של מידע (String) Module Module1 Sub Main() Dim x, y, z As String x = "Hello World" 'y = x / 3 This is in a comment 'z = x / 4 This is too Console.WriteLine("x is " & x) Console.WriteLine("y is " & y) Console.WriteLine("z is " & z) Console.ReadKey() End Sub End Module
מה זה יעשה? Module Module1 Sub Main() Dim x, y, z As String x = "Hello World" y = x + " Aviland " z = x + " what???? " + y 'This actually works! Console.WriteLine("x is " & x) Console.WriteLine("y is " & y) Console.WriteLine("z is " & z) Console.ReadKey() End Sub End Module
העולם של קלט • אתה חייב משתנה (זיכרון לאחסן את המידע) • אתה גם חייב משתנה מתאים של Console.ReadLine
דוגמא Module Module1 Sub Main() Dim x, y As String x = "Hello World" Console.WriteLine(x) y = Console.ReadLine() 'It waits until "enter" Console.WriteLine("You typed " & y) Console.ReadKey() End Sub End Module
זה לא יעבוד Module Module1 Sub Main() Dim x As Integer x = Console.Read() ' this reads as a word! 'It waits until "enter" Console.WriteLine("You typed " & x + 1) Console.ReadKey() End Sub End Module
זה כן יעבוד Module Module1 Sub Main() Dim x As Integer x = Console.ReadLine() 'this works! 'It waits until "enter" Console.WriteLine("You typed " & x + 1) Console.ReadKey() End Sub End Module
מה זה יעשה? Module Module1 Sub Main() Dim x As Integer Dim y As Decimal y = Console.ReadLine 'It waits until "enter" x = y Console.WriteLine("The whole portion was " & x) Console.WriteLine("The input was " & y) Console.WriteLine("The decimal was " & y - x) Console.ReadKey() End Sub End Module
Type Casting (and fun!) Module Module1 Sub Main() Dim thousands, hundreds, tens, ones As Integer Dim whole, stam_unused_variable As Decimal Console.WriteLine("Please enter a number greater than 1000 ") whole = Console.ReadLine 'It waits until "enter" thousands = Int(whole / 1000) whole -= thousands * 1000 hundreds = Int(whole / 100) whole -= hundreds * 100 tens = Int(whole / 10) ones = whole Mod 10 'Just for show off mod Console.WriteLine("The thousands part is " & thousands & ", the hundreds part is " & hundreds) Console.WriteLine("The tens portion is " & tens & " and the ones portion is " & ones) Console.ReadKey() End Sub End Module
פלט לקבצים Imports System.IO 'Notice the import! Module Module1 Sub Main() Dim writer As StreamWriter = _ New StreamWriter("z:\KBTest.txt") writer.WriteLine("File created using StreamWriter class2.") writer.Write("What???") writer.Close() End Sub End Module
דוגמא לקלט Imports System.IO 'Notice the import! Module Module1 Sub Main() Dim fileReader As StreamReader fileReader = _ My.Computer.FileSystem.OpenTextFileReader("KBTest.txt") Dim x As String x = fileReader.ReadLine() Console.WriteLine("The file line of the file is " & x) Console.ReadKey() End Sub End Module