C#。如何根据值获取2-12用户输入并输出乘法表? [英] C#. How do I take a 2-12 user input and output a multiplication table based on the values?

查看:77
本文介绍了C#。如何根据值获取2-12用户输入并输出乘法表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我是C#的新手,我正在尝试将用户输入并输出到可调整大小的乘法表中。用户输入2-12的值,结果是这些维度的表格(即2 x 2或12 x 12)。我可以输出一个12 x 12的乘法表,但我似乎无法将用户输入连接到表来改变它的大小。任何帮助将不胜感激。



我尝试过:



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;



命名空间W5M2A2_CResizableMTableApp

{

class W5M2A2_CResizableMTableAppProgram

{

static void Main(string [] args)

{





//显示程序信息

Console.WriteLine(可调整大小的乘法表应用程序(v.1)\ n\ n);

< br $>
//要求输入

Console.Write(请输入乘法表的大小(例如2-12):);



//读入用户输入

string strValue = Console.ReadLine();



// Assig n变量为Double

double dblValue1;



//将字符串转换为Double

dblValue1 = Convert.ToDouble (strValue);



double a =(dblValue1);

double b =(dblValue1);

for(a = 1; a< = 12; a ++)

{

for(b = 1; b< = 12; b ++)

Console.Write((a * b) .ToString()。PadLeft(5));

Console.WriteLine();

}



Console.ReadLine();

}



}

}

解决方案

只需添加条件即可检查输入的数字。在您的情况下,只需使用下面的代码。





if(max> 12)

return; <如果(最大<= 12)

{

< pre lang =C#> Console.WriteLine( \ n \ n生成的乘法表,大小为{0} :,strValue);
Console.WriteLine( ----------------- ---------------------------- \\\
\\\
);
for (answer1 = 1 ; answer1 < ; = max; answer1 ++)
{
for (answer2 = 1 ; answer2 < = max2; answer2 ++)

Console.Write((answer1 * answer2).ToString()。PadLeft(< span class =code-digit> 5
));
Console.WriteLine();
}





}


检查一下,验证并删除不需要的代码



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 W5M2A2_CResizableMTableApp
{
class W5M2A2_CResizableMTableAppProgram
{
静态 void Main( string [] args)
{

// 要求输入
这里:Console.WriteLine( 请输入乘法表的大小(例如2 - 12):);


string strValue = Console.ReadLine();
int value ;
if (!int.TryParse(strValue, out value ))
{
Console.WriteLine( 无效的整数);
goto here;
}
else
{
if (< span class =code-sdkkeyword> value
> 1 && < span class =code-sdkkeyword> value < 13
{

Console.WriteLine( \ n \ nnGenerated Multiplication Table,大小为{ 0}:,strValue);
Console.WriteLine( ----------------- ---------------------------- \\\
\\\
);
for int i = 1 ; i < = value ; i ++)
{
for int j = 1 ; j < = value ; j ++)

Console.Write((i * j) .ToString()。PadLeft( 5 ));
Console.WriteLine();
}
}
else
{
Console.Write( 请输入2到12之间的数字);
goto here;

}

Console.ReadLine();
}
}

}
}


我建​​议你定义一个通用整数控制台的输入例程,使用常量来验证条目值:

  private   static   int  GetIntInRange()
{
string input = ;
int value = 0 ;

while true
{
Console .WriteLine( 在{0}〜{1}或退出之间输入一个数字以退出应用程序,MINVALUE,MAXVALUE);
input = Console.ReadLine();

if (input == EXITCODE) return -1;

if int .TryParse(input, out value ))
{
if value > = MINVALUE&& value < = MAXVALUE) return value ;
}
}
}

以下是您在控制台应用中使用此功能的方法:

 使用系统; 

命名空间 YourConsoleApplication
{
class Program
{
const string EXITCODE = 退出;

const int MINVALUE = 2 ;
const int MAXVALUE = 12 ;

静态 void Main( string [] args)
{
int value1 = 0 ;
int value2 = 0 ;

value1 = GetIntInRange();
if (value1 == -1)System.Environment.Exit(-1);

value2 = GetIntInRange();
if (value2 == -1)System.Environment.Exit(-1);

for int i = 1 ; i < = value1; i ++)
{
for int j = 1 ; j < = value2; j ++)
{
Console.WriteLine( {0} * {1} = {2},i,j,i * j);
}
}

Console.WriteLine( < Return /输入>终止申请);
Console.ReadKey();
}

private static int GetIntInRange()
{
string input = ;
int value = 0 ;

while true
{
Console .WriteLine( 在{0}〜{1}或退出之间输入一个数字以退出应用程序,MINVALUE,MAXVALUE);
input = Console.ReadLine();

if (input == EXITCODE) return -1;

if int .TryParse(input, out value ))
{
if value > = MINVALUE&& value < = MAXVALUE) return value ;
}
}
}
}
}

注意:



1.注意在两个整数的条目中,用户唯一的选择是输入所需范围内的数字,或输入退出并终止申请。



2.在实践中,您可能希望以不同的方式处理;例如:您可能希望为用户提供固定数量的尝试以输入有效数字,并向他们提供有关其无效尝试的反馈。


Hello. I am very new to C# and am trying to take a user input and output that into a resizable multiplication table. The user inputs values from 2-12 and the result is a table at those dimension (ie. 2 x 2, or 12 x 12). I can output a 12 x 12 multiplication table but I can't seem connect the user input to the table to change it's size. Any help would be appreciated.

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace W5M2A2_CResizableMTableApp
{
class W5M2A2_CResizableMTableAppProgram
{
static void Main(string[] args)
{


// Display program info
Console.WriteLine("Resizable Multiplication Table Application (v.1)\n\n");

// Ask for input
Console.Write("Please enter the size of the multiplication table (e.g. 2-12): ");

// read in user input
string strValue = Console.ReadLine();

// Assign variable to Double
double dblValue1;

// Convert String to Double
dblValue1 = Convert.ToDouble(strValue);

double a = (dblValue1);
double b = (dblValue1);
for (a = 1; a <= 12; a++)
{
for (b = 1; b <= 12; b++)
Console.Write((a * b).ToString().PadLeft(5));
Console.WriteLine();
}

Console.ReadLine();
}

}
}

解决方案

Just add condition to check your input number. In your case just use below code.


if(max >12)
return;

or
if(max <=12)
{

Console.WriteLine("\n\nGenerated Multiplication Table for size of {0}:", strValue);
Console.WriteLine("---------------------------------------------\n\n");
for (answer1 = 1; answer1 <= max; answer1++)
{
for (answer2 = 1; answer2 <= max2; answer2++)

Console.Write((answer1 * answer2).ToString().PadLeft(5));
Console.WriteLine();
}



}


check this, with validation and removed unwanted codes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace W5M2A2_CResizableMTableApp
{
    class W5M2A2_CResizableMTableAppProgram
    {
        static void Main(string[] args)
        { 

            // Ask for input
          here:  Console.WriteLine("Please enter the size of the multiplication table (e.g. 2 - 12): ");

           
            string strValue = Console.ReadLine();
            int value;
            if (!int.TryParse(strValue, out value))
            {
                Console.WriteLine("Invalid integer number");
                goto here;
            }
            else
            {
                if (value > 1 && value < 13)
                {

                    Console.WriteLine("\n\nGenerated Multiplication Table for size of {0}:", strValue);
                    Console.WriteLine("---------------------------------------------\n\n");
                    for (int i = 1; i <= value; i++)
                    {
                        for (int j = 1; j <= value; j++)

                            Console.Write((i * j).ToString().PadLeft(5));
                        Console.WriteLine();
                    }
                }
                else
                {
                    Console.Write("Please enter the number between 2 and 12 ");
                    goto here;

                }

                Console.ReadLine();
            }
        }

    }
}


I suggest you define a general purpose integer input routine for the Console that uses Constants to validate entry values:

private static int GetIntInRange()
{
    string input = "";
    int value = 0;

    while (true)
    {
        Console.WriteLine("enter a number between {0}~{1} or 'Exit' to exit application", MINVALUE, MAXVALUE);
        input = Console.ReadLine();

        if (input == EXITCODE) return -1;

        if (int.TryParse(input, out value))
        {
            if (value >= MINVALUE && value <= MAXVALUE) return value;
        }
    }
}

Here's how you'd use this in a Console app:

using System;

namespace YourConsoleApplication
{
    class Program
    {
        const string EXITCODE = "Exit";

        const int MINVALUE = 2;
        const int MAXVALUE = 12;

        static void Main(string[] args)
        {
            int value1 = 0;
            int value2 = 0;

            value1 = GetIntInRange();
            if(value1 == -1) System.Environment.Exit(-1);

            value2 = GetIntInRange();
            if(value2 == -1 ) System.Environment.Exit(-1);

            for (int i = 1; i <= value1; i++)
            {
                for (int j = 1; j <= value2; j++)
                {
                    Console.WriteLine("{0} * {1} = {2}", i,j, i*j);
                } 
            }

            Console.WriteLine("<Return/Enter> to terminate application");
            Console.ReadKey();
        }

        private static int GetIntInRange()
        {
            string input = "";
            int value = 0;

            while (true)
            {
                Console.WriteLine("enter a number between {0}~{1} or 'Exit' to exit application", MINVALUE, MAXVALUE);
                input = Console.ReadLine();

                if (input == EXITCODE) return -1;

                if (int.TryParse(input, out value))
                {
                    if (value >= MINVALUE && value <= MAXVALUE) return value;
                }
            }
        }
    }
}

Notes:

1. note the in the entry of the two integers that the only alternatives the user has is either to enter a number in the required range, or to type 'Exit' and terminate the application.

2. in practice you may want to handle that differently; for example: you may wish to give the user a fixed number of "tries" to enter a valid number, and give them feedback on their invalid attempts.


这篇关于C#。如何根据值获取2-12用户输入并输出乘法表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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