Hi i was just wondering if anyone can show me how to use an array with mabye a boolean or something to either end restart last function or start from the very beginning of my Ohm's Law Caculator. Here is my code currently. Im kinda on a time crunch so thank you in advance.
using System;
using System.Threading;
namespace ENGR115_CourseProject_OctTerm
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.WriteLine(" Hello my name is Zachari Pryor ");
Console.WriteLine(" This is the Course Programming Project: Ohm's Law ");
Console.WriteLine(" The point of the term course project is to use C#");
Console.WriteLine(" to have user solve Ohm's law using different variables of their choosing.");
Console.WriteLine(" Press any key to move on");
Console.ReadLine();
Console.Clear();
enterName();
Console.ReadLine();
}
static void enterName()
{
Console.Write(" Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine(" Hello it is very nice to meet you " name);
Console.WriteLine(" Are you ready to use C# to solve Ohm's Law (y or n) ? ");
string y = Console.ReadLine();
Console.WriteLine(" Great!!, well then lets get started.Click enter");
Console.ReadLine();
Console.Clear();
Equation();
Console.ReadLine();
}
static void Equation()
{
Console.Write("What would you like to find? V,I, or R:");
string unitToFind = Console.ReadLine();
if (unitToFind == "V")
{
Console.WriteLine("Enter the Resistance as a decimal.");
double R;
while (!double.TryParse(Console.ReadLine(), out R)) ;
Console.WriteLine("Enter the Amperage as a decimal number");
double I;
while (!double.TryParse(Console.ReadLine(), out I)) ;
double V = R * I;
Console.WriteLine("Voltage is: " V.ToString() " Volts");
}
else if (unitToFind == "I")
{
Console.WriteLine("Enter the Voltage as a decimal number");
double V;
while (!double.TryParse(Console.ReadLine(), out V)) ;
Console.WriteLine("Enter the Resistance as a decimal number");
double R;
while (!double.TryParse(Console.ReadLine(), out R)) ;
double I = V / R;
Console.WriteLine("Amperage is: " I.ToString() " Amps");
}
else if (unitToFind == "R")
{
Console.WriteLine("Enter the Voltage as a decimal number");
double V;
while (!double.TryParse(Console.ReadLine(), out V)) ;
Console.WriteLine("Enter the Amperage as a decimal number");
double I;
while (!double.TryParse(Console.ReadLine(), out I)) ;
double R = V / I;
Console.WriteLine("Resistance is: " R.ToString() " Ohms");
}
else
{
** Console.WriteLine("You need to enter either V, I or R. Please run the program again.");
Console.WriteLine("Please enter 0 to continue working problems");
Console.WriteLine("1 to quit");
Console.WriteLine("2 to restart entire program. ");
}
int[] actionTake = { 0, 1, 2 };
{
Console.Write(actionTake =[1]);
Equation();
Console.Write(actionTake[0]);
Console.ReadLine();
}
**
I want the code to start back over from the beginning of the the equation(); method if my 0 array is used then if 1 is used to end and close program and 2 i want it to start completley over. and if possible think of some ways i can use arrays in other areas of the Program.
CodePudding user response:
namespace ENGR115_CourseProject_OctTerm
{
internal class Program
{
public static int answer { get; set; }
static void Main(string[] args)
{
do
{
Console.WriteLine("Hello World!");
Console.WriteLine(" Hello my name is Zachari Pryor ");
Console.WriteLine(" This is the Course Programming Project: Ohm's Law ");
Console.WriteLine(" The point of the term course project is to use C#");
Console.WriteLine(" to have user solve Ohm's law using different variables of their choosing.");
Console.WriteLine(" Press any key to move on");
Console.ReadLine();
Console.Clear();
enterName();
} while (answer == 2);
}
static void enterName()
{
Console.Write(" Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine(" Hello it is very nice to meet you " name);
Console.WriteLine(" Are you ready to use C# to solve Ohm's Law (y or n) ? ");
string y = Console.ReadLine().ToLower();
Console.WriteLine(" Great!!, well then lets get started.Click enter");
Console.Clear();
Equation();
}
static void Equation()
{
do
{
Console.Write("What would you like to find? V,I, or R:");
string unitToFind = Console.ReadLine().ToUpper();
if (unitToFind == "V")
{
Console.WriteLine("Enter the Resistance as a decimal.");
double R;
while (!double.TryParse(Console.ReadLine(), out R)) ;
Console.WriteLine("Enter the Amperage as a decimal number");
double I;
while (!double.TryParse(Console.ReadLine(), out I)) ;
double V = R * I;
Console.WriteLine("Voltage is: " V.ToString() " Volts");
}
else if (unitToFind == "I")
{
Console.WriteLine("Enter the Voltage as a decimal number");
double V;
while (!double.TryParse(Console.ReadLine(), out V)) ;
Console.WriteLine("Enter the Resistance as a decimal number");
double R;
while (!double.TryParse(Console.ReadLine(), out R)) ;
double I = V / R;
Console.WriteLine("Amperage is: " I.ToString() " Amps");
}
else if (unitToFind == "R")
{
Console.WriteLine("Enter the Voltage as a decimal number");
double V;
while (!double.TryParse(Console.ReadLine(), out V)) ;
Console.WriteLine("Enter the Amperage as a decimal number");
double I;
while (!double.TryParse(Console.ReadLine(), out I)) ;
double R = V / I;
Console.WriteLine("Resistance is: " R.ToString() " Ohms");
}
Console.WriteLine("You need to enter either V, I or R. Please run the program again.");
Console.WriteLine("Please enter 0 to continue working problems");
Console.WriteLine("1 to quit");
Console.WriteLine("2 to restart entire program. ");
answer = int.Parse(Console.ReadLine());
} while (answer == 0);
}
}
}
Here you go, but i highly recommend you fix some inputs, the possibility for users right now to input wrong data is large. What your needing is a Do while loop. The Do while loop runs through the entire program 1 time. Afterwards it checks if the condition is true. If it's true, it continues to cycle.
I also agree with comments. I removed a couple, but you use a lot of Console.readLine for nothing? You also have way to many WriteLine. Try to use \n As creates a new line, instead of writing on the same. (Like shift enter in Word). You could also use A single string/double outside the if parameters for multiple answers.
CodePudding user response:
i just nee an array used for my project this module week but i didnt want to mess up the previous code I used. I thought that i could use an array to give three different inputs to either close restart or add a new person to the the post.
Here's an example of using an array of strings to display a list of choices to the user in a repeating menu:
public static void Main (string[] args) {
String[] options = {
"0 - Continue working problems",
"1 - Quit",
"2 - Restart entire program"
};
int choice;
do {
Console.WriteLine("You need to enter either V, I or R.");
Console.WriteLine("Please run the program again:");
foreach(String option in options) {
Console.WriteLine(option);
}
Console.Write("Your choice: ");
String selection = Console.ReadLine();
if (int.TryParse(selection, out choice)) {
if (choice<0 || choice >2) {
Console.WriteLine("Menu choice must be between 0 and 2 inclusive!");
}
}
else {
Console.WriteLine("Invalid Input!");
choice = -1;
}
} while (choice<0 || choice>2);
Console.WriteLine("You selected option #" choice);
}
You can use logic like this, along with Rojhat Sefdin's example to make a complete program.