使用params在方法中传递变量数组时出错。 [英] Error while passing a variable array in a method using params.

查看:100
本文介绍了使用params在方法中传递变量数组时出错。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个可以将多个整数值作为参数的方法。我试图通过使用params来做到这一点。当我尝试执行此程序时,第5行MinVal声明出现错误



使用System; 
命名空间LearnigToProgram
{
class Min {
public void MinVal(params [] int numbers)
{
for(int i = 0; i< ; numbers.Length; i ++)
{
Console.WriteLine(numbers [i]);
Console.WriteLine();
}
}

}
类ParamsDemo {
static void Main(){
Min obj1 = new Min();
obj1.MinVal(1,2,3,4,5);


}
}

}

解决方案

试试这个,在线评论。

 使用系统; 
使用 System.Windows; // 添加此命名空间
命名空间 LearnigToProgram
{
class Min {
public void MinVal( params int [] number)< span class =code-comment> // 只有一个变量名称是有效语法
{
for int i = 0 ; i < numbers.Length; i ++)
{
Console.WriteLine(numbers [i]);
Console.WriteLine();
}
Console.ReadLine(); // 等待输入,查看控制台窗口上显示的输出,否则窗口将立即关闭
}

}

class ParamsDemo {
static void Main(){
Min obj1 = new Min();
obj1.MinVal( 1 2 3 4 5 );
}
}

}


我看到你的代码唯一的问题是你有阵列您的MinVal方法声明错误。



以下代码,只有更正的数据,编译并按预期运行。



< pre lang =C#> 静态 void Main( string [] args)
{

Min obj1 = new Min();
obj1.MinVal( 1 2 3 4 5 );

Console.ReadLine();
}


class 最低
{
public void MinVal( params int []数字)
{
for int i = 0 ; i < numbers.Length; i ++)
{
Console。的WriteLine(数字[1]);
Console.WriteLine();
}
}

}


I am creating a method that can take multiple integer values as arguments . I am trying to do this by using params. An error shows up on line 5 ,MinVal declaration, when I am trying to execute this program

using System;
namespace LearnigToProgram
{
	class Min {
		public void MinVal(params []int numbers)
		{
			for (int i = 0; i < numbers.Length; i++)
			{
				Console.WriteLine(numbers[i]);
				Console.WriteLine();
			}
		}
		
		}
		class ParamsDemo {
			static void Main() {
			Min obj1 = new Min();
			obj1.MinVal(1, 2, 3, 4, 5);

			
			}
		}
	
	}

解决方案

try this, check in line comments.

using System;
using System.Windows;  // Add this namespace
namespace LearnigToProgram
{
	class Min {
		public void MinVal(params int[] numbers) // only one variable name is a valid syntax
		{
			for (int i = 0; i < numbers.Length; i++)
			{
				Console.WriteLine(numbers[i]);
				Console.WriteLine();
			}
            Console.ReadLine(); //waits for input,  to view the output being displayed on the console window, else the window will be closed immeditely
		}
		
		}

		class ParamsDemo {
			static void Main() {
			Min obj1 = new Min();
			obj1.MinVal(1, 2, 3, 4, 5); 
			}
		}
	
	}


The only issue i see with your code is you have the array declaration wrong in your MinVal method.

The following code, only thing corrected being the array, compiles and runs as expected.

static void Main(string[] args)
        {

            Min obj1 = new Min();
            obj1.MinVal(1, 2, 3, 4, 5);

            Console.ReadLine();
        }


        class Min
        {
            public void MinVal(params int[] numbers)
            {
                for (int i = 0; i < numbers.Length; i++)
                {
                    Console.WriteLine(numbers[i]);
                    Console.WriteLine();
                }
            }

        }


这篇关于使用params在方法中传递变量数组时出错。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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