Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

c# 3.0 - There is a different way to write this code? c#

I am new in IT and my teacher gave us this exercise

Write a console application that asks the user to input numbers between 1-20. A user should not be able to input numbers outside of 1 and 20. To finish inputting numbers the user should type 0. You should then print to the screen the amount of times a user inputted a number greater than 10.

I wrote this code and it is working good

        int counter = 0;
        int[] num = new int[10000];
        Console.WriteLine("Please enter numbers between 1-20. To finish inputting numbers type 0");

        for(int i = 0; i < 10000; i++)
        {
            num[i] = int.Parse(Console.ReadLine());
            if(num[i] > 20)
            {
                Console.WriteLine("Invalid number");
                break;
            }
            else if(num[i] < 0)
            {
                Console.WriteLine("Invalid number");
                break;
            }
            else if(num[i] == 0)
            {
                break;
            }
            else if(num[i] > 10)
            {
                counter++;
            }
        }
        Console.WriteLine("You entered a number greater than ten {0} times", counter);

I am a very curious person and what i like most of coding is that people are going to write in differents way but in the end the result will be the same. So i am curious to see how would you solve it, to improve my knowledge


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...