I'm a uni student just starting to learn c#. I'm sure there is a simple solution to this but I have searched and I don't think know enough yet.
this is my program, note that I have not finished a few functions.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
public void welcome()
{
Console.WriteLine("Fuel Consumption Calculator "+"r/n"+"Are you using Metric 1 or Imperial 2 ?");
}
public void check()
{
string choice;
choice = Console.ReadLine();
if (choice == "1")
{
calcmetric();
}
else
{
calcimperial();
}
}
public void calcmetric()
{
}
public void calcimperial()
{
}
}
}
}
In Visual Studio I have two errors: one saying a '}' is expected after Main; and there is an error at the very end saying "type or namespace definition error".
You are declaring methods inside a method. This is wrong.
Change it:
class Program
{
static void Main(string[] args)
{
//call other methods here
welcome();
check();
//....
}
public static void welcome()
{
Console.WriteLine("Fuel Consumption Calculator "+"r/n"+"Are you using Metric 1 or Imperial 2 ?");
}
public static void check()
{
string choice;
choice = Console.ReadLine();
if (choice == "1")
{
calcmetric();
}
else
{
calcimperial();
}
}
public static void calcmetric()
{
}
public static void calcimperial()
{
}
}
Related
I'm new to C#.
I'm creating a small program that input number by a user returns to the number and display it.
It works but a user is asked the same questions twice.
I just need one.
I'd appreciate if anyone help me with that.
Here is output of the program.
Here is my programming.
Class1
using System;
using System.Numerics;
using System.Reflection.Emit;
using static System.Console;
using System.IO;
namespace aaaa
{
public class Program
{
public static void Main(string[] args)
{
TryReturn tryreturn = new TryReturn();
tryreturn.template();
}
}
}
Class2
using System;
using System.Numerics;
using System.Reflection.Emit;
using static System.Console;
using System.IO;
namespace aaaa
{
public class TryReturn
{
public int entryNum;
public void template()
{
returnyNumber();
DisplayNumber();
}
public void DisplayNumber()
{
entryNum = returnyNumber();
WriteLine("number is " + entryNum);
Read();
}
public int returnyNumber()
{
int entryNum;
Write("Choose 1 or 2 >> ");
do
{
int.TryParse(ReadLine(), out entryNum);
if (entryNum == 1 || entryNum == 2)
{
break;
}
else
{
Write("Invalid entry! Enter either '1' or '2' >> ");
}
}
while (true);
return entryNum;
}
}
}
//remove the call of returnyNumber from template as you are calling it from DisplayNumber
public void template()
{
// returnyNumber();
DisplayNumber();
}
public void DisplayNumber()
{
entryNum = returnyNumber();
WriteLine("number is " + entryNum);
Read();
}
A quick brief of the program that i have already done.
Golfer: A Golfer can be equipped with a Golf Club using the PickUp method; the golf club is
then stored in the Golfer’s club field. The Golfer’s holding property is true when its golf club
field is not null. When the Golfer is told to Swing, the following occurs:
If the Golfer is holding a golf club:
“Breathe and focus” is printed to the console. (b) The Golfer tells its club to swing.
Else…
“Where is my caddy?” is printed to the console.
GolfClub: Any golf club that can be swung; a GolfClub can also be held by a Golfer. GolfClub
is an abstract base class.
Putter: A Putter is a GolfClub that, when swung, prints “putt putt putt”.
SandWedge: A SandWedge is a GolfClub that can be swung 5 times before it is thrown in
frustration. (The constructor sets the swing counter to 5 and calling the Throw method resets
the play counter to 5.) When a SandWedge is swung, it does one of two things: if the remaining swing count is larger than zero, it prints “I am in my happy place” and the swing count is
decremented by one; otherwise it prints “hand me my hockey stick”.
My problem:
Code that uses a Golfer object may, therefore, ask for a specific golf club to be operated by referring
to that GolfClub by name. I'm not expected to get input from the user, just embedding it directly in Program.cs is totally fine. So basically a golfer wants to play, either select putter or sandwedge.
My code:
Program.cs
namespace Test
{
public class Program
{
private enum GolfKind
{
Putter, SandWedge
}
public static void Main(string[] args)
{
Golfer golfer = new Golfer();
Putter putt = new Putter();
SandWedge sandwedge = new SandWedge();
golfer.PickUp(putt);
golfer.PickUp(sandwedge);
if (golfer.Holding == true)
{
Console.WriteLine("Breate and Focus");
golfer.Swing(putt);
golfer.Swing(sandwedge);
}
else
{
Console.WriteLine("Where is my Caddy?");
}
Console.ReadLine();
}
}
}
GolfClub.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
public abstract class GolfClub
{
public GolfClub() { }
public abstract void Swing();
}
}
Golfer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
public class Golfer
{
private List<GolfClub> _clubs;
public Golfer()
{
_clubs = new List<GolfClub>();
}
public bool Holding
{
get
{
if(_clubs.Count() != 0)
{
return true;
}
else
{
return false;
}
}
}
public void PickUp(GolfClub club)
{
_clubs.Add(club);
}
public void Swing(GolfClub club)
{
foreach (GolfClub gc in _clubs)
{
if (gc == club)
{
gc.Swing();
}
}
}
}
}
SandWedge.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
public class SandWedge : GolfClub
{
private int _count;
public SandWedge()
{
_count = 5;
}
public override void Swing()
{
while(_count > 0)
{
Console.WriteLine("I am in my happy place");
_count--;
}
if (_count == 0)
{
Console.WriteLine("hand me my hockey stick");
}
}
public void Throw()
{
_count = 5;
}
}
}
Putter.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
public class Putter : GolfClub
{
public Putter () {}
public override void Swing()
{
Console.WriteLine("putt putt putt");
}
}
}
You can use a IDictionary<string, GolfClub> to associate a name with a golf club. Provide the name as a new argument for the PickUp() method. Then use a string argument in the Swing() method to find the right club.
public class Golfer
{
private readonly IDictionary<string, GolfClub> _clubs = new Dictionary<string, GolfClub>();
// [...]
public void PickUp(GolfClub club, string name) {
_clubs.Add(name, club);
}
public void Swing(string name) {
_clubs[name].Swing();
}
}
Consider the following console program. It has four classes: Program, Attacker, Defender, and Helper. I want to remove the logic from the Defender class and use delegates to call helpers. I've spent some time on this and can't quite get it.
Where do I declare my delegate: in Program or in Defender?
Where do I instantiate my delegate: in Program or in Defender?
Where do I subscribe my delegate: in Program or in Helper?
I could post my attempts but it wouldn't be helpful.
using System;
namespace Delegates19
{
public class Program
{
static void Main(string[] args)
{
Attacker a = new Attacker();
string weapon = "sword";
a.Attack(weapon);
Defender d = new Defender();
d.Help(weapon);
weapon = "spear";
a.Attack(weapon);
d.Help(weapon);
}
}
public class Attacker
{
public void Attack(string s)
{
Console.WriteLine($"Attacker attacks with {s}");
}
}
public class Defender
{
public void Help(string s)
{
Console.WriteLine($"Defender is attacked with {s} and calls for help");
if (s == "sword")
Helper.Knight();
if (s == "spear")
Helper.Bowman();
}
}
public class Helper
{
public static void Knight()
{
Console.WriteLine($"Knight charges Attacker");
}
public static void Bowman()
{
Console.WriteLine($"Bowman shoots Attacker");
}
}
}
I'm really not sure why you would want to do this, but these both work and might give you ideas.
Unless I understand your need for events/delegates I actually prefer your code over these.
Delegates:
public class Defender
{
private static Action SwordAction = Helper.Knight;
private static Action SpearAction = Helper.Bowman;
public void Help(string s)
{
Console.WriteLine($"Defender is attacked with {s} and calls for help");
if (s == "sword")
SwordAction();
if (s == "spear")
SpearAction();
}
}
public static class Helper
{
public static void Knight()
{
Console.WriteLine($"Knight charges Attacker");
}
public static void Bowman()
{
Console.WriteLine($"Bowman shoots Attacker");
}
}
Events:
static void Main(string[] args)
{
using (var d = new Defender())
{
d.GetHelp += StandardDefenseHelp.Defender_GetHelp;
d.Help("sword");
d.Help("spear");
}
}
public class Defender : IDisposable
{
public event EventHandler<string> GetHelp;
private void RaiseGetHelp(string weapon) => GetHelp?.Invoke(this, weapon);
public void Help(string weapon)
{
Console.WriteLine($"Defender is attacked with {weapon} and calls for help");
RaiseGetHelp(weapon);
}
public void Dispose()
{
GetHelp = null;
}
}
public static class StandardDefenseHelp
{
public static void Defender_GetHelp(object sender, string weapon)
{
if (weapon == "sword")
Knight();
if (weapon == "spear")
Bowman();
}
private static void Knight()
{
Console.WriteLine($"Knight charges Attacker");
}
private static void Bowman()
{
Console.WriteLine($"Bowman shoots Attacker");
}
}
Important: events often are the cause of memory leaks, that's why Defender is now disposable.
The above design could be useful if you have multiple "DefenseHelp" types and other things also should happen when a defender needs help that the defender itself needs to know nothing about.
But I'd only do this if it gave some benefit. I believe in the KISS development methodology.
class Program
{
public static string playerName;
static void Main(string[] args)
{
playerName = Console.ReadLine();
}
public static void userInterface()
{
Console.Writeline("Name:" + playerName)
}
}
Been trying to understand where im falling short for a few hours now and cannot figure it out, wondering if any of the SO residents can help me.
Im trying to display a inputted username in a GUI using C# console, I have defined it as a public variable in the class and called it in the method, however its throwing me this exception and displaying a null value?
Any help is appreciated.Class and Main The method im trying to call the variable to
EDIT the desired aim is to have the program display the users inputted username in the UI at the top of the console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Player player = new Player
{
Name = "name coming from your input class"
};
UserInterface userInterface = new UserInterface(player);
}
}
class UserInterface
{
public UserInterface(Player player)
{
Console.SetWindowSize(220, 55);
Console.WriteLine("Name: {0}", player.Name);
}
}
class Player
{
public string Name { get; set; }
}
}
Or something alike. So you need to provide values for your variables.
Just call the method userInterface() after this line playerName = Console.ReadLine();
this will display the accepted value on console.
trying to make a rpn calculator in c# not sure why its not working. is there a error in my program or class file?
class Program
{
static void Main(string[] args)
{
IntStack mystack = new IntStack();
mystack.Push(10);
System.Console.WriteLine(mystack.Pop());
mystack.Push(20);
mystack.Push(30);
mystack.Push(40);
System.Console.WriteLine(mystack.Pop());
System.Console.WriteLine(mystack.Pop());
System.Console.WriteLine(mystack.Pop());
System.Console.ReadKey();
}
}
Try following modifications :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IntStack mystack = new IntStack();
mystack.Push(10);
System.Console.WriteLine(mystack.Pop());
mystack.Push(20);
mystack.Push(30);
mystack.Push(40);
System.Console.WriteLine(mystack.Pop());
System.Console.WriteLine(mystack.Pop());
System.Console.WriteLine(mystack.Pop());
System.Console.ReadKey();
}
}
public class IntStack
{
private List<int> stack = new List<int>();
public int? Pop()
{
if (stack.Count == 0) return null;
int value = stack[0];
stack.RemoveAt(0);
return value;
}
public void Push(int value)
{
stack.Insert(0, value);
}
}
}