Hello, I have been programming for a few months now, but not in C#. I am having trouble with the last part of this assignment.
Create a class called "SimpleCalc" with two
private members of type double and the following methods:
addNumbers(double a, double b)
subtractNumbers(double a, double b)
multiplyNumbers(double a, double b)
divideNumbers(double a, double b)
Utilizing the class above, you will then rewrite one
of the programs from week #2, which was to write console C# program
that will prompt the user for an integer, read it in and store it in a
variable, prompt the user for a 2nd integer, store it in a variable then
display all of the results, prompt the user for an operator (+, -, *,
/, %) , and store it in a variable. The operator will determine the
arithmetic performed and display the result. After displaying the
result, the program will ask the user if he/she wants to run again, if
the user wants to, it will start all over and the process repeats.
Include any necessary error handling.
I am not asking for someone to do my homework, but when I added in a method i have just completely hit a wall. I have no clue what I am doing. Methods in C# are very confusing to me. Any help is needed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
class SimpleCalc
{
private double a;
private double b;
public double addNumbers(double a, double b)
{
return a + b;
}
static void Main(string[] args)
{
string userxInput;
string useryInput;
string operatorInput;
string startOver;
int x, y, result;
float floatresult;
do
{
Console.Write("Please enter integer x: ");
userxInput = Console.ReadLine();
x = Int32.Parse(userxInput);
Console.Write("Enter integer y: ");
useryInput = Console.ReadLine();
y = Int32.Parse(useryInput);
Console.Write("Enter an operator for arithmetic to be performed: ");
operatorInput = Console.ReadLine();
if (operatorInput == "+")
{
result = x + y;
Console.WriteLine("x+y: {0}", result);
}
else if (operatorInput == "-")
{
result = x - y;
Console.WriteLine("x-y: {0}", result);
}
else if (operatorInput == "*")
{
result = x * y;
Console.WriteLine("x*y: {0}", result);
}
else if (operatorInput == "/")
{
floatresult = (float)x / (float)y;
Console.WriteLine("x/y: {0}", floatresult);
}
else if (operatorInput == "%")
{
result = x % y;
Console.WriteLine("x%y: {0}", result);
}
Console.WriteLine("Would you like to try another operation? yes/no? ");
startOver = Console.ReadLine();
} while (startOver == "yes");
Console.ReadLine();
}
}
}
Loading
theLizardPosted Jul 6, 2010, 12:32 AM
count should be count = 0;
while (count != 11) should be while (count != 10)
count = 0;
while (count != 10) //this is the same as saying while(number of elements != 11) because arrays are zero based.
{
Console.Write("Please enter player " + count.ToString() + "Score: ");
x = Console.ReadLine(); //where are you storing x?
//do you mean to do this
score[count] = Console.ReadLine();
count++;
}
you should only do a while loop if the number of elements will be unknown, if you know there are 10 or 11 scores needed then a for loop is fine but not both.
Other than what i have mentioned there is no reason why it should scroll like crazy!!!
MaxPosted Aug 12, 2010, 7:07 PM
theLizardPosted Jul 30, 2010, 8:15 PM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string b = "______________________________________________________________________________________";
int charcount = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
b = "______________________________________________________________________________________";
lb.Text = "";
b = b.Substring(0, charcount);
teWordToGuess.Visible = false;
teLetter.Focus();
}
private void teLetter_TextChanged(object sender, EventArgs e)
{
string s = teWordToGuess.Text;
string a = teLetter.Text;
teLetter.Text = "";
string c = "";
for (int i = 0; i < s.Length; i++)
{
if (s[i].ToString() == a)
{
c += a;
}
else
c += b[i];
}
b = c;
lb.Text = b;
}
private void te_KeyUp(object sender, KeyEventArgs e)
{
charcount = teWordToGuess.Text.Length;
lb1.Text = charcount.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
teWordToGuess.Visible = true;
teWordToGuess.Text = "";
teWordToGuess.Focus();
}
}
}
Sam HobbsPosted Jul 28, 2010, 8:46 PM
MaxPosted Jul 28, 2010, 6:41 PM
MaxPosted Jul 10, 2010, 12:39 AM
Write a console program similar to 'Hangman':
- Create an array
of 10 words (you decide the words).
- Randomly select a
word from the array.
- Then prompt the
user to guess a letter of the word
- Then if the
user's letter guesses matches any letter(s) in the word, then put the
letter guessed into the proper indices of another array and display the
contents at this point to the user.
- Repeat till
either all letters are guessed or the user misses 3 times.
Include any necessary error handling.MaxPosted Jul 5, 2010, 11:43 PM
Here is what I have now.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication11
{
class scores
{
static void Main(string[] args)
{
int[] score = new int[11]; //this can also be an unknown size
int count = 1;
int total = 0, mean;
string x;
for(int i = 0; i < score.Length; i++) // will print ALL scores
{
//for unknown array size
Array.Resize(ref score, score.Length + 1); //ad one element to the score array
//in this case the for loop would become a while loop and you would test for a key to terminate the for loop
while (count != 11)
{
Console.Write("Please enter player " + count.ToString() + "Score: ");
x = Console.ReadLine();
count++;
}
Console.WriteLine("Player " + i.ToString() + " : " + score[i].ToString());
Console.WriteLine("Total for all players : " + total.ToString()); //will print total
mean = total / score.Length;
Console.WriteLine("Average Score: " + mean.ToString());// will print mean (Average) score
}
}
}
}
I am able to input all the scores, but after I put in all the scores it just begins scrolling like crazy. I feel like something minor is out of order here...
MaxPosted Jul 5, 2010, 11:06 PM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
int[] score = new int[11]; //this can also be an unknown size
int total, mean;
Console.WriteLine("Score for each player");
for(int i = 0; i < score.Length; i++) // will print ALL scores
{
//for unknown array size
Array.Resize(ref score, score.Length +1); //add one element to the score array
//in this case the for loop would become a while loop and you would test for a key to terminate the for loop
Console.WriteLine("Player " + i.ToString() + " : " + score[i].ToString());
**total** += score[i];
}
Console.WriteLine("Total for all players : " + total.ToString()); //will print total
mean = total / score.Length;
Console.WriteLine("Average Score: " + mean.ToString());// will print mean (Average) score
}
}
}
The only error I am getting now is the "total" that I put stars around. The error is a "use of unassigned local variable" error. I cleaned up the code because I had a few of those errors, but I can't seem to get rid of this one. Also, should I be combining this code with the code from earlier? Or just using this second half of code since we eliminated the player array? Thanks again Lizard! You have been awesome!
theLizardPosted Jul 5, 2010, 5:42 PM
If you do not need the player name then for this exercise all you would need is an array of of int
int[] score = new int[11]; //this can also be an unknown size
to display the score's
int total, mean;
Console.WriteLine("Score for each player")??
for(int i = 0; i < score.length; i++) // will print ALL scores
{
//for unknown array size
Array.Resize(ref score, score.Length +1) //ad one element to the score array
//in this case the for loop would become a while loop and you would test for a key to terminate the for loop
Console.WriteLine("Player " + i.ToString() + " : " + score[i].ToString)??
total += score[i];
}
Console.WriteLine("Total for all players : " + total.ToString) //will print total
mean = total / score.length;
Console.WriteLine("Average Score: " + mean.ToString) // will print mean (Average) score
MaxPosted Jul 5, 2010, 2:29 AM
MaxPosted Jul 5, 2010, 2:25 AM
Console.WriteLine(player[])??
Sorry. I promise no more questions after I finish this last assignment!!
MaxPosted Jul 5, 2010, 2:02 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication11
{
class player
{
string name;
int score;
static void Main(string[] args)
{
player[] players = new player[12];
int count = 1;
while (count != 11)
{
Console.Write("Please enter player " + count.ToString() + " Name: ");
players[count - 1].name = Console.ReadLine();
Console.Write("Please enter player " + count.ToString() + "Score: ");
players[0].score = Console.ReadLine();
count++;
}
}
}
}
Now I am getting an error because the score needs to be converted but I don't know how to do the Int32.Parse() when it comes to the array...
theLizardPosted Jul 4, 2010, 11:01 PM
have a class of players (this can also be a struct)
class player
{
string name;
int score;
...
...
}
player[] players = new player[10];
int count = 1;
while(count != 10)
{
Console.Write("Please enter player " + count.ToString() + " Name: ");
players[count -1].name = Console.ReadLine();
Console.Write("Please enter player " + count.ToString() + "Score: ");
players[0].score = Console.ReadLine();
count ++;
}
then do your other stuff..
MaxPosted Jul 4, 2010, 12:53 PM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
string scoreInput;
int x;
int[] scores = new int[10];
Console.Write("Please enter player 1 score: ");
scoreInput = Console.ReadLine();
x = Int32.Parse(scoreInput);
}
}
}
MaxPosted Jul 4, 2010, 2:23 AM
Console.WriteLine(SimpleCalc.addNumbers(x, y).ToString()); to Console.WriteLine(calc.addNumbers(x, y).ToString());
seems to compile just fine now! thanks for your help, I will post any other issues I have!
MaxPosted Jul 4, 2010, 2:19 AM
SimpleCalc calc = new SimpleCalc();
where you said but I still have the same error... Thanks for your help, i really don't know why I can't get this! arggg
theLizardPosted Jul 4, 2010, 1:29 AM
{
string userxInput;
string useryInput;
string operatorInput;
string startOver;
int x, y, result;
float floatresult;
//OOPS, I forgot to instantiate the class
do this in main here
SimpleCalc calc = new SimpleCalc();
then
if (operatorInput == "+")
{
Console.WriteLine(calc.addNumbers(x, y).ToString());
}
Note: I am not testing these so you may need to ask more as you need to.
MaxPosted Jul 4, 2010, 1:03 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
class SimpleCalc
{
private double a;
private double b;
public double addNumbers(double a, double b)
{
return a + b;
}
public double subtractNumbers(double a, double b)
{
return a - b;
}
public double multiplyNumbers(double a, double b)
{
return a * b;
}
public double divideNumbers(double a, double b)
{
return a / b;
}
public double modulusNumbers(double a, double b)
{
return a % b;
}
}
class testCalc
{
static void Main(string[] args)
{
string userxInput;
string useryInput;
string operatorInput;
string startOver;
int x, y, result;
float floatresult;
do
{
Console.Write("Please enter integer x: ");
userxInput = Console.ReadLine();
x = Int32.Parse(userxInput);
Console.Write("Enter integer y: ");
useryInput = Console.ReadLine();
y = Int32.Parse(useryInput);
Console.Write("Enter an operator for arithmetic to be performed: ");
operatorInput = Console.ReadLine();
if (operatorInput == "+")
{
Console.WriteLine(SimpleCalc.addNumbers(x, y).ToString());
}
else if (operatorInput == "-")
{
Console.WriteLine(SimpleCalc.subtractNumbers(x, y).ToString());
}
else if (operatorInput == "*")
{
Console.WriteLine(SimpleCalc.multiplyNumbers(x, y).ToString());
}
else if (operatorInput == "/")
{
floatresult = (float)x / (float)y;
Console.WriteLine(SimpleCalc.divideNumbers(x, y).ToString());
}
else if (operatorInput == "%")
{
Console.WriteLine(SimpleCalc.modulusNumbers(x, y).ToString());
}
Console.WriteLine("Would you like to try another operation? yes/no? ");
startOver = Console.ReadLine();
} while (startOver == "yes");
Console.ReadLine();
}
}
}
The error is: Error 2 An object reference is required for the non-static field, method, or property 'ConsoleApplication5.SimpleCalc.subtractNumbers(double, double)'
I get the error for each method
theLizardPosted Jul 3, 2010, 8:39 PM
If I am interpreting this correctly I think you are on the right track, to push you along the class you have "SimpleCalc" is on target you need to add the other methods like
DivideNumbers(double x, double y)
{
return(x / y);
}
the other thing is that you are not using the class in your Main, what you should be doing is
if (operatorInput == "+")
{
Console.WriteLine(SimpleCalc.AddNumbers(x, y).ToString());
}
if (operatorInput == "/")
{
Console.WriteLine(SimpleCalc.DivideNumbers(x, y).ToString());
}
See where I am going? hope this helps...
Sam HobbsPosted Jul 3, 2010, 4:45 PM