Console.Readline&需要Console.Writeline帮助。 [英] Console.Readline & Console.Writeline help required.
问题描述
我有一个用c编写的代码。我想转换为c#的相同代码。我无法得到它。请求你的帮助。
我知道我们必须使用Console.Writeline for printf。但是我没有得到的是你在使用Console.Readline命令时如何替换& m& n。
1.我的意思是如何以m和n存储用户输入值。
2.接下来,如何将成本值存储在c [] []数组中?
,最后,
3.如何打印总价值。
我在C中的代码如下:
I have a code written in c. The same code i want to convert to c#. I am unable to get that. Requesting your help.
I do know we have to use Console.Writeline for printf. But what i am not getting is how you will replace &m &n while using the Console.Readline command.
1. I mean how to store the user input values in m and n.
2. Nextly, how to store the cost values in the c[][] array?
and lastly,
3. How to print the total value.
My code in C is as follows:
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i,j,c[100][100],cost,total=0;
clrscr();
printf("Enter number of rows & columns\n");
scanf("%d %d",&m,&n);
printf("Enter cost for each combination\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d", &c[i][j]);
}
}
printf("The total cost computation\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(c[i][j]>0)
{
cost=c[i][j];
total=total+cost;
}
}
}
printf("the total cost is %d", total);
getch();
}
推荐答案
考虑如何在C#控制台应用程序的上下文中突破此代码:
a。得到行数和列数
a.1创建两个整数变量,
Consider how this code breaks out in the context of a C# Console application:
a. get the number of rows and columns
a.1 create two integer variables,
int rows, columns;
a.2写一个'while循环,一直持续到用户为行/列输入两个有效整数
a.2 write a 'while loop that keeps going until the user enters two valid integers for rows/columns
bool arrayParamsIncomplete = true;
while (arrayParamsIncomplete)
{
// get an integer for rows
// validate it with Int32.TryParse
// continue if invalid
// get an integer for columns
// validate it with Int32.TryParse
// continue if invalid
// exit the loop
arrayParamsIncomplete = false;
}
b。使用类似的'while循环来处理以对您有意义的方式读取值的任务:逐行?使用String.Split将值转换为'String的数组,然后使用'Int32.TryParse来验证每个条目。
我认为你更接近于你知道的解决方案。
b. Use a similar 'while loop to handle the task of reading values in a way that makes sense to you: row by row ? Use String.Split to get the values into an array of 'String, and then use 'Int32.TryParse to validate each entry.
I think you are closer to a solution than you know.
在scanf和console.WriteLine中使用Console.ReadLine你在哪里printf。除此之外,代码应该相当多。如果你不能查看那个C代码并看到c#等价物,那么你真的需要先学习c#,因为这是一个相当简单的任务。我们不是来为你工作。
Use Console.ReadLine where you scanf and Console.WriteLine where you printf. Other than that the code should come over fairly much as it is. If you can't look at that C code and see the c# equivalent then you really need to learn c# first as this is a fairly simple task. We're not here to do your work for you.
这篇关于Console.Readline&需要Console.Writeline帮助。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!