我如何在C#中编写此代码 [英] how can i write this code in C#

查看:65
本文介绍了我如何在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 replace printf 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 of name from char[50] to string

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();.
Well clrscr(); is the equivalent of Console.Clear();. printf equates to Console.Write(); (but if you want it to run to new lines, use Console.WriteLine();. getch(); is similar to Console.ReadKey(); and scanf(); can be thought of as Console.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();
// ...


这篇关于我如何在C#中编写此代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆