görsel c # ile windows programlama güz 200 9 ( 4 . hafta)

40
2006 Pearson Education, Inc. All rights rese Görsel C# ile Windows Programlama Güz 2009 (4. Hafta)

Upload: sezja

Post on 25-Jan-2016

62 views

Category:

Documents


3 download

DESCRIPTION

Görsel C # ile Windows Programlama Güz 200 9 ( 4 . Hafta). Kontrol Y apıları – Seçim Deyimleri. if Deyimi Tek seçim if…else Deyimi Çift seçim switch Deyimi Birden fazla seçim. Kontrol Y apıları – Tekrarlama Deyimleri. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

Görsel C# ile Windows Programlama

Güz 2009

(4. Hafta)

Page 2: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

2

Kontrol Yapıları – Seçim Deyimleri

• if Deyimi• Tek seçim

• if…else Deyimi• Çift seçim

• switch Deyimi• Birden fazla seçim

Page 3: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

3

• Döngüye-devam-koşulu doğru olduğu müddetçe belirli işlemleri tekrar eden yapılardır. • while Deyimi• for Deyimi• foreach Deyimi

Kontrol Yapıları – Tekrarlama Deyimleri

Page 4: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

4

1 set total to zero

2 set grade counter to one

3

4 while grade counter is less than or equal to ten

5 prompt the user to enter the next grade

6 input the next grade

7 add the grade into the total

8 add one to the grade counter

9

10 set the class average to the total divided by ten

11 print the class average

Sınıf Ortalamasını Hesaplayan Algoritma

Page 5: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

5 1 // GradeBook.cs

2 // GradeBook class that solves class-average problem using

3 // counter-controlled repetition.

4 using System;

5

6 public class GradeBook

7 {

8 private string courseName; // name of course this GradeBook represents

9

10 // constructor initializes courseName

11 public GradeBook( string name )

12 {

13 CourseName = name; // initializes courseName by using property

14 } // end constructor

15

16 // property to get and set the course name

17 public string CourseName

18 {

19 get

20 {

21 return courseName;

22 } // end get

23 set

24 {

25 courseName = value; // set should validate

26 } // end set

27 } // end property CourseName

GradeBook.cs

(1 of 3)

Page 6: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

628

29 // display a welcome message to the GradeBook user

30 public void DisplayMessage()

31 {

32 // property CourseName gets the name of the course

33 Console.WriteLine( "Welcome to the grade book for\n{0}!\n",

34 CourseName );

35 } // end method DisplayMessage

36

37 // determine class average based on 10 grades entered by user

38 public void DetermineClassAverage()

39 {

40 int total; // sum of the grades entered by user

41 int gradeCounter; // number of the grade to be entered next

42 int grade; // grade value entered by the user

43 int average; // average of the grades

44

45 // initialization phase

46 total = 0; // initialize the total

47 gradeCounter = 1; // initialize the loop counter

GradeBook.cs

(2 of 3)

Page 7: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

748

49 // processing phase

50 while ( gradeCounter <= 10 ) // loop 10 times

51 {

52 Console.Write( "Enter grade: " ); // prompt the user

53 grade = Convert.ToInt32( Console.ReadLine() ); // read grade

54 total = total + grade; // add the grade to total

55 gradeCounter = gradeCounter + 1; // increment the counter by 1

56 } // end while

57

58 // termination phase

59 average = total / 10; // integer division yields integer result

60

61 // display total and average of grades

62 Console.WriteLine( "\nTotal of all 10 grades is {0}", total );

63 Console.WriteLine( "Class average is {0}", average );

64 } // end method DetermineClassAverage

65 } // end class GradeBook

GradeBook.cs

(3 of 3)

Page 8: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

8 1 // GradeBookTest.cs

2 // Create GradeBook object and invoke its DetermineClassAverage method.

3 public class GradeBookTest

4 {

5 public static void Main( string[] args )

6 {

7 // create GradeBook object myGradeBook and

8 // pass course name to constructor

9 GradeBook myGradeBook = new GradeBook(

10 "CS101 Introduction to C# Programming" );

11

12 myGradeBook.DisplayMessage(); // display welcome message

13 myGradeBook.DetermineClassAverage(); // find average of 10 grades

14 } // end Main

15 } // end class GradeBookTest

Welcome to the grade book for CS101 Introduction to C# Programming! Enter grade: 88 Enter grade: 79 Enter grade: 95 Enter grade: 100 Enter grade: 48 Enter grade: 88 Enter grade: 92 Enter grade: 83 Enter grade: 90 Enter grade: 85

Total of all 10 grades is 848 Class average is 84

GradeBookTest.cs

int veri tipli bir değişken içinde saklanan ortalama !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Page 9: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

9

1 initialize total to zero

2 initialize counter to zero

3

4 prompt the user to enter the first grade

5 input the first grade (possibly the sentinel)

6

7 while the user has not yet entered the sentinel

8 add this grade into the running total

9 add one to the grade counter

10 prompt the user to enter the next grade

11 input the next grade (possibly the sentinel)

12

13 if the counter is not equal to zero

14 set the average to the total divided by the counter

15 print the average

16 else 17 print “No grades were entered”

Bitiş-değer Kontrollü Döngüler (Sentinel-controlled)

Page 10: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

10 1 // GradeBook.cs

2 // GradeBook class that solves class-average problem using

3 // sentinel-controlled repetition.

4 using System;

5

6 public class GradeBook

7 {

8 private string courseName; // name of course this GradeBook represents

9

10 // constructor initializes courseName

11 public GradeBook( string name )

12 {

13 CourseName = name; // initialize courseName by using property

14 } // end constructor

15

16 // property to get and set the course name

17 public string CourseName

18 {

19 get

20 {

21 return courseName;

22 } // end get

23 set

24 {

25 courseName = value; // set should validate

26 } // end set

27 } // end property CourseName

GradeBook.cs

(1 of 3)

Page 11: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

1128

29 // display a welcome message to the GradeBook user

30 public void DisplayMessage()

31 {

32 Console.WriteLine( "Welcome to the grade book for\n{0}!\n",

33 CourseName );

34 } // end method DisplayMessage

35

36 // determine the average of an arbitrary number of grades

37 public void DetermineClassAverage()

38 {

39 int total; // sum of grades

40 int gradeCounter; // number of grades entered

41 int grade; // grade value

42 double average; // number with decimal point for average

43

44 // initialization phase

45 total = 0; // initialize total

46 gradeCounter = 0; // initialize loop counter

47

48 // processing phase

49 // prompt for input and read grade from user

50 Console.Write( "Enter grade or -1 to quit: " );

51 grade = Convert.ToInt32( Console.ReadLine() );

52

53 // loop until sentinel value read from user

54 while ( grade != -1 )

55 {

56 total = total + grade; // add grade to total

57 gradeCounter = gradeCounter + 1; // increment counter

GradeBook.cs

(2 of 3)

Page 12: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

1258

59 // prompt for input and read next grade from user

60 Console.Write( "Enter grade or -1 to quit: " );

61 grade = Convert.ToInt32( Console.ReadLine() );

62 } // end while

63

64 // termination phase

65 // if user entered at least one grade...

66 if ( gradeCounter != 0 )

67 {

68 // calculate average of all grades entered

69 average = ( double ) total / gradeCounter;

70

71 // display total and average (with two digits of precision)

72 Console.WriteLine( "\nTotal of the {0} grades entered is {1}",

73 gradeCounter, total );

74 Console.WriteLine( "Class average is {0:F2}", average );

75 } // end if

76 else // no grades were entered, so output error message

77 Console.WriteLine( "No grades were entered" );

78 } // end method DetermineClassAverage

79 } // end class GradeBook

GradeBook.cs

(3 of 3)

Calculate average grade using (double) to perform explicit conversion

Display average grade

Display “No grades were entered” message

Page 13: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

13

Ondalıklı Sayıları Formatlı Yazdırma

• double number = 3.1234;• System.Console.WriteLine(“OUTPUT: {0:F3}”, number);

• OUTPUT: 3.123

Page 14: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

14 1 // GradeBookTest.cs

2 // Create GradeBook object and invoke its DetermineClassAverage method.

3 public class GradeBookTest

4 {

5 public static void Main( string[] args )

6 {

7 // create GradeBook object myGradeBook and

8 // pass course name to constructor

9 GradeBook myGradeBook = new GradeBook(

10 "CS101 Introduction to C# Programming" );

11

12 myGradeBook.DisplayMessage(); // display welcome message

13 myGradeBook.DetermineClassAverage(); // find average of grades

14 } // end Main

15 } // end class GradeBookTest Welcome to the grade book for CS101 Introduction to C# Programming!

Enter grade or -1 to quit: 96 Enter grade or -1 to quit: 88 Enter grade or -1 to quit: 79 Enter grade or -1 to quit: -1

Total of the 3 grades entered is 263 Class average is 87.67

GradeBookTest.cs

Page 15: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

15

Bileşik Atama Deyimleri

BİLEŞİK ATAMA OPERATÖRÜ KULLANIM AÇIK FORM

+= c += 7 c = c + 7

-= d -= 4 d = d - 4 *= e *= 5 e = e * 5 /= f /= 3 f = f / 3

%= g %= 9 g = g % 9

Page 16: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

++ Artırma Operatörü (increment)

-- Azaltma Operatörü (decrement)

++a Önce artır (Pre-increment) Operatörü (a değişkenini bir artır ve a değişkeninin yeni değerini kullan.)

y=++x; ise x=x+1 ve sonra y=x

a++ Sonra artır (Post-increment) Operatörü (a değiskeninin mevcut değerini kullan ve sonra değerini bir artır.)

y=x++; ise y=x ve sonra x=x+1

--b Önce azalt (Pre-decrement) Operatörü

b-- Sonra azalt (Post-decrement) Operatörü

Artırma ve Azaltma Operatörleri

Page 17: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

a=a+1;

ya da

a+=1;

ya da

a++;

ya da

++a;

4 Değişik 1 Artırma Deyimi

Page 18: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

18 1 // Increment.cs

2 // Prefix increment and postfix increment operators.

3 using System;

4

5 public class Increment

6 {

7 public static void Main( string[] args )

8 {

9 int c;

10

11 // demonstrate postfix increment operator

12 c = 5; // assign 5 to c

13 Console.WriteLine( c ); // print 5

14 Console.WriteLine( c++ ); // print 5 again, then increment

15 Console.WriteLine( c ); // print 6

16

17 Console.WriteLine(); // skip a line

18

19 // demonstrate prefix increment operator

20 c = 5; // assign 5 to c

21 Console.WriteLine( c ); // print 5

22 Console.WriteLine( ++c ); // increment then print 6

23 Console.WriteLine( c ); // print 6 again

24 } // end Main

25 } // end class Increment

5 5 6

5 6 6

Increment.cs

Page 19: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

19

for Tekrarlama Deyimi

for (initialization; loopContinuationCondition; increment ) statement;

initialization;while (loopContinuationCondition )

{ statement; increment;}

Page 20: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

20 1 // ForCounter.cs

2 // Counter-controlled repetition with the for repetition statement.

3 using System;

4

5 public class ForCounter

6 {

7 public static void Main( string[] args )

8 {

9 // for statement header includes initialization,

10 // loop-continuation condition and increment

11 for ( int counter = 1; counter <= 10; counter++ )

12 Console.Write( "{0} ", counter );

13

14 Console.WriteLine(); // output a newline

15 } // end Main

16 } // end class ForCounter 1 2 3 4 5 6 7 8 9 10

ForCounter.cs

Control-variable name is counter

Control-variable initial value is 1

Condition tests for counter’s final value

Increment for counter

Page 21: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

21

Page 22: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

22

For Döngüsü Örnekleri

• Vary control variable from 1 to 100 in increments of 1•for ( int i = 1; i <= 100; i++ )

• Vary control variable from 100 to 1 in increments of –1•for ( int i = 100; i >= 1; i-- )

• Vary control variable from 7 to 77 in increments of 7•for ( int i = 7; i <= 77; i += 7 )

• Vary control variable from 20 to 2 in decrements of -2•for ( int i = 20; i >= 2; i -= 2 )

• Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20•for ( int i = 2; i <= 20; i += 3 )

• Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0

•for ( int i = 99; i >= 0; i -= 11 )

Page 23: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

23 1 // Sum.cs

2 // Summing integers with the for statement.

3 using System;

4

5 public class Sum

6 {

7 public static void Main( string[] args )

8 {

9 int total = 0; // initialize total

10

11 // total even integers from 2 through 20

12 for ( int number = 2; number <= 20; number += 2 )

13 total += number;

14

15 Console.WriteLine( "Sum is {0}", total ); // display results

16 } // end Main

17 } // end class Sum Sum is 110

Sum.cs

increment number by 2 each iteration

Page 24: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

24 1 // Interest.cs

2 // Compound-interest calculations with for.

3 using System;

4

5 public class Interest

6 {

7 public static void Main( string[] args )

8 {

9 decimal amount; // amount on deposit at end of each year

10 decimal principal = 1000; // initial amount before interest

11 double rate = 0.05; // interest rate

12

13 // display headers

14 Console.WriteLine( "{0}{1,20}", "Year", "Amount on deposit" );

Interest.cs

(1 of 2)

Second string is right justified and displayed with a field width of 20

Page 25: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

2515

16 // calculate amount on deposit for each of ten years

17 for ( int year = 1; year <= 10; year++ )

18 {

19 // calculate new amount for specified year

20 amount = principal *

21 ( ( decimal ) Math.Pow( 1.0 + rate, year ) );

22

23 // display the year and the amount

24 Console.WriteLine( "{0,4}{1,20:C}", year, amount );

25 } // end for

26 } // end Main

27 } // end class Interest Year Amount on deposit 1 $1,050.00

2 $1,102.50 3 $1,157.63 4 $1,215.51

5 $1,276.28 6 $1,340.10 7 $1,407.10

8 $1,477.46 9 $1,551.33 10 $1,628.89

Interest.cs

(2 of 2)

Calculate amount with for statement

amount is displayed right justified in currency format

Page 26: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

26

Çıktıyı Formatlama

• {1, 20}• Alan genişliği virgülden sonra yazılır.

• Varsayılan (default) olarak sağa dayalıdır.

• Sola dayalı yapmak için - kullanılır.

Page 27: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

• Döngü içlerinde programın akış kontrolünü değiştirmek için bu deyimleri kullanırız.

• “break” deyimi while, for veya switch yapılarının içinde kullanıldığında bu yapılar ani bir şekilde sonlandırılır.

• “continue” deyimi sadece for döngüsü içerisinde kullanıldığında bu deyimden sonra gelen komutlar atlanır ve döngü bir sonraki iterasyona başlar.

break ve continue Deyimleri

Page 28: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

28 1 // BreakTest.cs

2 // break statement exiting a for statement.

3 using System;

4

5 public class BreakTest

6 {

7 public static void Main( string[] args )

8 {

9 int count; // control variable also used after loop terminates

10

11 for ( count = 1; count <= 10; count++ ) // loop 10 times

12 {

13 if ( count == 5 ) // if count is 5,

14 break; // terminate loop

15

16 Console.Write( "{0} ", count );

17 } // end for

18

19 Console.WriteLine( "\nBroke out of loop at count = {0}", count );

20 } // end Main

21 } // end class BreakTest 1 2 3 4 Broke out of loop at count = 5

BreakTest.csLoop 10 times

Exit for statement (break) when count equals 5

Page 29: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

29 1 // ContinueTest.cs

2 // continue statement terminating an iteration of a for statement.

3 using System;

4

5 public class ContinueTest

6 {

7 public static void Main( string[] args )

8 {

9 for ( int count = 1; count <= 10; count++ ) // loop 10 times

10 {

11 if ( count == 5 ) // if count is 5,

12 continue; // skip remaining code in loop

13

14 Console.Write( "{0} ", count );

15 } // end for

16

17 Console.WriteLine( "\nUsed continue to skip printing 5" );

18 } // end Main

19 } // end class ContinueTest 1 2 3 4 6 7 8 9 10 Used continue to skip printing 5

ContinueTest.csLoop 10 times

Skip line 14 and proceed to line 9 when count equals 5

Page 30: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

30

switch Çoklu Seçim Deyimi

• Her case mutlaka bir break deyimi ile sonlanmalıdır.

• case lerden hiçbirinin işletilmediği durumları karşılamak üzere bir default durumu olmalıdır.

Page 31: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

31 1 // GradeBook.cs

2 // GradeBook class uses switch statement to count A, B, C, D and F grades.

3 using System;

4

5 public class GradeBook

6 {

7 private string courseName; // name of course this GradeBook represents

8 private int total; // sum of grades

9 private int gradeCounter; // number of grades entered

10 private int aCount; // count of A grades

11 private int bCount; // count of B grades

12 private int cCount; // count of C grades

13 private int dCount; // count of D grades

14 private int fCount; // count of F grades

15

16 // constructor initializes courseName;

17 // int instance variables are initialized to 0 by default

18 public GradeBook( string name )

19 {

20 CourseName = name; // initializes courseName

21 } // end constructor

GradeBook.cs

(1 of 5)

Variables to be incremented determined by the switch

statement

Page 32: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

3222

23 // property that gets and sets the course name

24 public string CourseName

25 {

26 get

27 {

28 return courseName;

29 } // end get

30 set

31 {

32 courseName = value;

33 } // end set

34 } // end property CourseName

35

36 // display a welcome message to the GradeBook user

37 public void DisplayMessage()

38 {

39 // CourseName gets the name of the course

40 Console.WriteLine( "Welcome to the grade book for\n{0}!\n",

41 CourseName );

42 } // end method DisplayMessage

GradeBook.cs

(2 of 5)

Page 33: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

33

GradeBook.cs

(3 of 5)

43

44 // input arbitrary number of grades from user

45 public void InputGrades()

46 {

47 int grade; // grade entered by user

48 string input; // text entered by the user

49

50 Console.WriteLine( "{0}\n{1}",

51 "Enter the integer grades in the range 0-100.",

52 "Type <Ctrl> z and press Enter to terminate input:" );

53

54 input = Console.ReadLine(); // read user input

55

56 // loop until user enters the end-of-file indicator (<Ctrl> z)

57 while ( input != null )

58 {

59 grade = Convert.ToInt32( input ); // read grade off user input

60 total += grade; // add grade to total

61 gradeCounter++; // increment number of grades

62

63 // call method to increment appropriate counter

64 IncrementLetterGradeCounter( grade );

65

66 input = Console.ReadLine(); // read user input

67 } // end while

68 } // end method InputGrades

Display prompt

Loop condition: if there is more data input

Page 34: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

34

GradeBook.cs

(4 of 5)

69

70 // add 1 to appropriate counter for specified grade

71 private void IncrementLetterGradeCounter( int grade )

72 {

73 // determine which grade was entered

74 switch ( grade / 10 )

75 {

76 case 9: // grade was in the 90s

77 case 10: // grade was 100

78 aCount++; // increment aCount

79 break; // necessary to exit switch

80 case 8: // grade was between 80 and 89

81 bCount++; // increment bCount

82 break; // exit switch

83 case 7: // grade was between 70 and 79

84 cCount++; // increment cCount

85 break; // exit switch

86 case 6: // grade was between 60 and 69

87 dCount++; // increment dCount

88 break; // exit switch

89 default: // grade was less than 60

90 fCount++; // increment fCount

91 break; // exit switch

92 } // end switch

93 } // end method IncrementLetterGradeCounter

switch statement determines which case label to execute,

depending on controlling expression

(grade / 10 ) is the controlling expression

default case for grade less than 60

Page 35: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

35

GradeBook.cs

(5 of 5)

94

95 // display a report based on the grades entered by the user

96 public void DisplayGradeReport()

97 {

98 Console.WriteLine( "\nGrade Report:" );

99

100 // if user entered at least one grade...

101 if ( gradeCounter != 0 )

102 {

103 // calculate average of all grades entered

104 double average = ( double ) total / gradeCounter;

105

106 // output summary of results

107 Console.WriteLine( "Total of the {0} grades entered is {1}",

108 gradeCounter, total );

109 Console.WriteLine( "Class average is {0:F2}", average );

110 Console.WriteLine( "{0}A: {1}\nB: {2}\nC: {3}\nD: {4}\nF: {5}",

111 "Number of students who received each grade:\n",

112 aCount, // display number of A grades

113 bCount, // display number of B grades

114 cCount, // display number of C grades

115 dCount, // display number of D grades

116 fCount ); // display number of F grades

117 } // end if

118 else // no grades were entered, so output appropriate message

119 Console.WriteLine( "No grades were entered" );

120 } // end method DisplayGradeReport

121 } // end class GradeBook

Prints out results

Page 36: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

36 1 // GradeBookTest.cs

2 // Create GradeBook object, input grades and display grade report.

3

4 public class GradeBookTest

5 {

6 public static void Main( string[] args )

7 {

8 // create GradeBook object myGradeBook and

9 // pass course name to constructor

10 GradeBook myGradeBook = new GradeBook(

11 "CS101 Introduction to C# Programming" );

12

13 myGradeBook.DisplayMessage(); // display welcome message

14 myGradeBook.InputGrades(); // read grades from user

15 myGradeBook.DisplayGradeReport(); // display report based on grades

16 } // end Main

17 } // end class GradeBookTest

GradeBookTest.cs

(1 of 2)

Call GradeBook public methods to count grades

Page 37: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

37 Welcome to the grade book for CS101 Introduction to C# Programming! Enter the integer grades in the range 0-100. Type <Ctrl> z and press Enter to terminate input:

99 92 45 100 57 63 76 14 92 ^Z

Grade Report: Total of the 9 grades entered is 638 Class average is 70.89 Number of students who received each grade: A: 4 B: 0 C: 1 D: 1 F: 3

GradeBookTest.cs

(2 of 2)

Page 38: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

public static void Main( string[ ] args )

{

while(true) //sonsuz dongu

{

……….

if(…….) //kullanici dogru tahminde bulunduysa

break;

}

}

Sonsuz Döngü

Page 39: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

public static void Main( string[ ] args )

{

bool oyunaDevam=true;

while(oyunaDevam) //sonsuz dongu

{

……….

if(…….) //kullanici dogru tahminde bulunduysa

oyunaDevam=false;

}

}

Sonsuz Döngü

Page 40: Görsel C #  ile Windows Programlama Güz  200 9 ( 4 . Hafta)

2006 Pearson Education, Inc. All rights reserved.

SINAV HAZIRLIK SORULARI

1. Kullanıcıdan klavye yoluyla aldığı bir sayının bölenlerini veya çarpanlarını bularak ekrana basan bir C# programı yazınız.

2. Kullanıcıdan klavye yoluyla aldığı bir sayının ASAL sayı olup olmadığını belirleyen ve eğer ASAL sayı ise ASAL çarpanlarını ekrana basan bir C# programı yazınız.

3. Kullanıcıdan klavye yoluyla aldığı belirli bir aralık içerisindeki bütün ASAL sayıları ve bu ASAL sayıların sayısını ekrana basab bir C# programı yazınız.

4. Kullanıcıdan klavye yoluyla aldığı iki sayının aralarında ASAL olup olmadığını bulan bir C# programı yazınız. (4 ve 15 aralarında asaldır örneğin.)

5. Kullanıcıdan klavye yoluyla aldığı 3 sayının En Büyük Ortak Bölenini (EBOB) bulacak bir C# programı yazınız.

6. Kullanıcıdan klavye yoluyla aldığı 3 sayının En Küçük Ortak Katını (EKOK) bulacak bir C# programı yazınız. (15 sayısının katları: 15 30 45 60 75 ve 20 sayısının katları: 20 40 60 tır. Dolayısıyla EKOK(15,20)=60 tır.)

7. Kullanıcıdan klavye yoluyla aldığı 2 rasyonel sayıyı toplayıp en sade haliyle ekrana basacak bir C# programı yazınız.