본문 바로가기

c#(유데미)

c#:do while 구문/loops/평균점수값 구하기

 

 

이름 수 20자 넘으면 그만쓰게 하기=>쓴 이름들 wholeText로 붙이기

using System;

namespace Hello_World
{
    class Program
    {
        static void Main(string[] args)
        {
            int lenghtOfText = 0;
            string wholeText = "";
            do
            {
                Console.WriteLine("Please enter the name of a friend");
                string nameOfAFriend = Console.ReadLine();
                int currentLenght = nameOfAFriend.Length;
                lenghtOfText += currentLenght;
                wholeText += nameOfAFriend;

            } while (lenghtOfText < 20);
            Console.WriteLine("Thanks, that was enough!" + wholeText);

            Console.Read();
        }
    }
}

 

 

 

 

 

Enter키만 누르면 계속 while문 작동하면서 카운트하고, 아무 글 넣으면 카운팅 끝내게 하기

using System;

namespace Hello_World
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            string enteredText = "";
            while (enteredText.Equals(""))
            {
                Console.WriteLine("Please press enter to increase amount by one and anything else" +
                    " + enter if you want to finish counting");
                enteredText = Console.ReadLine();
                i++;
                Console.WriteLine("Current People count is {0}", i);
            }
            Console.WriteLine("{0} people are inside the bus. Press enter to clase the program", i);
            Console.Read();
        }
    }
}

enteredText.Equals("")는 사용자가 콘솔에 입력한 텍스트(enteredText)가 빈 문자열("")인지 아닌지를 검사하는 역할을 한다.

빈값""이면 계속 저 while문 돌리라는 뜻

 

 

 

 

using System;

namespace Hello_World
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "0";
            int count = 0;
            int total = 0;
            int currentNumber = 0;

            while(input != "-1")
            {
                Console.WriteLine("Last number was {0}", currentNumber);
                Console.WriteLine("Please enter the next score");
                Console.WriteLine("Current amount of entries {0}", count);
                Console.WriteLine("Please enter -1 once you are ready to calculate the average");

                input = Console.ReadLine();
                if (input.Equals("-1"))
                {
                    Console.WriteLine("----------------------------");
                    double average = (double)total / (double)count;
                    Console.WriteLine("The average score of your students is {0}", average);
                    ////Calculate average and let the teacher know
                }
                if(int .TryParse(input, out currentNumber) && currentNumber > 0 && currentNumber < 21)
                {
                    total += currentNumber;
                }
                else
                {
                    if (!(input.Equals("-1")))
                    {
                        Console.WriteLine("Please enter a value between 1 and 20!");
                    }
                    continue;
                }
                count++;
             }

            Console.ReadLine();
        }
    }
}