Login to ZARP
|
Doing C# at school and wanted to get ahead of the game a bit, but i can't seem to get my code to work. It's meant to be a calculator, but when i enter the first number, the program closes :/
Warning: Spoiler! [ Click to expand ][ Click to hide ] using System;
namespace Calculator
{
class Program
{
static void Main(string[] args)
{
int num1;
int num2;
int num3 = 1;
string method;
Console.WriteLine("Enter First Number..");
num1 = Convert.ToInt32(Console.Read());
Console.WriteLine("Enter Operation..");
method = Convert.ToString(Console.Read());
Console.WriteLine("Enter Second Number..");
num2 = Convert.ToInt32(Console.Read());
switch (method)
{
case "x":
num3 = num1 * num2;
break;
case "/":
num3 = num1 / num2;
break;
case "+":
num3 = num1 + num2;
break;
case "-":
num3 = num1 - num2;
break;
}
Console.WriteLine("{0} {1} {2} = {3}", num1, method, num2, num3);
Console.ReadLine();
}
}
} Just wondering if anyone knows why. Thanks. |
|
Login or register to post a reply.
|
Good luck finding it!
|
|
Login or register to post a reply.
|
ASK Josh deadmonstor
|
|
Login or register to post a reply.
|
I have never programmed in C# before but I rewrote your entire program cause it was aids, keep it more simple next time with numbers instead of converting shit to strings (strings are really slow usually).
Warning: Spoiler! [ Click to expand ][ Click to hide ] using System;
namespace Calculator
{
public class Program
{
public static void Main(string[] args)
{
int num1;
int num2;
int sum = 0;
int method;
Console.WriteLine("Enter First Number..");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Second Number..");
num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Operation: 0 = Multiplication, 1 = Division, 2 = Sum, 3 = Subtraction");
method = Convert.ToInt32(Console.ReadLine());
switch (method)
{
case 0:
sum = num1 * num2;
break;
case 1:
sum = num1 / num2;
break;
case 2:
sum = num1 + num2;
break;
case 3:
sum = num1 - num2;
break;
}
Console.WriteLine("Answer is = {0}" , sum);
Console.ReadLine();
}
}
} |
|
ex-minecraft owner, zarp legend (Was there during the infancy of Zarp), ex-ssrp admin, ex-teamspeak emblem (For SSRP admin & minecraft owner)
Last Edit: 6 years 2 months ago by catboy sven ツ. Reason: i'm a shit coder basically
Login or register to post a reply.
|
Agent Aspect wrote:
I have never programmed in C# before but I rewrote your entire program cause it was aids, keep it more simple next time with numbers instead of converting shit to strings (strings are really slow usually). Warning: Spoiler! [ Click to expand ][ Click to hide ] using System;
namespace Calculator
{
public class Program
{
public static void Main(string[] args)
{
int num1;
int num2;
int sum = 0;
int method;
Console.WriteLine("Enter First Number..");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Second Number..");
num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Operation: 0 = Multiplication, 1 = Division, 2 = Sum, 3 = Subtraction");
method = Convert.ToInt32(Console.ReadLine());
switch (method)
{
case 0:
sum = num1 * num2;
break;
case 1:
sum = num1 / num2;
break;
case 2:
sum = num1 + num2;
break;
case 3:
sum = num1 - num2;
break;
}
Console.WriteLine("Answer is = {0}" , sum);
Console.ReadLine();
}
}
} nvm, it's probably because i'm a massive dribble and was using Read instead of ReadLine. |
|
Last Edit: 6 years 2 months ago by bunnyslippers69.
Login or register to post a reply.
|
Bunnyslippers69 wrote:
Agent Aspect wrote:
I have never programmed in C# before but I rewrote your entire program cause it was aids, keep it more simple next time with numbers instead of converting shit to strings (strings are really slow usually). Warning: Spoiler! [ Click to expand ][ Click to hide ] using System;
namespace Calculator
{
public class Program
{
public static void Main(string[] args)
{
int num1;
int num2;
int sum = 0;
int method;
Console.WriteLine("Enter First Number..");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Second Number..");
num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Operation: 0 = Multiplication, 1 = Division, 2 = Sum, 3 = Subtraction");
method = Convert.ToInt32(Console.ReadLine());
switch (method)
{
case 0:
sum = num1 * num2;
break;
case 1:
sum = num1 / num2;
break;
case 2:
sum = num1 + num2;
break;
case 3:
sum = num1 - num2;
break;
}
Console.WriteLine("Answer is = {0}" , sum);
Console.ReadLine();
}
}
} nvm, it's probably because i'm a massive dribble and was using Read instead of ReadLine. You also have to use public, never programmed in C# before tho so I'm sure Max could prove me wrong... |
|
ex-minecraft owner, zarp legend (Was there during the infancy of Zarp), ex-ssrp admin, ex-teamspeak emblem (For SSRP admin & minecraft owner)
Login or register to post a reply.
|
Agent Aspect wrote:
Bunnyslippers69 wrote:
Agent Aspect wrote:
I have never programmed in C# before but I rewrote your entire program cause it was aids, keep it more simple next time with numbers instead of converting shit to strings (strings are really slow usually). Warning: Spoiler! [ Click to expand ][ Click to hide ] using System;
namespace Calculator
{
public class Program
{
public static void Main(string[] args)
{
int num1;
int num2;
int sum = 0;
int method;
Console.WriteLine("Enter First Number..");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Second Number..");
num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Operation: 0 = Multiplication, 1 = Division, 2 = Sum, 3 = Subtraction");
method = Convert.ToInt32(Console.ReadLine());
switch (method)
{
case 0:
sum = num1 * num2;
break;
case 1:
sum = num1 / num2;
break;
case 2:
sum = num1 + num2;
break;
case 3:
sum = num1 - num2;
break;
}
Console.WriteLine("Answer is = {0}" , sum);
Console.ReadLine();
}
}
} nvm, it's probably because i'm a massive dribble and was using Read instead of ReadLine. You also have to use public, never programmed in C# before tho so I'm sure Max could prove me wrong... I probably should of mentioned the fact that i've only done 1 lesson on C# and have no clue what most things are, thanks for the help. |
|
Login or register to post a reply.
|