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");
13 replies to this topic
#1
Posted 16 January 2011 - 06:20 PM
I apologize for this very basic question, what would I put at the end of this code to make this whole program repeat? Thanks!
|
|
|
#2
Posted 17 January 2011 - 08:41 AM
Here this code works fine for me.
Also put braces around your 'if' statements, it makes it hard to tell when they end and makes your code messier. ~ Committed.
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.
Science is only an educated theory, which we cannot disprove.
#3
Posted 17 January 2011 - 09:27 AM
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.
Oh and I forgot to mention its a console app if you didn't already notice.
#4
Posted 17 January 2011 - 09:35 AM
under where you have
write
Console.WriteLine("The Computer has " + c + " wins");
write
Console.ReadLine();
#5
Posted 17 January 2011 - 09:39 AM
Oh wow that was easy. Thanks for your help!
#6
Posted 17 January 2011 - 02:43 PM
Another question, how would I get the program end after an "If" statement?
#7
Posted 18 January 2011 - 09:09 AM
If i got to understand you well, at the line you want it to stop just write:
break;
#8
Posted 18 January 2011 - 05:56 PM
That just prevented it from continuing, but it didn't close the app.
#9
Posted 18 January 2011 - 07:15 PM
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
Posted 19 January 2011 - 11:52 AM
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.
Science is only an educated theory, which we cannot disprove.
#11
Posted 19 January 2011 - 01:18 PM
Oh wow thank you very much! That helped lots.
#12
Posted 22 January 2011 - 12:25 AM
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 :
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


Sign In
Create Account


Back to top









