Jump to content

Very beginner C# question

- - - - -

  • Please log in to reply
13 replies to this topic

#1
mbridges

mbridges

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
I apologize for this very basic question, what would I put at the end of this code to make this whole program repeat? Thanks!

        static void Main(string[] args)

        {

         

            

            int i = 0;

            int p = 0;

            int c = 0;

            Random playerCard = new Random { };

            while (i < 1)

            {

                int dealOne = playerCard.Next(1, 11);

                int dealTwo = playerCard.Next(1, 11);

                int dealThree = playerCard.Next(1, 11);

                int dealFour = playerCard.Next(1, 11);

                int playerSum = (dealOne + dealTwo);

                int compSum = (dealThree + dealFour);

                i++;

                if (compSum > playerSum)

                    c++;

                if (playerSum > compSum)

                    p++;

                

                Console.WriteLine("You have: " + playerSum + " points.");

                Console.WriteLine("The computer has: " + compSum + " points.");

               

                if (playerSum > compSum)

                    Console.WriteLine("You win");

     

                else if (compSum > playerSum)

                    Console.WriteLine("Computer Wins");


                else

                    Console.WriteLine("You tie");


              Console.WriteLine("You have " + p + " wins.");

              Console.WriteLine("The Computer has " + c + " wins");

                


#2
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Here this code works fine for me.
static void Main(string[] args)
        {


            int i = 0;
            int p = 0;
            int c = 0;
            Random playerCard = new Random { };
            while (i < 5)[COLOR=green]//Change this number to the number of times you want the program to loop.[/COLOR]
            {
                int dealOne = playerCard.Next(1, 11);
                int dealTwo = playerCard.Next(1, 11);
                int dealThree = playerCard.Next(1, 11);
                int dealFour = playerCard.Next(1, 11);
                int playerSum = (dealOne + dealTwo);
                int compSum = (dealThree + dealFour);
                i++;
                if (compSum > playerSum)
                {
                    c++;
                }
                if (playerSum > compSum)
                {
                    p++;
                }

                Console.WriteLine("You have: " + playerSum + " points.");
                Console.WriteLine("The computer has: " + compSum + " points.");

                if (playerSum > compSum)
                {
                    Console.WriteLine("You win");
                }
                else if (compSum > playerSum)
                {
                    Console.WriteLine("Computer Wins");
                }
                else
                {
                    Console.WriteLine("You tie");
                }

                Console.WriteLine("You have " + p + " wins.");
                Console.WriteLine("The Computer has " + c + " wins");
            }
        }

Also put braces around your 'if' statements, it makes it hard to tell when they end and makes your code messier. ~ Committed.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#3
mbridges

mbridges

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Okay thanks! But what if I wanted it so you press enter and it does it again, instead of all of them coming up at once?
Oh and I forgot to mention its a console app if you didn't already notice.

#4
Matt Ellen

Matt Ellen

    Newbie

  • Members
  • PipPip
  • 14 posts
under where you have

Console.WriteLine("The Computer has " + c + " wins");

write

Console.ReadLine();


#5
mbridges

mbridges

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Oh wow that was easy. Thanks for your help!

#6
mbridges

mbridges

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Another question, how would I get the program end after an "If" statement?

#7
Combinu

Combinu

    Newbie

  • Members
  • Pip
  • 9 posts
If i got to understand you well, at the line you want it to stop just write:

break;


#8
mbridges

mbridges

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
That just prevented it from continuing, but it didn't close the app.

#9
mbridges

mbridges

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Oh and another quick question. Lets say for example if I wanted to provide the user with 2 options and they have to press 1 or 2. How would I do this? I know you need Console.ReadLine but how do get the program to read that and select whatever the user chose?

#10
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Here, I think this is what your looking for. ;)

static void Main(string[] args)
        {
            int playAgain = 0;
            int i = 0;
            int p = 0;
            int c = 0;
            Random playerCard = new Random { };
            play:
            while (i < 1)
            {
                int dealOne = playerCard.Next(1, 11);
                int dealTwo = playerCard.Next(1, 11);
                int dealThree = playerCard.Next(1, 11);
                int dealFour = playerCard.Next(1, 11);
                int playerSum = (dealOne + dealTwo);
                int compSum = (dealThree + dealFour);
           
                if (compSum > playerSum)
                {
                    c++;
                }
                if (playerSum > compSum)
                {
                    p++;
                }

                Console.WriteLine("You have: " + playerSum + " points.");
                Console.WriteLine("The computer has: " + compSum + " points.");

                if (playerSum > compSum)
                {
                    Console.WriteLine("You win");
                }
                else if (compSum > playerSum)
                {
                    Console.WriteLine("Computer Wins");
                }
                else
                {
                    Console.WriteLine("You tie");
                }

                Console.WriteLine("You have " + p + " wins.");
                Console.WriteLine("The Computer has " + c + " wins");
                Console.WriteLine("Would you like to play again? Enter '1' for 'yes', '2' for 'no'.");
                playAgain = Convert.ToInt32(Console.ReadLine());

                if (playAgain == 1)
                {
                    goto play;
                }
                else if (playAgain == 2)
                {
                    break;
                }
            }
           
        }
Hope this is what you where needing, and you understand how it works. ~ Committed. :)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#11
mbridges

mbridges

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Oh wow thank you very much! That helped lots.

#12
bytecoder

bytecoder

    Newbie

  • Members
  • Pip
  • 3 posts
The code above by Committed should do the trick but 3 things :
1) When pressing "2" it will not exit, but stop the program.
3) There is a variable "i" being used, assigned 0 and never goes on, but is used as a "while(true)" solution, unless I miss somthing, you dont really need this.
2) In my opinion using goto is never the best solution.

Therefore I offer this code to be used :

using System;

using System.Collections.Generic;

using System.Text;


namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

	        string playAgain = null;

	        bool bGo = true;

	        int p = 0;

	        int c = 0;

	        Random playerCard = new Random { };


            do

            {

                int dealOne = playerCard.Next(1, 11);

                int dealTwo = playerCard.Next(1, 11);

                int dealThree = playerCard.Next(1, 11);

                int dealFour = playerCard.Next(1, 11);

                int playerSum = (dealOne + dealTwo);

                int compSum = (dealThree + dealFour);


                if (compSum > playerSum)

                {

                    c++;

                }

                if (playerSum > compSum)

                {

                    p++;

                }


                Console.WriteLine("You have: " + playerSum + " points.");

                Console.WriteLine("The computer has: " + compSum + " points.");


                if (playerSum > compSum)

                {

                    Console.WriteLine("You win");

                }

                else if (compSum > playerSum)

                {

                    Console.WriteLine("Computer Wins");

                }

                else

                {

                    Console.WriteLine("You tie");

                }


                Console.WriteLine("You have " + p + " wins.");

                Console.WriteLine("The Computer has " + c + " wins");

                Console.WriteLine("Would you like to play again? Write 'no' to exit, anything else to continue.");

                playAgain = Console.ReadLine();


                if (playAgain == "no")

                {

                    bGo = false;

                }

            }

            while (bGo);

        }

    }

}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users