hacker red
hacker red

Reputation: 15

Program not working even though no error

 private void button1_Click(object sender, EventArgs e)
    {
        {
            int a = 127;
            int v = Roll(a);
            MessageBox.Show(String.Format("Roll for (0) is (1), ", a, v));
        }
        int Roll (int x)
        {
            return (x <= 2) ? x : x + Roll(x / 2);
        }
    }

I have tried so many things with it but this is the best I could get and still its not working

Upvotes: 1

Views: 59

Answers (1)

James Hill
James Hill

Reputation: 61802

Your syntax is all wrong. See notes in comments below.

private void button1_Click(object sender, EventArgs e)
{
    int a = 127;
    int v = Roll(a);

    // Use curly braces, not parens for String.Format
    MessageBox.Show(String.Format("Roll for {0} is {1}, ", a, v));

    // If you're using C# 6+, you could also use string interpolation
    MessageBox.Show($"Roll for {a} is {v}");
}

// Roll should be it's own method, outside of your click handler
private int Roll (int x)
{
    return (x <= 2) ? x : x + Roll(x / 2);
}

// Again, if you're using C# 6+, you could simplify your Roll method
// using expression bodied members
private int Roll (int x) => (x <= 2) ? x : x + Roll(x / 2);

Additional Info

Upvotes: 2

Related Questions