Question calculate the area of another triangle again?

CompDummy

Member
Joined
Dec 5, 2020
Messages
5
Programming Experience
Beginner
I created a program
C#:
class Program
{
    static void Main(string[] args)
    {
        double b;
        double h;

        Console.WriteLine("Basis: ");
        b= Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Height: ");
        h= Convert.ToDouble(Console.ReadLine());

        double area1 = Program.calcArea(b, h);
        Console.WriteLine("The area of triangle 1 is: ");
        Console.WriteLine(area1);
        Console.ReadLine();
    }

    public static double calcArea(double b, double h)
    {
        return b * h / 2;
    }
}
It works, but I have to do it twice, what's the solution?
 
Last edited by a moderator:
Solution
The way you perform an action multiple times is to use a loop. I see no loop in your code so there is no repetition. If you don't know how to write loops, I suggest that you do a bit of research. Any beginners'' tutorial will teach you how as loops are one of the most fundamental building blocks in programming. If you do know how, I suggest that you implement what you think is correct and then post back if it doesn't work, providing all the details of the issue.
Please provide a FULL and CLEAR explanation of the problem. Provide details of:
  1. what you are trying to achieve;
  2. how you are trying to achieve it; and
  3. what happens when you try.
but I have to do it twice
Do what twice? What EXACTLY do you expect to happen and what EXACTLY does happen? Have you debugged the code, i.e. set a breakpoint and stepped through it line by line, examining the state at each step? If you haven't then stop whatever else you're up to and do that. ALWAYS debug your code before posting a question. If you aren't able to solve the problem then at least you can provide us with precise details of the issue, i.e. EXACTLY where and how the actual behaviour differs from your expectations.
 
If you calculate this, you get a solution. If I press enter, the program closes again. But I want to get the same thing back after the solution, so I can calculate the area of another triangle again. Only then can I close it.
 
The way you perform an action multiple times is to use a loop. I see no loop in your code so there is no repetition. If you don't know how to write loops, I suggest that you do a bit of research. Any beginners'' tutorial will teach you how as loops are one of the most fundamental building blocks in programming. If you do know how, I suggest that you implement what you think is correct and then post back if it doesn't work, providing all the details of the issue.
 
Solution
Your code looks like it should run as you wrote it. Why would you expect it to run more than once?
Since there is nothing in your code to make it run twice, I suggest following the perfectly valid advice outlined in the above post in order to achieve that. Then report back and let us know if you resolved your issue after said modifications. :)
 
Back
Top Bottom