/* (Start of program header annotation)
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: compile a console application -- enter x to calculate and output the value of the piecewise function F (x ).
* Author: Lei hengxin
* Completion date: January 1, September 08, 2012
* Version No.: V1.0
* Description of tasks and Solutions
* Input description:
* Problem description:
* Program output:
* End the comment in the program Header
*/
[Csharp]
<Span style = "font-size: 24px;"> using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace ConsoleApplication_read_string
{
Class Program
{
Static void Main (string [] args)
{
Double d; // If double is not added
Char c = 'a'; // cyclic control variable
Console. Write ("enter a number :");
String s = Console. ReadLine ();
Double x = double. Parse (s );
While (! C. Equals ('E') // end the loop when the character c is equal to e
{
If (x! = 0)
{
D = 1/x; // If double is added
}
Else
{
D = 0; // If double is not added, the system prompts that the name "d" does not exist in the current context ";
}
// Double d1 = double. Parse (d. ToString ("1F "));
// String d1 = Convert. ToDecimal ("d"). ToString ("0.00 ");
// String bb = d. ToString (). Substring (0, d. ToString (). LastIndexOf (".") + 3 );
// String bb = d. ToString (). Substring (0, d. ToString (). LastIndexOf (".") + 3 <d .()? D. Length: d. ToString (). LastIndexOf (".") + 3 );
Console. WriteLine ("\ r \ n is not rounded in, the value of the piecewise function f (x) is: {0}", Math. Round (d, 1 ));
// Console. WriteLine ("\ r \ n rounding, the value of the piecewise function f (x) is: {0}", d1 );
Console. Write ("do you want to continue the operation? Press "e" to end the operation. Press another key to continue the operation! ");
C = Console. ReadKey (). KeyChar;
Console. WriteLine ();
If (c. Equals ('E '))
{
Break;
}
Else
{
Console. Write ("enter a number :");
S = Console. ReadLine ();
X = double. Parse (s );
}
}
Console. ReadKey (false );
}
}
}
</Span>
Running result:
Experience Accumulation:
1. either of the following methods can forcibly convert a character to a double type.
[Csharp]
<Span style = "font-size: 24px;"> x = Convert. ToDouble (s );
X = double. Parse (s); </span>
Problem:
1. "Which function is used in C # To retain two decimal places ?"
Math. Round (d, 1) only gave up the last one without rounding it.
The methods provided on the network are not feasible, and it is a bit difficult to compile by yourself.
Please try to find out if there are any ready-made functions available.