Program won't move onto next method - c#

My program won't move onto the next method after it executes the main method.
Right now, it just prints out "Hey! Welcome to Tina's Dice Game. Let's Start!", and then stops. How do I fix this? I've messed around with it for a while, but nothing worked. Thank you for your help.
using System;
namespace Major_Coding_Assignment_1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hey! Welcome to Tina's Dice Game.");
Console.WriteLine("Let's start!");
}
public static int numberofInvokes = 0;
public void EvenOrOdd()
{
numberofInvokes += 1;
Random rnd = new Random();
int x = rnd.Next(1, 7);
int y = rnd.Next(1, 7);
int added = x + y;
if (added % 2 == 0)
{
Console.WriteLine("I got" + " " + x + " " + "and" + " " + y);
Console.WriteLine("Evens are better than odds.");
}
else
{
Console.WriteLine("I got" + " " + x + " " + "and" + " " + y);
Console.WriteLine("Odds are still cool!");
}
}
public void playAgain()
{
Console.WriteLine("Do you want to play again?");
string val = Console.ReadLine();
if (val == "yes")
{
EvenOrOdd();
}
else
{
Console.WriteLine("The number of times the dice was thrown is:" + " " + numberofInvokes);
Console.WriteLine("Thanks for playing, come play again soon!");
}
}
}
}

You need to call the methods you need from the main function like this...
static void Main(string[] args)
{
Console.WriteLine("Hey! Welcome to Tina's Dice Game.");
Console.WriteLine("Let's start!");
EvenOrOdd();
playAgain();
}
The better way is to call playAgain inside EvenOrOdd method to make things look cleaner.
static void Main(string[] args)
{
Console.WriteLine("Hey! Welcome to Tina's Dice Game.");
Console.WriteLine("Let's start!");
EvenOrOdd();
}
public static int numberofInvokes = 0;
public void EvenOrOdd()
{
numberofInvokes += 1;
Random rnd = new Random();
int x = rnd.Next(1, 7);
int y = rnd.Next(1, 7);
int added = x + y;
if (added % 2 == 0)
{
Console.WriteLine("I got" + " " + x + " " + "and" + " " + y);
Console.WriteLine("Evens are better than odds.");
}
else
{
Console.WriteLine("I got" + " " + x + " " + "and" + " " + y);
Console.WriteLine("Odds are still cool!");
}
playAgain(); //Call playAgain from here.
}
public void playAgain()
{
Console.WriteLine("Do you want to play again?");
string val = Console.ReadLine();
if (val == "yes")
{
EvenOrOdd();
}
else
{
Console.WriteLine("The number of times the dice was thrown is:" + " " + numberofInvokes);
Console.WriteLine("Thanks for playing, come play again soon!");
}
}

its important that you call your methods from main. I like to think of it like a recipe main is the first instruction but you need to tell the reader where to look next. The same is true for C#, you need to tell the compiler where to go next by calling the next function in the main one.
static void Main(string[] args)
{
//your start code
nextFunction();
}
public void nextFunction(){
//your next function code
}

Related

Operator `+' cannot be applied to operands of type `int' and `System.Random'

i'm a beginner to c#,so i was trying to make a program that rolls a die for you and the enemy for 10 turns,each turns adding the number of your roll to an overall count and whoever got the largest in the end won,i didn't finish it all the way but this is what i have so far:
namespace dfgs
{
class dice
{
public static void Main(String[] args)
{
int plsc = 0;
int aisc = 0;
int turns = 0;
Random plrnd = new Random();
Random airnd = new Random();
while (turns < 11)
{
Console.WriteLine("Player Roll Is" + Convert.ToInt32(plrnd.Next(6)));
Console.WriteLine("AI Roll Is" + Convert.ToInt32(airnd.Next(6)));
plsc = plsc + plrnd;
aisc = aisc + airnd;
Console.WriteLine("Type A and hit enter to go again");
string nxt = Console.ReadLine();
if (nxt == "A"){
turns++;
}
Console.ReadLine();
}
}
}
}
and whenever i try to compile i get the error Operator +' cannot be applied to operands of type int' and System.Random',this error appears twice,i tried changing the type of the random numbers to int but then i got the error messege Type int' does not contain a definition for Next' and no extension method Next' of type int' could be found. Are you missing an assembly reference? i am kinda stuck here,any help would be appreciated.
EDIT:Thank you to everyone who answered, i have managed to make this work,here is the final code,it's not the cleanest but works as intended:
namespace dfgs
{
class die
{
public static void Main(String[] args)
{
int plsc = 0;
int aisc = 0;
int turns = 0;
Random plrnd = new Random();
Random airnd = new Random();
while (turns < 10)
{
Console.WriteLine("Player Roll Is " + Convert.ToInt32(plrnd.Next(6)));
Console.WriteLine("AI Roll Is " + Convert.ToInt32(airnd.Next(6)));
plsc = plsc + plrnd.Next(6);
aisc = aisc + airnd.Next(6);
Console.WriteLine("Type A and hit enter to go again");
string nxt = Console.ReadLine();
if (nxt == "A"){
turns++;
}
else{
break;
}
if (turns == 10){
if (plsc > aisc){
Console.WriteLine("The Player Has Won,Ai Score: " + aisc + " Player Score: " + plsc);
}
else if (aisc > plsc){
Console.WriteLine("The AI Has Won,Ai Score: " + aisc + " Player Score: " + plsc);
}
break;
}
}
Console.ReadLine();
}
}
}
You probably want to save the random int you get from the Random, rather than trying to calculate number, plus a random_number_generator_machine - which, in some real world terms would be like asking someone "what is your age, divided by a cheese sandwich?"..
var plroll = plrnd.Next(6);
var airoll = airnd.Next(6);
Console.WriteLine("Player Roll Is" + plroll);
Console.WriteLine("AI Roll Is" + airoll));
plsc = plsc + plroll;
aisc = aisc + airoll;
You don't need a Random for the player and a Random for the computer; one Random could adequately generate for both, btw:
int plsc = 0;
int aisc = 0;
int turns = 0;
Random r = new Random();
while (turns < 11)
{
var plroll = r.Next(6);
var airoll = r.Next(6);
Console.WriteLine("Player Roll Is" + plroll);
Console.WriteLine("AI Roll Is" + airoll));
plsc = plsc + plroll;
aisc = aisc + airoll;
Look at this line:
plsc = plsc + plrnd;
In this code, plrnd is not a number. Instead, it's a generator for producing numbers. You must tell the generator to give you the next number, as you did on the line above:
plrnd.Next(6)
Moreover, you likely need to change the code above to save the results of each call in variables so you can use that same value to both show to the user and increment the total count.
Since you want to use the result of the roll multiple times, start by assigning the results to a variable for later use:
while (turns < 11)
{
// roll dies, store results in local variable
var plRoll = plrnd.Next(6);
var aiRoll = airnd.Next(6);
// then reuse the variables
Console.WriteLine("Player Roll Is " + plRoll);
Console.WriteLine("AI Roll Is " + aiRoll);
plsc = plsc + plRoll;
aisc = aisc + aiRoll;
Console.WriteLine("Type A and hit enter to go again");
string nxt = Console.ReadLine();
if (nxt == "A"){
turns++;
}
else {
// remember to break out if the player doesn't want to continue
break;
}
}
int is a built in value data type in C#. Random is a class object. The + operator does not know how to add an int data type and a Random class object together. What you probably meant to do is to use the Next() method, which does return an int. Also, you only want to new up the Random class one time. Consider using this code instead:
namespace dfgs
{
class dice
{
public static void Main(String[] args)
{
Random rnd = new Random();
int plsc = 0;
int aisc = 0;
int turns = 0;
int plrnd = rnd.Next(6);
int airnd = rnd.Next(6);
while (turns < 11)
{
Console.WriteLine("Player Roll Is" + plrnd);
Console.WriteLine("AI Roll Is" + airnd);
plsc = plsc + plrnd;
aisc = aisc + airnd;
Console.WriteLine("Type A and hit enter to go again");
string nxt = Console.ReadLine();
if (nxt == "A"){
turns++;
}
Console.ReadLine();
}
}
}
}

method endgame() cannot be called

So for the main class, I only have this code. Calling the cClass. THIS IS A DICE GAME
{
class Program
{
static void Main(string[] args)
{
cClass cs = new cClass();
}
}
For the Constructor class, which is cClass i have this code. The method endgame() is not working. This is where the problem located the when the dice game done battling, the endgame() is not working.
class cClass
{
public cClass()
{
Intialize();
}
public void Intialize()
{
sClass.round = 1;
while (sClass.com < 3 && sClass.player < 3)
{
start:
Console.WriteLine("Round (" + sClass.round + ")");
Console.WriteLine("Enter your guess: ( 1 - 6)");
sClass.guess = Convert.ToInt32(Console.ReadLine());
if (sClass.guess >= 7)
{
Console.WriteLine("Only enter number ranges from 1 - 6");
Console.ReadLine();
Console.Clear();
goto start;
}
sClass.playgame();
System.Threading.Thread.Sleep(1000);
Console.WriteLine("\nThe dice rolled is...");
Console.WriteLine(sClass.playerdie);
Console.WriteLine("\nPlayer guessed: " + sClass.guess);
Console.WriteLine("Computer guessed: " + sClass.comguess);
if ((sClass.comguess == sClass.playerdie) && (sClass.guess == sClass.playerdie))
{
Console.WriteLine("\nBoth guessed correctly! Restarting the round!\n");
Console.WriteLine("Player points: " + sClass.player);
Console.WriteLine("Computer points: " + sClass.com);
}
else if (sClass.guess == sClass.playerdie)
{
sClass.player++;
Console.WriteLine("\nPlayer guessed correctly you won!\n");
Console.WriteLine("Player points: " + sClass.player);
Console.WriteLine("Computer points: " + sClass.com);
sClass.round++;
}
else if (sClass.comguess == sClass.playerdie)
{
sClass.com++;
Console.WriteLine("\nComputer guessed correctly you lost...\n");
Console.WriteLine("Player points: " + sClass.player);
Console.WriteLine("Computer points: " + sClass.com);
sClass.round++;
}
else
{
Console.WriteLine("\nNo one got it correct restarting the round!\n");
Console.WriteLine("Player points: " + sClass.player);
Console.WriteLine("Computer points: " + sClass.com);
}
Console.ReadLine();
Console.Clear();
}
endgame();
}
public void endgame()
{
if (sClass.player > sClass.com)
{
Console.WriteLine("Player Win! Player vs Computer");
Console.WriteLine(" (" + sClass.player + ") vs (" + sClass.com + ")");
}
else if (sClass.player < sClass.com)
{
Console.WriteLine("Computer Win! Player vs Computer");
Console.WriteLine(" (" + sClass.player + ") vs (" + sClass.com + ")");
}
else
{
Console.WriteLine("Its a draw! Player vs Computer");
Console.WriteLine(" (" + sClass.player + ") vs (" + sClass.com + ")");
}
}
}
then I also have a static class , which is sClass.
static class sClass
{
public static int dice = 1;
public static int playerdie;
public static int guess;
public static int comguess;
public static int player;
public static int com;
public static int round;
public static Random r = new Random();
public static void playgame()
{
comguess = r.Next(1, 6);
playerdie = rolldie();
}
public static int rolldie()
{
int die = r.Next(1, 6);
return die;
}
}
So, you just said that there is no error. I think you just missed only 1 code. In cClass.cs Go to the Initialize() method, go at the endgame() then put the Console.ReadLine above the endgame
endgame();
Console.ReadLine();
Then it will show the endgame method.
Currently, your while loop says that it's checking on both simultaniously:
while (sClass.com < 3 && sClass.player < 3)
I said formerly if it should be an OR-statement, but that didn't work, because then either of them would still be true, continue-ing the while loop.
So it'll only stop the while loop if all statements are false.
Perhaps this code will work:
while (!(sClass.com >= 3 && sClass.player >= 3))
What I did now is reversing the while check, and now it checks if both are still false, and the ! will reverse that false to true. So if either of the scores becomes 3 now, it'll go out of the while loop.

I'm trying to return out of a method but I get a CS0219 warning

I'm trying to return out of a method in C#, but every time I run the program I get a message that I'm creating a variable but never using it, then I get errors that the current variable does not exist in current context.
I am relatively new to C#, and I might be really dumb but if anyone can help me that will be great!
The code:
using System;
class MainClass
{
public static void Main()
{
int health = 3;
int powerCrystals = 0;
int healthCrystals = 0;
int basicEnemyDamage = 1;
int basicEnemyScore = 10;
string basicEnemyDrops = "1 powerCrystal";
int score = 0;
// Basic Enemies Drop 1 powerCrystal.
// Moderate Enemies Drop 1 powerCrystal and 1 healthCrystal.
// Advanced Enemies Drop 2 powerCrystals and 1 healthCrystal.
// Bosses Drop 3 powerCrystals and 3 healthCrystals.
Console.WriteLine("Input your username:");
string userName = Console.ReadLine();
Console.WriteLine("Welcome, " + userName);
Console.WriteLine("To check your stats, write: stats");
TrollFight();
}
static void TrollFight()
{
Console.WriteLine(userName + "! There is a pack of trolls aproaching! Guess the right number to kill them!");
Console.WriteLine("Your current health is = " + health);
Console.WriteLine("Guess the correct number and then you can defeat the trolls! It is between 1-9!");
int guessedNumber = Convert.ToInt32(Console.ReadLine());
if (guessedNumber == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyDamage;
Console.WriteLine("Your score is = " + score);
return;
}
else
{
Console.WriteLine("WRONG! You took damage! Come on, guess again!");
health = health - basicEnemyDamage;
}
int guessedNumber2 = Convert.ToInt32(Console.ReadLine());
if (guessedNumber2 == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyScore;
Console.WriteLine("Your score is = " + score);
return;
}
else
{
Console.WriteLine("WRONG! You took damage! Come on, guess again! Your health is = 2");
health = health - basicEnemyScore;
}
int guessedNumber3 = Convert.ToInt32(Console.ReadLine());
if (guessedNumber3 == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyDamage;
Console.WriteLine("Your score is = " + score);
return;
}
else
{
Console.WriteLine("WRONG! You took damage! Come on, guess again! Your health is = 1");
Console.WriteLine("You are almost dead! You do not have any Health Crystals so you can't heal!");
health = health - basicEnemyScore;
}
int guessedNumber4 = Convert.ToInt32(Console.ReadLine());
if (guessedNumber4 == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyScore;
Console.WriteLine("Your score is = " + score);
return;
}
else
{
Console.WriteLine("WRONG! You died! Your final score = " + score);
health = health - basicEnemyDamage;
}
Console.WriteLine("To check your stats, write: stats");
}
}
I'm trying to return out of a method but it gives me a CS0219 warning
Ok a few things. The difference between a Void/Subroutine and a Function is that a Function returns something. Both your methods are voids, they don't return anything:
public static void Main()
static void TrollFight()
The problem with the variables is that they are out of scope.
Local - visible within the method
Private - visible to the class
Public - visible outside the assembly
One solution is to make the variables Private member variables:
class MainClass {
private int health = 3;
private int powerCrystals = 0;
private int healthCrystals = 0;
private int basicEnemyDamage = 1;
private int basicEnemyScore = 10;
private string basicEnemyDrops = "1 powerCrystal";
private int score = 0;
public static void Main() {
Run the project, this should fix it. The problem with using Private variable scope is it can get messy when multiple methods can change variables (without the other knowing). In this case we can use parameters to pass variables to voids or functions:
TrollFight(basicEnemyDamage);
...
static void TrollFight(string basicEnemyDamage) {
A good design will use all three with local variables:
class MainClass {
private int health = 3;
private string basicEnemyDrops = "1 powerCrystal";
private int score = 0;
public static void Main() {
...
}
static void TrollFight(string basicEnemyDamage) {
int powerCrystals = 0;
int healthCrystals = 0;
int basicEnemyDamage = 1;
int basicEnemyScore = 10;
I'd even change the void to return the Score:
static int TrollFight(string basicEnemyDamage, int score)
{
return score;
}
The Code:
using System;
public class MainClass {
private int health = 3;
private int score = 0;
private int powerCrystals = 0;
private int healthCrystals = 0;
private int basicEnemyDamage = 1;
private int basicEnemyScore = 10;
private string basicEnemyDrops = "1 powerCrystal";
public static void Main() {
//Basic Enemies Drop 1 powerCrystal.
//Moderate Enemies Drop 1 powerCrystal and 1 healthCrystal.
//Advanced Enemies Drop 2 powerCrystals and 1 healthCrystal.
//Bosses Drop 3 powerCrystals and 3 healthCrystals.
Console.WriteLine("Input your username:");
string userName = Console.ReadLine();
Console.WriteLine("Welcome, " + userName);
Console.WriteLine("To check your stats, write: stats");
TrollFight(userName);
}
static void TrollFight(string userName) {
Console.WriteLine(userName + "! There is a pack of trolls approaching! Guess the right number to kill them!");
Console.WriteLine("Your current health is = " + health.ToString());
Console.WriteLine("Guess the correct number and then you can defeat the trolls! It is between 1-9!");
int guessedNumber = Convert.ToInt32(Console.ReadLine());
if (guessedNumber == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyDamage;
Console.WriteLine("Your score is = " + score.ToString());
return;
}
else
{
Console.WriteLine("WRONG! You took damage! Come on, guess again!");
health = health - basicEnemyDamage;
}
int guessedNumber2 = Convert.ToInt32(Console.ReadLine());
if (guessedNumber2 == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyScore;
Console.WriteLine("Your score is = " + score.ToString());
return;
}
else
{
Console.WriteLine("WRONG! You took damage! Come on, guess again! Your health is = 2");
health = health - basicEnemyScore;
}
int guessedNumber3 = Convert.ToInt32(Console.ReadLine());
if (guessedNumber3 == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyDamage;
Console.WriteLine("Your score is = " + score.ToString());
return;
}
else
{
Console.WriteLine("WRONG! You took damage! Come on, guess again! Your health is = 1");
Console.WriteLine("You are almost dead! You do not have any Health Crystals so you can't heal!");
health = health - basicEnemyScore;
}
int guessedNumber4 = Convert.ToInt32(Console.ReadLine());
if (guessedNumber4 == 4)
{
Console.WriteLine("You killed the trolls! You gained: " + basicEnemyDrops);
score = score + basicEnemyScore;
Console.WriteLine("Your score is = " + score.ToString());
return;
}
else
{
Console.WriteLine("WRONG! You died! Your final score = " + score.ToString());
health = health - basicEnemyDamage;
}
Console.WriteLine("To check your stats, write: stats");
}
}

How to get out of a while-loop in c#

Here I am trying to have the user enter "YES" to continue with the game but i'm stuck in the while-loop. This is also my first post so I don't the correct way to post ie. copy and paste? Also is there a indentation shortcut for Visual Studio? Thanks for helping.
using System;
namespace BIT_142_Major_Assignment_1
{
class Program
{
static void Main(string[] args)
{
int numGames = 0;
String answer = "";
Random rand = new Random();
int Dice1 = rand.Next(1, 7);
int Dice2 = rand.Next(1, 7);
Console.WriteLine("Hey! Welcome to Andy’s Dice Game.");
Console.WriteLine("Let’s start!");
{
while (true)
{
numGames++;
Console.WriteLine("Dice 1: " + Dice1);
Console.WriteLine("Dice 2: " + Dice2);
Console.WriteLine("I got " + Dice1 + " and " + Dice2 + "!");
if ((Dice1 + Dice2) % 2 == 0)
{
Console.WriteLine("Evens are better than odds!");
}
else
{
Console.WriteLine("Odds are still cool!");
Console.WriteLine("Do you want to play again?");
answer = Console.ReadLine();
if (answer == "YES")
{
continue;
}
else
{
Console.WriteLine("The number of times the dice was thrown is: " + numGames);
Console.WriteLine("Nice Game!");
Console.WriteLine("Thank for playing, Come play again soon!");
break;
}
}
}
}
}
}
}

How to start over with my code? C# Console [duplicate]

This question already has answers here:
How to loop a Console App
(6 answers)
Closed 6 years ago.
I wanted to try how the if conditional works so I created this code almost by myself. I also had problems with random into int.
Here's my code:
using System;
namespace Bigger_Smaller_Equal
{
class Program
{
static void Main(string[] args)
{
int min = 1;
int max = 100;
Random rnd = new Random();
int gen = rnd.Next(min, max);
Console.WriteLine("My Number is : " + gen + "!");
Console.WriteLine("Tell me your number:");
string typ = Console.ReadLine();
int num = int.Parse(typ);
if (num == gen)
{
Console.WriteLine(num + " is Equal to " + gen);
}
else if (num > gen)
{
Console.WriteLine(num + " Is Bigger than " + gen);
}
else if (num < gen)
{
Console.WriteLine(num + " Is Smaller than " + gen);
}
Console.WriteLine("Press Any Key to exit.");
Console.ReadLine();
}
}
}
How to make the console stop, so it will allow me to enter another number?
Basically:
I write a number it tells me if its smaller bigger or equal to number which was randomly generated
After I press enter instead of closing the console the number will be generated again and I can write new number and so on.
Here's an example using goto, although it is not recommended for more complex applications as you could end up creating endless loops. Feel free to try it out
static void Main(string[] args)
{
int min = 1;
int max = 100;
Random rnd = new Random();
again:
int gen = rnd.Next(min, max);
Console.WriteLine("My Number is : " + gen + "!");
Console.WriteLine("Tell me your number:");
string typ = Console.ReadLine();
int num = int.Parse(typ);
if (num == gen)
{
Console.WriteLine(num + " is Equal to " + gen);
}
else if (num > gen)
{
Console.WriteLine(num + " Is Bigger than " + gen);
}
else if (num < gen)
{
Console.WriteLine(num + " Is Smaller than " + gen);
}
repeat:
Console.WriteLine("Play again? (Y/N)");
string ans = Console.ReadLine();
switch (ans.ToUpper())
{
case "Y": goto again; break;
case "N": break; //continue
default: goto repeat; break;
}
}
You can use console.ReadKey() insted of using console.ReadLine().
console.ReadLine() wait for input set of character thats why you console window is apperre there after pressing any key.
You can use "do while" or "while" operator.If you dont want to use while(true) , you can use this diffirent way. I mean that when user enter 0 or -1 this system can stop. while()
bool repeat = true;
do
{
Console.WriteLine("Enter value ");
string typ = Console.ReadLine();
int num = int.Parse(typ);
if (num!=0)
// bla bla bla.
else
repeat = false;
}while (repeat);

Categories