Lsnso
New member
- Joined
- Aug 10, 2019
- Messages
- 1
- Programming Experience
- Beginner
Hey!
So I've been following the freeCodeCamp.org C# tutorial (Youtube). As I'm learning about if statements, the host says he'll create a little program that, given 3 numbers, gives you the bigger one out of the three. As the clearly superior individual I am, I try to do it by myself before seeing his suggestion/walkthrough. My program was a bit different as I used user input instead of simply choosing my 3 numbers as I write the code and then running the program. So my idea was that the user would enter three numbers (this way: 123 and not 1 2 3). Then I'd identify them using their index numbers (for example 1 would be arr[0]). Then I'd use them for my function ( MaxNum(arr[0] , arr[1] , arr[2]) ) and I'd see the biggest in the console. Problem is, if I Console.Write them they everything works fine but as soon as I use the function the numbers change. For instance, for the numbers 123, 1 now becomes 49, 2 becomes 50, 3 becomes 51 and so 51 is printed. I have "Console.Written" all of them ( arr[0], arr[1], arr[2] ) and they come up as 1 2 and 3. My code looks something like this: If anyone could help I'd appreciate, thank you Yes, im a complete noob at this.
So I've been following the freeCodeCamp.org C# tutorial (Youtube). As I'm learning about if statements, the host says he'll create a little program that, given 3 numbers, gives you the bigger one out of the three. As the clearly superior individual I am, I try to do it by myself before seeing his suggestion/walkthrough. My program was a bit different as I used user input instead of simply choosing my 3 numbers as I write the code and then running the program. So my idea was that the user would enter three numbers (this way: 123 and not 1 2 3). Then I'd identify them using their index numbers (for example 1 would be arr[0]). Then I'd use them for my function ( MaxNum(arr[0] , arr[1] , arr[2]) ) and I'd see the biggest in the console. Problem is, if I Console.Write them they everything works fine but as soon as I use the function the numbers change. For instance, for the numbers 123, 1 now becomes 49, 2 becomes 50, 3 becomes 51 and so 51 is printed. I have "Console.Written" all of them ( arr[0], arr[1], arr[2] ) and they come up as 1 2 and 3. My code looks something like this: If anyone could help I'd appreciate, thank you Yes, im a complete noob at this.
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpLearning
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter three numbers: ");
string arr = Console.ReadLine();
Console.WriteLine("The biggest number is: " + MaxNum(arr[0], arr[1], arr[2]));
Console.ReadLine();
}
static int MaxNum(int num1, int num2, int num3)
{
int result;
if (num1 >= num2 && num1 >= num3)
{
result = num1;
}
else if (num2 >= num1 && num2 >= num3)
{
result = num2;
}
else if (num3 >= num1 && num3 >= num1)
{
result = num3;
}
else
{
result = num1;
}
return result;
}
}
}
Last edited by a moderator: