Small-dish programming growth (an interview frustrated-the code is not wrong is good?) ) 1th/3 page _c# Tutorial

Source: Internet
Author: User
Tags readline
Small dishes This year computer professional senior, learned a lot of software development aspects of things, but also learned to make up a small program, complacent, one is bent to find a good unit. When the delivery of a few resumes, finally received a unit interview notice, side dishes ecstatic.
To the People's unit, the receptionist gave him a title, said, "please use C + +, Java, C # or vb.net any one of the object-oriented language to implement a calculator console program, require the input of two numbers and operational symbols, get results." ”
A piece of cake, this is not simple, envelope, less than 10 minutes, side dishes finished, feel no mistakes. After handing in the papers, the unit said a week and so on notice bar. So the vegetable had to wait patiently. But half a month has passed, what news has not, the vegetable is very puzzled, my code realizes, why not give me the opportunity.
Vegetable to find work three years of Shige Big Bird, ask the reason, Big Bird asked the topic and understand the details of the side-dishes code, laughing, said: "Small vegetable, you fooled, people's unit of the meaning of the topic, you completely did not understand, of course, will not contact you."
The side Dish said: "Is my Code wrong?" The unit topic is not to want me to implement a calculator code, I write so what is the problem. ”
Copy Code code as follows:

Class Program
{
static void Main (string[] args)
{
Console.Write ("Please enter a number a:");
String A = Console.ReadLine ();
Console.Write ("Please select the operator symbol (+ 、-、 *,/):");
String B = Console.ReadLine ();
Console.Write ("Please enter the number B:");
String C = Console.ReadLine ();
String D = "";

if (B = = "+")
D = convert.tostring (convert.todouble (A) + convert.todouble (C));
if (B = = "-")
D = convert.tostring (convert.todouble (A)-convert.todouble (C));
if (B = = "*")
D = convert.tostring (convert.todouble (A) * convert.todouble (C));
if (O = = "/")
D = convert.tostring (convert.todouble (A)/convert.todouble (C));

Console.WriteLine ("The result is:" + D);
}
}

What's wrong with the code for the side dishes?
Two code specifications, refactoring

Big Bird said: "And do not say the topic of the person's meaning, alone in your current code, there are many deficiencies need to improve." Like variable naming, your name is ABCD, the variable does not have any concrete meaning, this is very irregular; Judging branch, you're writing like this means that every condition has to be judged, equal to three times the computer did not work; Data input validity judgment, etc. what if the user entered a character symbol instead of a number? What if the customer entered 0 when dividing the divisor? These are areas that can be improved. ”

"Oh, that's right, I have heard the teacher said before, but never cared about, I immediately change, change the end to show you." ”
Copy Code code as follows:

Class Program
{
static void Main (string[] args)
{
Try
{
Console.Write ("Please enter a number a:");
String strnumbera = Console.ReadLine ();
Console.Write ("Please select the operator symbol (+ 、-、 *,/):");
String stroperate = Console.ReadLine ();
Console.Write ("Please enter the number B:");
String strnumberb = Console.ReadLine ();
String strresult = "";

Switch (stroperate)
{
Case "+":
strresult = convert.tostring (convert.todouble (Strnumbera) + convert.todouble (strnumberb));
Break
Case "-":
strresult = convert.tostring (convert.todouble (Strnumbera)-convert.todouble (Strnumberb));
Break
Case "*":
strresult = convert.tostring (convert.todouble (Strnumbera) * convert.todouble (Strnumberb));
Break
Case "/":
if (Strnumberb!= "0")
strresult = convert.tostring (convert.todouble (Strnumbera)/convert.todouble (Strnumberb));
Else
strresult = "Divisor cannot be 0";
Break
}

Console.WriteLine ("The result is:" + strresult);

Console.ReadLine ();


}
catch (Exception ex)
{
Console.WriteLine ("Your input is wrong:" + ex. message);
}
}
}


Big Bird: "Ho ho, yes, yes, change quickly?" To the current code, the implementation of the calculator is no problem, but this write code is the same as the author of the Meaning of it? ”

Side dishes: "Do you mean object oriented?" ”

Big Bird: "Ha, the vegetable is not a vegetable also!" ”
Current 1/3 page 123 Next read the full text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.