C
C#12mo ago
PocketPixie

Help with this coding problem.

Hello all, can someone diagnois what does it mean by this code stack?
public class Solution {
public int[] SortArrayByParityII(int[] nums) {
int n = Convert.ToInt32(nums);
if(n % 2 == 0)
{
Console.WriteLine(n);
}
else
{
Console.WriteLine(n);
}
}
}
public class Solution {
public int[] SortArrayByParityII(int[] nums) {
int n = Convert.ToInt32(nums);
if(n % 2 == 0)
{
Console.WriteLine(n);
}
else
{
Console.WriteLine(n);
}
}
}
The problem is
Line 2: Char 18: error CS0161: 'Solution.SortArrayByParityII(int[])': not all code paths return a value (in Solution.cs)
Line 2: Char 18: error CS0161: 'Solution.SortArrayByParityII(int[])': not all code paths return a value (in Solution.cs)
I've tried converting the array in the method to an int type but that doesn't work
7 Replies
SinFluxx
SinFluxx12mo ago
The int[] in your method definition is you saying that the method will return an array of ints, but you're not doing that
PocketPixie
PocketPixieOP12mo ago
Oh, thank you
public class Solution {
public int[] SortArrayByParityII(int[] nums) {
int n = Convert.ToInt32(nums);
if(n % 2 == 0)
{
Console.WriteLine(n);
}
else
{
Console.WriteLine(n);
}
return int[] result = n.ToString().Select(o=> Convert.ToInt32(n)).ToArray();
}
}
public class Solution {
public int[] SortArrayByParityII(int[] nums) {
int n = Convert.ToInt32(nums);
if(n % 2 == 0)
{
Console.WriteLine(n);
}
else
{
Console.WriteLine(n);
}
return int[] result = n.ToString().Select(o=> Convert.ToInt32(n)).ToArray();
}
}
it's still not converting even with the above code
SinFluxx
SinFluxx12mo ago
Convert.ToInt32(nums) Is this line not giving you an error? Also not really sure what you're trying to do in your return statement, or why you're assigning to a new variable to instantly return it
PocketPixie
PocketPixieOP12mo ago
The Convert.ToInt32 isn't giving me any errors, but I am attempting to have it where the numbers when divided by 2 come out as even, else they come out as odd. It requires that i have a return as an array though
SinFluxx
SinFluxx12mo ago
I don't think you mean to be passing the whole array into the convert method though, or why you'd need to convert to int when it's already an array of ints?
I am attempting to have it where the numbers when divided by 2 come out as even, else they come out as odd
I'm not sure what you mean here?
Angius
Angius12mo ago
You will need to use a loop, then Loop over the items in nums Parse each to an int Then check if that int is even or odd
PocketPixie
PocketPixieOP12mo ago
Thanks I thought I would have to cast the array as an integer first, then back return as an array
Want results from more Discord servers?
Add your server