C
C#9mo 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
BrosquareOP9mo 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
Lisa9mo 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
MODiX9mo ago
use $tryparse
Lisa
Lisa9mo ago
$tryparse
MODiX
MODiX9mo 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
BrosquareOP9mo ago
i did parse
Pobiega
Pobiega9mo ago
Not in the code you pasted above.
Brosquare
BrosquareOP9mo 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
Pobiega9mo ago
that wont compile, int.parse isnt int.Parse most programming languages are case sensitive
Brosquare
BrosquareOP9mo 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
Lisa9mo ago
C# Online Compiler | .NET Fiddle
Test your C# code online with .NET Fiddle code editor.
Brosquare
BrosquareOP9mo ago
well guys do have any suggestions for execercises based learning for c#
Pobiega
Pobiega9mo ago
$helloworld
Pobiega
Pobiega9mo 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
BrosquareOP9mo ago
Thx a lot btw it better if i grinded mainstream C# before unity version?
Pobiega
Pobiega9mo 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
BrosquareOP9mo ago
Bro i be lackin monobehavior
Pobiega
Pobiega9mo 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
BrosquareOP9mo ago
so what do i do idk where to start
Pobiega
Pobiega9mo ago
Do you care about learning "normal C#"?
Brosquare
BrosquareOP9mo ago
and like if i have to code for unity i have to make the whole game
Pobiega
Pobiega9mo ago
or is your only goal of learning this to make a game in Unity?
Brosquare
BrosquareOP9mo ago
yes
Pobiega
Pobiega9mo 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
BrosquareOP9mo ago
ok w3schools? up to where?
Pobiega
Pobiega9mo 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
BrosquareOP9mo ago
so you say if i take the hard path
Pobiega
Pobiega9mo 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
BrosquareOP9mo ago
choice's i cant come back to change
Pobiega
Pobiega9mo ago
Unlearning what you already know is hard tbh its doable, but its hard old habits die hard etc
Brosquare
BrosquareOP9mo ago
i wanna be a full fledged C# 3d dev on unity
Pobiega
Pobiega9mo ago
then thats what you should do
Brosquare
BrosquareOP9mo ago
unity in a nutshell is just that i have to learn every game dev skill just for programming
Brosquare
BrosquareOP9mo ago
ok
Pobiega
Pobiega9mo ago
then go learn unity from people who know unity Also, at that point, $unity is where you go for help
Pobiega
Pobiega9mo ago
not here This is a discord for "normal" C# development, not unity
Brosquare
BrosquareOP9mo ago
am already joined there
Pobiega
Pobiega9mo ago
we have #game-dev where some people who know unity hang around, but they are the experts on unity
Brosquare
BrosquareOP9mo 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
Pobiega9mo ago
¯\_(ツ)_/¯
Jimmacle
Jimmacle9mo 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