C# 0 The Complete Reference


ReadLine( )  and uses  Int32.Parse( )



Download 4,07 Mb.
Pdf ko'rish
bet508/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   504   505   506   507   508   509   510   511   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

ReadLine( )

 and uses 



Int32.Parse( )

to convert the string into an integer. Next, it inputs the values, using 



Double.Parse( )

 to convert 

the strings into their 

double

 equivalents.

// This program averages a list of numbers entered by the user.

using System;

using System.IO;

class AvgNums {

  static void Main() {

    string str;

    int n;

    double sum = 0.0;

    double avg, t;

    Console.Write("How many numbers will you enter: ");

    str = Console.ReadLine();

    try {

      n = Int32.Parse(str);

    } catch(FormatException exc) {

      Console.WriteLine(exc.Message);

      return;

    } catch(OverflowException exc) {

      Console.WriteLine(exc.Message);

      return;

    }


www.freepdf-books.com


398

 

P a r t   I :  



T h e   C #   L a n g u a g e

    Console.WriteLine("Enter " + n + " values.");

    for(int i=0; i < n ; i++)  {

      Console.Write(": ");

      str = Console.ReadLine();

      try {

        t = Double.Parse(str);

      } catch(FormatException exc) {

        Console.WriteLine(exc.Message);

        t = 0.0;

      } catch(OverflowException exc) {

        Console.WriteLine(exc.Message);

        t = 0;

      }


      sum += t;

    }


    avg = sum / n;

    Console.WriteLine("Average is " + avg);

  }

}

Here is a sample run:



How many numbers will you enter: 5

Enter 5 values.

: 1.1

: 2.2


: 3.3

: 4.4


: 5.5

Average is 3.3

One other point: You must use the right parsing method for the type of value you 

are trying to convert. For example, trying to use 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   504   505   506   507   508   509   510   511   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish