所以。。。我的大学项目需要一个帮助。它要求编写一个程序,从用户那里收集数字并将它们添加到数组中,直到他们键入ok,当他们键入ok时显示数组的和。这就是我到现在为止想出来的,它接收的数字很好,但当我键入ok时,它显示错误并显示字符串格式。我该怎么办。。。?我哪里出错了。。?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace array_sum
{
class Program
{
static void Main(string[] args)
{
int[] nums = new int[10];
int i = -1;
Console.WriteLine("please eneter as many numbers as you want and when your done type: OK");
string input = Console.ReadLine();
while (true && input!= "ok")
{
if (int.TryParse(input, out int numsum))
{
nums[i + 1] = int.Parse(Console.ReadLine());
Array.Resize(ref nums, nums.Length + 1);
numsum = nums.Sum();
}
else
{
break;
}
}
if (input == "ok")
{
int numssums = nums.Sum ();
Console.WriteLine(numssums);
}
Console.ReadKey();
}
}
}
在我的语言中,我们有一句谚语“hij heeft de klok horen luiden,maar weet niet waar de klepel hangt”。你掌握了概念,却犯了很多错误。
我希望你能以身作则
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var nums = new List<int>();
Console.WriteLine("please enter as many numbers as you want and when your done type: OK");
string input;
while ((input = Console.ReadLine()).ToLower() != "ok")
{
if (int.TryParse(input, out int num))
{
nums.Add(num);
}
else
{
Console.WriteLine("please enter a number!");
}
}
Console.WriteLine(nums.Sum());
Console.ReadKey();
}
}