如何使用'for loop'数组'在c#
本文关键字:数组 loop for 何使用 | 更新日期: 2023-09-27 18:17:58
我是c#的新手,我正在学习反转整数(数字)。我必须使用forloop和数组来完成代码,但我真的不知道如何使用它们。
为例。如果输入为:1 2 3 4 5 6
结果应为:6 5 4 3 2 1
有人能帮帮我吗?谢谢你:)
我真的很抱歉,伙计们,我忘了添加代码。在这里。再次感谢static void Main(string[] args)
{
Console.WriteLine("Enter 6 Numbers");
int numb = int.Parse(Console.ReadLine());
int reverse = 0;
while (numb > 0)
{
int rem = numb % 10;
reverse = (reverse * 10) + rem;
numb = numb / 10;
}
Console.WriteLine("The reverse is = {0}", reverse);
Console.ReadLine();
}
编辑
这里一个字符串作为输入,需要反转。只是因为输入只是作为数字,所以这有点令人困惑。
除非您需要使用循环,否则更简单的解决方案是使用Array.Reverse
:
int[] array = { 1, 2, 3, 4, 5, 6 };
Array.Reverse(array);
可以使用for循环迭代数组的一半,与数组另一边的位置交换位置。
int holder = 0;
int size = array.Length;
for (int i = 0; i < size / 2; ++i)
{
holder = array[i];
array[i] = array[size - 1 - i];
array[size - 1 - i] = holder;
}
这是一个使用循环和使用数组来反转数字/整数的简单逻辑。反向功能
示例:输入12345将给我们输出54321
namespace PurushLogics
{
class Purush_ReverseInteger
{
static void Main()
{
//Reverse a Number
int intstr;
string intreverse = "";
int intLength = 0;
int? intj = null;
Console.WriteLine("Enter a Number"); // Entering "12345" has input will return "54321" as output
intstr = int.Parse(Console.ReadLine());//Getting integer from Console
char[] inta = intstr.ToString().ToCharArray();
intLength = inta.Length - 1;
for (int c = intLength; c >= 0; c--)
{
intreverse = intreverse + inta[c].ToString();
intj = int.Parse(intreverse);
}
Console.WriteLine("Reverse Number is '"{0}'"", intj);//Displaying the reverse integer
Console.ReadLine();
//Reverse integer using Array.Reverse
Array.Reverse(inta);
foreach (char reverse in inta)
{
Console.Write(reverse);
}
Console.ReadLine();
}
}
}
static void Main(string[] args)
{
int[] numbers = new int[6];
for (int i = 0; i < 6; i++)
{
Console.Write("Number {0} : ", i + 1);
numbers[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine();
for (int i = 5; i >= 0; i--)
{
Console.Write("{0} ", numbers[i]);
}
Console.ReadLine();
}
class Program
{
static void Main(string[] args)
{
int[] array = new int[] { 1, 2, 3, 4, 5, 6,7};
int temp = 0;
for (int i = array.Length - 1; i >= (((array.Length - 1) % 2==0)? (((array.Length - 1) / 2)):((array.Length - 1) / 2)+1) ; i--)
{
temp = array[(array.Length - 1) - i];
array[(array.Length - 1) - i] = array[i];
array[i] = temp;
}
foreach (var p in array)
{
Console.WriteLine(p);
}
Console.ReadLine();
}
}
int[] numbers=new int[]{1,2,3,4,5};
int[] reverse=new int[numbers.Length];
int j=numbers.Length-1;
for(int i=0;i<numbers.Length;i++)
{
reverse[i]=numbers[j];
Console.WriteLine(reverse[i]);
j--;
}
public static int[] Test( params Int32[] nums)
{
int[] ar = new int[nums.Length];
var count = 0;
for (int i = nums.Length-1; i >= 0 ; i--)
{
ar[count] = nums[i];
count++;
}
return ar;
}
This is Palindrom or not string tmp=""; string values = "";Console.Write("Please Enter Number : "); string strnumber = Convert.ToString(Console.ReadLine()); tmp = strnumber; if (strnumber.Length > 0) { foreach (var res in strnumber.Reverse()) { values += res; } } if (values == strnumber) { Console.WriteLine("Number is Palindrome."); } else { Console.WriteLine("Number is not Palindrome")