Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to write a simple program in C# to display the sum and the average of the numbers 1 to 100. the output should look like...
the sum is 5050
the average is 50.5
I cant seem to get it to work properly though, the return value only returns the sum not the wording before it, I have tried other ways for it to display the message and sum and ave but they arent working. I am trying to do this using the model view view controller method, but cant understand where I am going wrong and how to get it to display the above result. my code is below.
class SumAndAverage
{
public float sum = 0;
public float ave = 0;
public float SumAndAve()
{
for (int i = 1; i <= 100; i++)
{
sum = sum + i;
ave = sum / i;
}
return sum + ave;
}
}
}
class SumAndAverageController
{
IView view;
SumAndAverage sumAndAverage;
public SumAndAverageController(IView theView, SumAndAverage theSumAndAverage){
view = theView;
sumAndAverage = theSumAndAverage;
}
public void Go()
{
view.Start();
//mAndAverage.SetNumber(view.GetString("Please enter a number"));
view.Show(sumAndAverage.SumAndAve());
//view.Show(sumAndAverage.Result());
view.Stop();
}
}
class ConsoleView : IView
{
public void Start()
{
Console.Clear();
}
public void Stop()
{
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
public void Show<T>(T message)
{
Console.WriteLine(message);
}
}
interface IView
{
void Start();
void Stop();
void Show<T>(T message);
}
class Program
{
static void Main(string[] args)
{
new SumAndAverageController(new ConsoleView(), new SumAndAverage()).Go();
}
}
using System;
using System.Linq;
namespace SumAndAverage
{
class Program
{
static void Main(string[] args)
{
var data = Enumerable.Range(1, 100);
Console.WriteLine("the sum is " + data.Sum());
Console.WriteLine("the average is " + data.Average());
}
}
}
Related
This question already has answers here:
"does not contain a static 'main' method suitable for an entry point"
(9 answers)
Closed 8 days ago.
why i can't run my program it's says
CS5001: exe does not contain a static 'Main' method suitable for an entry point
This is my Code in C#:
using System;
class Human
{
private int _distance = 0;
private int _wordsSpoken = 0;
public void Walk()
{
Console.WriteLine("Walking");
_distance++;
}
public void Speak()
{
string[] phrases = {
"How are you?",
"It is a lovely day. Isn't it?",
"It's a pleasure to meet you.",
"Hello.",
"A pleasant morning to you."
};
Random rand = new Random();
int index = rand.Next(phrases.Length);
Console.WriteLine(phrases[index]);
_wordsSpoken++;
}
public void Hop()
{
Console.WriteLine("Hop");
_distance += 2;
}
public int Distance
{
get { return _distance; }
}
public int WordsSpoken
{
get { return _wordsSpoken; }
}
}
You need to include this in your code:
static void Main(string[] args)
{
// call your first method or whatever you need
}
This method is the starting point for your program and it is required. You would usually put this at the top of your code.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am getting this error:
"The variable 'a' is assigned but its value is never available"
namespace test
{
public class Program
{
static void Main(string[] args)
{
test();
Console.WriteLine(a);
Console.ReadLine();
}
public static void test()
{
int a = 11;
}
}
}
You are talking about "methods" not classes.
You can read more here https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods
But in the mean time, try this:
namespace test
{
public class Program
{
static void Main(string[] args)
{
int a = test();
Console.WriteLine(a);
Console.ReadLine();
}
public static int test()
{
int a = 11;
return a;
}
}
}
class Program
{
static void Main(string[] args)
{
int a = test();
Console.WriteLine(a);
Console.ReadLine();
}
public static int test()
{
int a = 11;
return a;
}
}
You should probably get this error from your code. "The name 'a' does not exist in the current context"
As you do not get the variable "a" out from your method and try to access it.
Try the above and see if it helps.
This is a class and a method, what you have there. NOT 2 classes.
What do you want to do exactly?
The a variable does not exist where you want to print it.
Something like this?
static void Main(string[] args)
{
test();
Console.ReadLine();
}
public static void test()
{
int a = 11;
Console.WriteLine(a);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
recently I've come across a question which is Design an ATM Machine using State Design Pattern an I'm new to design patterns but somehow I've completed the question and designed an ATM Machine using state design pattern in c#.
So, I just want to kindly tell me is there any optimization that can be done on this program, or this solution can be accepted. The current solution is running without any error.
Client Class which controls the state of ATM Machine.
public class Client
{
public static void Main(string [] args)
{
AtmMachine atmMachine = new AtmMachine();
atmMachine.enterPinAndWithdrawMoney();
atmMachine.ejectDebitCard();
atmMachine.insertDebitCardState();
atmMachine.enterPinAndWithdrawMoney();
atmMachine.insertDebitCardState();
atmMachine.ejectDebitCard();
Console.ReadLine();
}
}
ATM Machine Class
public class AtmMachine : IAtmMachineState
{
private IAtmMachineState _state;
public int _balance = 5000;
public int _pin = 1234;
public AtmMachine()
{
_state = new NoDebitCardState();
}
public IAtmMachineState GetMachineState()
{
return _state;
}
public void setAtmMachineState(IAtmMachineState state)
{
this._state = state;
}
public void ejectDebitCard()
{
_state.ejectDebitCard();
if(_state is HasDebitCardState)
{
_state = new NoDebitCardState();
Console.WriteLine("State Changed");
Console.WriteLine(_state.GetType().Name);
}
}
public void enterPinAndWithdrawMoney()
{
if(_state is HasDebitCardState)
{
_state.enterPinAndWithdrawMoney();
}
}
public void insertDebitCardState()
{
if(_state is NoDebitCardState)
{
_state = new HasDebitCardState();
Console.WriteLine("State Changed");
Console.WriteLine(_state.GetType().Name);
}
}
}
ATM Machine Interface
public interface IAtmMachineState
{
void insertDebitCardState();
void ejectDebitCard();
void enterPinAndWithdrawMoney();
}
Has Debit Card State
class HasDebitCardState : IAtmMachineState
{
AtmMachine atmMachine;
public HasDebitCardState()
{
atmMachine = new AtmMachine();
}
public void ejectDebitCard()
{
Console.WriteLine("Debit card ejected successfully");
}
public void enterPinAndWithdrawMoney()
{
Console.WriteLine("Enter the Pin:");
int pin = int.Parse(Console.ReadLine());
if(atmMachine._pin==pin)
{
Console.WriteLine("Enter the amount");
int amount = int.Parse(Console.ReadLine());
if(amount > atmMachine._balance)
{
Console.WriteLine("Money can not be withdrawn because of low balance");
}
else
{
atmMachine._balance = atmMachine._balance - amount;
Console.WriteLine("Please collect your cash");
Console.WriteLine("Remaining Balance {0}", atmMachine._balance);
}
} else
{
Console.WriteLine("Incorrect Pin.");
}
}
public void insertDebitCardState()
{
Console.WriteLine("Debit card is already there, so can not insert debit card");
}
}
No Debit Card State
class NoDebitCardState : IAtmMachineState
{
public void ejectDebitCard()
{
Console.WriteLine("No debit card inside atm");
}
public void enterPinAndWithdrawMoney()
{
Console.WriteLine("No debit card in ATM Machine, so you can not withdraw money");
}
public void insertDebitCardState()
{
Console.WriteLine("Debit Card Inserted");
}
}
You might want to consider using an abstract class instead of an interface.
Instead of an abstract class you could also used default implementations but this will limit you in using pre defined members and state as in your abstract class.
This way you can have some standard functionality and avoid re writing the same code multiple times.
Override only the functionality that changes per state.
You can also easily extend by calling the base implementation of the abstract class as well as the extra logic in each state.
I am writing a simple program in c# which asks the user to enter a number, then tells the user if the number is odd or even. my program works however wheni first enter the number nothing happens, i have to enter the number twice and then it tells me if the number is odd or even, im not very good at using the mvvc technique, so if anyone knows why this is happening and could help me that would be great.my code is below...
class CheckNumber
{
protected String number;
public void SetNumber(String newNumber)
{
number = newNumber;
}
public int Number()
{
int number = Convert.ToInt32(Console.ReadLine());
if (number % 2 == 1) //(number % 2 == 0) would test for even numbers(0 remainder)
{
Console.WriteLine("Odd number");
}
else
{
Console.WriteLine("Even number");
}
return number;
}
}
class CheckNumberController
{
IView view;
CheckNumber checkNumber;
public CheckNumberController(IView theView, CheckNumber theCheckMark)
{
view = theView;
checkNumber = theCheckMark;
}
public void Go()
{
view.Start();
checkNumber.SetNumber(view.GetString("Please enter a number"));
view.Show(checkNumber.Number());
view.Stop();
}
}
class ConsoleView : IView
{
public void Start()
{
Console.Clear();
}
public void Stop()
{
Console.WriteLine("Press any key to finish");
Console.ReadKey();
}
public String GetString(String prompt = "")
{
Console.WriteLine(prompt);
return Console.ReadLine();
}
public Int32 GetInt(String prompt = "")
{
Console.WriteLine(prompt);
return Int32.Parse(Console.ReadLine());
}
public void Show<T>(T message)
{
Console.WriteLine(message);
}
}
interface IView
{
void Start();
void Stop();
String GetString(String prompt);
Int32 GetInt(String prompt);
void Show<T>(T message);
}
class Program
{
static void Main(string[] args)
{
new CheckNumberController(new ConsoleView(), new CheckNumber()).Go();
}
}
You're reading input twice. Firstly in the CheckNumberController.Go()
checkNumber.SetNumber(view.GetString("Please enter a number"));
And secondly in CheckNumber.Number()
int number = Convert.ToInt32(Console.ReadLine());
The latter should be:
int number = Convert.ToInt32(this.number);
As you want to work on the value you've already read and set, not an additional one
public String GetString(String prompt = "")
{
Console.WriteLine(prompt);
//return Console.ReadLine();
return "error is here";
}
When calling GetString() method your again trying to get an input. Just comment and return string if you want.
The key is in this method:
public void Go()
{
view.Start();
checkNumber.SetNumber(view.GetString("Please enter a number"));
view.Show(checkNumber.Number());
view.Stop();
}
SetNumber(string) sets the protected field number in the CheckNumber class. However, when you call view.Show<T>(T), you are calling the Number() method on the CheckNumber class, which ignores the stored variable and reads from the console again.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to develop a class library in C# whose methods I would call in a hierarchical way, like this:
using MyProduct.Common;
protected void Page_Load(object sender, EventArgs e)
{
Utils myUtils = new Utils();
int res = myUtils.Numbers.Add(1, 2);
string text = myUtils.Text.ToUpper("test"); //***
}
Is it possible to do, using Namespaces, classes, interfaces, whatever?
UPDATE:
I have my library like this, but it doesn't work, as I can't "reference a type through an expression" (line with *** above)
namespace MyProduct.Common
{
public class Utils
{
public static class Text
{
public static string ToUpper(string s)
{
return s.ToUpper();
}
}
}
}
UPDATE 2:
Ok, it seems I need to clarify...
I have this MyProduct.Common.dll:
namespace MyProduct.Common
{
public static class Utils
{
public static class Text
{
public static void ToUpper(string s)
{
return s.ToUpper();
}
}
public static class Number
{
public static void Add(int a, int b)
{
return (a + b);
}
}
}
}
and I have my project:
using MAD.Comum;
...
protected void Page_Load(object sender, EventArgs e)
{
string x = Utils.Text.ToUpper("aa"); //this works
string res = Utils.Number.Add(1, 2); //this works
}
and now 2 questions:
- is it possible to separate classes Number and Text in 2 files?
- is this a good design?
Of course, you can do something like this:
namespace MyProduct.Common.Utils
{
public static class Numbers
{
public static int Add(int n, int m)
{
return n + m;
}
}
public static class Text
{
public static string ToUpper(string str)
{
return str.ToUpper();
}
}
}
And you use it like this:
MyProduct.Common.Utils.Numbers.Add(1, 2);
Hope this helps.
Cheers