我如何在C#中编写此代码
问题描述:
#include<stdio.h>
void main()
{
float basic, da, hra, tax, pf, gross, net;
char name[50];
clrscr();
printf("\n\n\t ENTER YOUR NAME...:");
scanf("%s", &name);
printf("\n\t ENTER THE BASIC SALARY...:");
scanf("%f", &basic);
pf = 0.08 * basic;
if (basic < 5000)
{
da = 0.3 * basic;
hra = 0.08 * basic;
}
else if ((basic >= 5000) && (basic < 10000))
{
da = 0.4 * basic;
hra = 0.1 * basic;
}
else
{
da = 0.5 * basic;
hra = 0.2 * basic;
}
gross = basic + da + hra;
net = gross - tax + pf;
printf("\n\n\t THE GROSS SALARY IS...: %f", gross);
printf("\n\n\t THE NET SALARY IS...: %f", net);
getch();
}
答
将其创建为控制台应用程序,然后将printf
替换为Console.WriteLine,将scanf替换为Console.ReadLine以及适当的转换方法,在字符串和所需的数据类型之间切换.您需要将最后两行的语法从%f"更改为"{0}",并将name
的数据类型从char[50]
更改为string
要将字符串转换为双精度:
Cretae it as a console application, and replaceprintf
with Console.WriteLine and scanf with Console.ReadLine plus the appropriate conversion method to change between a string and the required datatype. You will need to change the syntax of the last two lines from "%f" to "{0}", and change the datatype ofname
fromchar[50]
tostring
To convert a string to a double:
double d = double.Parse(myStringValue);
我想您还可以猜测其他数据类型!
大部分代码将保持不变-几乎只需要修改I/O内容.
I think you can guess the other datatypes!
Most of the code will work unchanged - it is only the I/O stuff you will need to modify, pretty much.
好吧clrscr();
等同于Console.Clear();
.printf
等同于Console.Write();
(但是,如果您希望它运行到新行,请使用Console.WriteLine();
.getch();与Console.ReadKey();
类似,并且scanf();
可以认为是Console.ReadLine();
.
Wellclrscr();
is the equivalent ofConsole.Clear();
.printf
equates toConsole.Write();
(but if you want it to run to new lines, useConsole.WriteLine();
. getch(); is similar toConsole.ReadKey();
andscanf();
can be thought of asConsole.ReadLine();
.
我想您正在寻找printf和scanf的C#版本吗?
看看 System.Console WriteLine和Read可能会帮助您.
I guess you are looking for the C# Versions of printf and scanf?
Take a look at System.Console WriteLine and Read might help you.
Console.WriteLine("Enter your name");
string name = Console.ReadString();
// ...