C
C#3mo ago
Brosquare

Write a C# Sharp program to swap two numbers.Test Data:Input the First Number : 5Input the Second

the above is the question
44 Replies
Brosquare
Brosquare3mo ago
using System;

public class EX5
{
public static void Main(string[] args)
{
int NUM1,NUM2,temp;

Console.WriteLine("First Number: ");
NUM1 = (Console.ReadLine());

Console.WriteLine("Second Number: ");
NUM2 = (Console.ReadLine());

temp = NUM1;
NUM1 = NUM2;
NUM2 = temp;

Console.WriteLine("Numbers after swaping: ");
Console.WriteLine("Number One: " + NUM1);
Console.WriteLine("Number Two: " + NUM2);
}
}


using System;

public class EX5
{
public static void Main(string[] args)
{
int NUM1,NUM2,temp;

Console.WriteLine("First Number: ");
NUM1 = (Console.ReadLine());

Console.WriteLine("Second Number: ");
NUM2 = (Console.ReadLine());

temp = NUM1;
NUM1 = NUM2;
NUM2 = temp;

Console.WriteLine("Numbers after swaping: ");
Console.WriteLine("Number One: " + NUM1);
Console.WriteLine("Number Two: " + NUM2);
}
}


this my answer and its not working
Lisa
Lisa3mo ago
This doesn't compile because you're assigning a string to an int. console.ReadLine() returns a string. Either you'd have to make NUM1, NUM2 and temp a string, or you'd have to parse the strings. $parse
MODiX
MODiX3mo ago
use $tryparse
Lisa
Lisa3mo ago
$tryparse
MODiX
MODiX3mo ago
When you don't know if a string is actually a number when handling user input, use int.TryParse (or variants, e.g. double.TryParse)
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
TryParse returns a bool, where true indicates successful parsing. Remarks: - Avoid int.Parse if you do not know if the value parsed is definitely a number. - Avoid Convert.ToInt32 entirely, this is an older method and Parse should be preferred where you know the string can be parsed. Read more here
Brosquare
Brosquare3mo ago
i did parse
Pobiega
Pobiega3mo ago
Not in the code you pasted above.
Brosquare
Brosquare3mo ago
using System;

namespace Swaping
{
public class EX5
{
public static void Main(string[] args)
{
int NUM1,NUM2,temp;

Console.WriteLine("First Number: ");
NUM1 = int.parse(Console.ReadLine());

Console.WriteLine("Second Number: ");
NUM2 = int.parse(Console.ReadLine());

temp = NUM1;
NUM1 = NUM2;
NUM2 = temp;

Console.WriteLine("Numbers after swaping: ");
Console.WriteLine("Number One: " + NUM1);
Console.WriteLine("Number Two: " + NUM2);
}
}
}
using System;

namespace Swaping
{
public class EX5
{
public static void Main(string[] args)
{
int NUM1,NUM2,temp;

Console.WriteLine("First Number: ");
NUM1 = int.parse(Console.ReadLine());

Console.WriteLine("Second Number: ");
NUM2 = int.parse(Console.ReadLine());

temp = NUM1;
NUM1 = NUM2;
NUM2 = temp;

Console.WriteLine("Numbers after swaping: ");
Console.WriteLine("Number One: " + NUM1);
Console.WriteLine("Number Two: " + NUM2);
}
}
}
Pobiega
Pobiega3mo ago
that wont compile, int.parse isnt int.Parse most programming languages are case sensitive
Brosquare
Brosquare3mo ago
damn /tmp/VvCT1xFkqp.cs(23,49): error CS1002: ; expected /tmp/VvCT1xFkqp.cs(23,49): error CS1513: } expected like am using an online compliler cuz vs code not running any code
Lisa
Lisa3mo ago
C# Online Compiler | .NET Fiddle
Test your C# code online with .NET Fiddle code editor.
Brosquare
Brosquare3mo ago
well guys do have any suggestions for execercises based learning for c#
Pobiega
Pobiega3mo ago
$helloworld
Pobiega
Pobiega3mo ago
https://learn.microsoft.com/en-us/dotnet/csharp/ all these are good resources There is a course platform called Dometrain that currently has their "Intro to C#" course free for the rest of march, so that might be worth checking out if you like video content
Brosquare
Brosquare3mo ago
Thx a lot btw it better if i grinded mainstream C# before unity version?
Pobiega
Pobiega3mo ago
Unity... where do I even begin... So, unity uses C#, but a slightly lower "language version" than "normal modern .NET" thats not really the problem
Brosquare
Brosquare3mo ago
Bro i be lackin monobehavior
Pobiega
Pobiega3mo ago
the problem is that unity just does everything differently. It has a different standard library. It compiles to C++ :d So what is "best practice" in normal C# is terrible in Unity, and vice-versa.
Brosquare
Brosquare3mo ago
so what do i do idk where to start
Pobiega
Pobiega3mo ago
Do you care about learning "normal C#"?
Brosquare
Brosquare3mo ago
and like if i have to code for unity i have to make the whole game
Pobiega
Pobiega3mo ago
or is your only goal of learning this to make a game in Unity?
Brosquare
Brosquare3mo ago
yes
Pobiega
Pobiega3mo ago
If Unity is your end-goal, learn unity maybe learn the absolute basics of C# first, like classes, methods etc but dont go deep
Brosquare
Brosquare3mo ago
ok w3schools? up to where?
Pobiega
Pobiega3mo ago
just be aware! if you do this, you will NOT actually know modern .net development You will not know "normal C#". Thats fine, just keep that in mind
Brosquare
Brosquare3mo ago
so you say if i take the hard path
Pobiega
Pobiega3mo ago
No its two different paths. One isnt harder than the other they just diverge a lot its the same language, but learning the language itself isnt the problem
Brosquare
Brosquare3mo ago
choice's i cant come back to change
Pobiega
Pobiega3mo ago
Unlearning what you already know is hard tbh its doable, but its hard old habits die hard etc
Brosquare
Brosquare3mo ago
i wanna be a full fledged C# 3d dev on unity
Pobiega
Pobiega3mo ago
then thats what you should do
Brosquare
Brosquare3mo ago
unity in a nutshell is just that i have to learn every game dev skill just for programming
Brosquare
Brosquare3mo ago
ok
Pobiega
Pobiega3mo ago
then go learn unity from people who know unity Also, at that point, $unity is where you go for help
Pobiega
Pobiega3mo ago
not here This is a discord for "normal" C# development, not unity
Brosquare
Brosquare3mo ago
am already joined there
Pobiega
Pobiega3mo ago
we have #game-dev where some people who know unity hang around, but they are the experts on unity
Brosquare
Brosquare3mo ago
thank you for you wisdom i think the best way to learn is to join a dev team of begginers and do each part
Pobiega
Pobiega3mo ago
¯\_(ツ)_/¯
Jimmacle
Jimmacle3mo ago
the best way to learn would be to have at least one experienced person to learn from
Want results from more Discord servers?
Add your server
More Posts