C# 使用方法并将用户输入放入数组
我正在尝试创建一个程序,它使用方法和数组来获取用户输入并显示最小值、最大值、平均值和总值。我已经创建了从用户输入返回最小值、最大值、总数和平均值的方法。代码允许我获取用户输入,但不会显示。我做错了什么?这就是我所拥有的C# 使用方法并将用户输入放入数组,c#,C#,我正在尝试创建一个程序,它使用方法和数组来获取用户输入并显示最小值、最大值、平均值和总值。我已经创建了从用户输入返回最小值、最大值、总数和平均值的方法。代码允许我获取用户输入,但不会显示。我做错了什么?这就是我所拥有的 class Program { const int SIZE = 6; static void Main(string[] args) { double min = 0.0; double max = 0.0; double
class Program
{
const int SIZE = 6;
static void Main(string[] args)
{
double min = 0.0;
double max = 0.0;
double ave = 0.0;
double total = 0.0;
double[] numbers = new double[SIZE];
getInput(numbers);
calcmin(numbers, ref min);
calcmax(numbers, ref max);
calcTotal(numbers, ref total);
calcave(numbers, ref ave, ref total);
printResult(numbers, ref min, ref max, ref ave, ref total);
}//v
static void calcave(double[] numbers, ref double ave, ref double total)
{
int sub = 0;
while (sub < numbers.Length)
{
ave = total / numbers.Length;
}
sub++;
}//end while
static void calcTotal(double[] numbers, ref double total)
{
int sub = 0;
while (sub < numbers.Length)
{
total = numbers[sub] + total ;
}
sub++
}//end while
static void calcmax(double[] numbers, ref double max)
{
int sub = 0;
max = numbers[0];
while (sub < numbers.Length)
if (numbers[sub] > max)
{
numbers[sub] = max;
}//end if
sub++;
}//end while
static void calcmin(double[] numbers, ref double min)
{
int sub = 0;
min = numbers[0];
while (sub < numbers.Length)
if (numbers[sub] < min)
{
numbers[sub] = min;
}//end if
sub++;
}//end while
static void getInput(double[] numbers)
{
int sub = 0;
for (sub = 0; sub < numbers.Length; sub++)
{
Console.WriteLine("Enter number {0}",sub + 1);
while (!double.TryParse(Console.ReadLine(), out numbers[sub]))
Console.WriteLine("Please try again.");
}//End write lines
}//end while
static void printResult(double[] numbers, ref double min, ref double max, ref double ave, ref double total)
{
Console.WriteLine("The smallest number is {0}.", min);
Console.WriteLine("the largest number is {0}.", max);
Console.WriteLine("the total is {0}.", total);
Console.WriteLine("The average is {0}.", ave);
}//end write lines for out put of Minimum, Maximum, Total, and Average.
}
}
}
类程序
{
常数int SIZE=6;
静态void Main(字符串[]参数)
{
双最小值=0.0;
双最大值=0.0;
双平均值=0.0;
双倍合计=0.0;
双精度[]数字=新的双精度[尺寸];
获取输入(数字);
calcmin(数字,参考最小值);
calcmax(数字,参考最大值);
calcTotal(数字、参考总数);
计算(数字、参考平均值、参考总值);
打印结果(编号、参考最小值、参考最大值、参考平均值、参考总计);
}//五
静态空隙率(双[]个数字,参考双平均值,参考双总计)
{
int sub=0;
while(sub最大值)
{
数字[子]=最大值;
}//如果结束
sub++;
}//结束时
静态无效calcmin(双[]个数字,参考双最小值)
{
int sub=0;
最小值=数字[0];
while(sub
在calcmin
方法中,您没有增加循环中的s
。所以循环永远不会结束。将其更改为:
static void calcmin(double[] numbers, ref double min)
{
int sub = 0;
min = numbers[0];
while (sub < numbers.Length)
{
if (numbers[sub] < min)
{
numbers[sub] = min;
}
sub++;
}
}
在calcave
中也有同样的错误
编辑:查找差异。这是完整的代码
class Program
{
const int SIZE = 6;
static void Main(string[] args)
{
double min = 0.0;
double max = 0.0;
double ave = 0.0;
double total = 0.0;
double[] numbers = new double[SIZE];
getInput(numbers);
calcmin(numbers, ref min);
calcmax(numbers, ref max);
calcTotal(numbers, ref total);
calcave(numbers, ref ave, total);
printResult(numbers, ref min, ref max, ref ave, ref total);
Console.ReadLine();
}
static void calcave(double[] numbers, ref double ave, double total)
{
ave = total / numbers.Length;
}
static void calcTotal(double[] numbers, ref double total)
{
int sub = 0;
while (sub < numbers.Length)
{
total = numbers[sub] + total;
sub++;
}
}
static void calcmax(double[] numbers, ref double max)
{
int sub = 0;
max = numbers[0];
while (sub < numbers.Length)
{
if (numbers[sub] > max)
{
max = numbers[sub]; //HERE
}
sub++;
}
}
static void calcmin(double[] numbers, ref double min)
{
int sub = 0;
min = numbers[0];
while (sub < numbers.Length)
{
if (numbers[sub] < min)
{
min = numbers[sub]; //HERE
}
sub++;
}
}
static void getInput(double[] numbers)
{
int sub = 0;
for (sub = 0; sub < numbers.Length; sub++)
{
Console.WriteLine("Enter number {0}", sub + 1);
while (!double.TryParse(Console.ReadLine(), out numbers[sub]))
Console.WriteLine("Please try again.");
}
}
static void printResult(double[] numbers, ref double min, ref double max, ref double ave, ref double total)
{
Console.WriteLine("The smallest number is {0}.", min);
Console.WriteLine("the largest number is {0}.", max);
Console.WriteLine("the total is {0}.", total);
Console.WriteLine("The average is {0}.", ave);
}
}
类程序
{
常数int SIZE=6;
静态void Main(字符串[]参数)
{
双最小值=0.0;
双最大值=0.0;
双平均值=0.0;
双倍合计=0.0;
双精度[]数字=新的双精度[尺寸];
获取输入(数字);
calcmin(数字,参考最小值);
calcmax(数字,参考最大值);
calcTotal(数字、参考总数);
计算(数字、参考值、总数);
打印结果(编号、参考最小值、参考最大值、参考平均值、参考总计);
Console.ReadLine();
}
静态空隙率(双[]个数字,参考双平均值,双总计)
{
ave=总数/数量。长度;
}
静态无效calcTotal(双倍[]数字,参考双倍总数)
{
int sub=0;
while(sub最大值)
{
max=numbers[sub];//此处
}
sub++;
}
}
静态无效calcmin(双[]个数字,参考双最小值)
{
int sub=0;
最小值=数字[0];
while(sub
您的程序中有几个错误
在calcmin
中,sub++
不在while
语句中。这样,while
循环就永远不会结束。
另一个问题是calcmin
方法中的逻辑。此方法用最小值替换数组中的所有值。使用此方法后,整个数字
数组具有相同的值
您的calcmax
方法也是如此sub++
在while循环之外,您将用最大值替换所有值(等于前面方法之后的最小值)
calctotal
在while
循环之外有sub++
calcave只需要总数和项目数来计算平均值。我不明白为什么要使用while
循环(在循环之外使用sub++
)
您应该继续阅读,了解如何在while循环中移动
sub++
。您是如何运行程序的?这不是唯一的错误。作业编号[sub]把计算搞砸了。是的,这是作业。我是新来的
class Program
{
const int SIZE = 6;
static void Main(string[] args)
{
double min = 0.0;
double max = 0.0;
double ave = 0.0;
double total = 0.0;
double[] numbers = new double[SIZE];
getInput(numbers);
calcmin(numbers, ref min);
calcmax(numbers, ref max);
calcTotal(numbers, ref total);
calcave(numbers, ref ave, total);
printResult(numbers, ref min, ref max, ref ave, ref total);
Console.ReadLine();
}
static void calcave(double[] numbers, ref double ave, double total)
{
ave = total / numbers.Length;
}
static void calcTotal(double[] numbers, ref double total)
{
int sub = 0;
while (sub < numbers.Length)
{
total = numbers[sub] + total;
sub++;
}
}
static void calcmax(double[] numbers, ref double max)
{
int sub = 0;
max = numbers[0];
while (sub < numbers.Length)
{
if (numbers[sub] > max)
{
max = numbers[sub]; //HERE
}
sub++;
}
}
static void calcmin(double[] numbers, ref double min)
{
int sub = 0;
min = numbers[0];
while (sub < numbers.Length)
{
if (numbers[sub] < min)
{
min = numbers[sub]; //HERE
}
sub++;
}
}
static void getInput(double[] numbers)
{
int sub = 0;
for (sub = 0; sub < numbers.Length; sub++)
{
Console.WriteLine("Enter number {0}", sub + 1);
while (!double.TryParse(Console.ReadLine(), out numbers[sub]))
Console.WriteLine("Please try again.");
}
}
static void printResult(double[] numbers, ref double min, ref double max, ref double ave, ref double total)
{
Console.WriteLine("The smallest number is {0}.", min);
Console.WriteLine("the largest number is {0}.", max);
Console.WriteLine("the total is {0}.", total);
Console.WriteLine("The average is {0}.", ave);
}
}