数组乘对
Array multiply pairs
我必须在 C# 中创建代码以:
要求用户将任意一组数字输入数组并显示所有输入的数字。
我完成了这部分并且代码运行正常
然后将数字对相乘并显示结果。如果您有奇数个数字,则只显示最后一个数字。
2 3 8 4 变成 6 32
2 3 8 4 7 变成 6 32 7
我的问题是奇数数组。如果数组有偶数个元素没问题,但如果数组有奇数个元素就会出错,永远不要打印最后一个元素。
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArrayDisplay
{
class Array
{
static void Main(string[] args)
{
int[] array = new int[9];
int userInput = -1;
int itt = 0;
int count = 0;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
while (userInput != -1 && itt<array.Length)
{
array[itt] = userInput;
itt++;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
}
Console.WriteLine("The array contains: ");
for (int i = 0; i < array.Length; i++) {
Console.Write(" {0} , " ,array[i]);
}
Console.WriteLine("");
Console.WriteLine("count {0}",count-1);
if (count % 2 == 0)
{
for (int i = 0; i < array.Length; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
else {
for (int i = 0; i < array.Length; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1])
}
}
}
}
}
因为 7*0=0,请尝试以下:
if (count % 2 == 0)
{
for (int i = 0; i < array.Length - 1; i += 2)
{
Console.Write(" {0} , ", i == count - 2 ? array[i] : array[i] * array[i + 1]);
}
}
else
{
for (int i = 0; i < array.Length - 1; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
试试这个:
int[] array = new int[9];
int userInput = -1;
int itt = 0;
int count = 0;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
while (userInput != -1 && itt < array.Length)
{
array[itt] = userInput;
itt++;
if (count == 9) break;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
}
Console.WriteLine("The array contains: ");
for (int i = 0; i < array.Length; i++)
{
Console.Write(" {0} , ", array[i]);
}
Console.WriteLine("");
Console.WriteLine("count {0}", count);
if (count % 2 == 0)
{
for (int i = 0; i < array.Length; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
else
{
for (
int i = 0; i < array.Length; i += 1)
{
if (i == 7)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
break;
}
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
Console.ReadLine()
;
我必须在 C# 中创建代码以:
要求用户将任意一组数字输入数组并显示所有输入的数字。 我完成了这部分并且代码运行正常
然后将数字对相乘并显示结果。如果您有奇数个数字,则只显示最后一个数字。
2 3 8 4 变成 6 32 2 3 8 4 7 变成 6 32 7
我的问题是奇数数组。如果数组有偶数个元素没问题,但如果数组有奇数个元素就会出错,永远不要打印最后一个元素。
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArrayDisplay
{
class Array
{
static void Main(string[] args)
{
int[] array = new int[9];
int userInput = -1;
int itt = 0;
int count = 0;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
while (userInput != -1 && itt<array.Length)
{
array[itt] = userInput;
itt++;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
}
Console.WriteLine("The array contains: ");
for (int i = 0; i < array.Length; i++) {
Console.Write(" {0} , " ,array[i]);
}
Console.WriteLine("");
Console.WriteLine("count {0}",count-1);
if (count % 2 == 0)
{
for (int i = 0; i < array.Length; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
else {
for (int i = 0; i < array.Length; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1])
}
}
}
}
}
因为 7*0=0,请尝试以下:
if (count % 2 == 0)
{
for (int i = 0; i < array.Length - 1; i += 2)
{
Console.Write(" {0} , ", i == count - 2 ? array[i] : array[i] * array[i + 1]);
}
}
else
{
for (int i = 0; i < array.Length - 1; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
试试这个:
int[] array = new int[9];
int userInput = -1;
int itt = 0;
int count = 0;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
while (userInput != -1 && itt < array.Length)
{
array[itt] = userInput;
itt++;
if (count == 9) break;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
}
Console.WriteLine("The array contains: ");
for (int i = 0; i < array.Length; i++)
{
Console.Write(" {0} , ", array[i]);
}
Console.WriteLine("");
Console.WriteLine("count {0}", count);
if (count % 2 == 0)
{
for (int i = 0; i < array.Length; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
else
{
for (
int i = 0; i < array.Length; i += 1)
{
if (i == 7)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
break;
}
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
Console.ReadLine()
;