C
C#2mo ago
TuckBuckk

Can't declare variables not in main?

All of my variables in Main and playLottery have the error line underneath them. How come
//Lab Exercise 6.3.2024 Problem 1
//Author: Tucker Sopha

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Problem_1
{
class Program
{
List<int> yourWhiteBalls = new List<int>();
int yourPowerBall;

List<int> winningWhiteBalls = new List<int>();
int winningPowerBall = -1;
bool win = false;
int numberOfPlays;
Random r = new Random();
static void Main(string[] args)
{
//Declare variables



//Generate 5 numbers
for (int i = 0; i < 5; i++)
{
yourWhiteBalls.Add(r.Next(1, 70));
}
//Sort the list
yourWhiteBalls.Sort();
//Pick the PowerBall
yourPowerBall = r.Next(1, 27);


//Print the picks and the PowerBall
printLottery(yourWhiteBalls, yourPowerBall);


}
static void playLottery()
{
while (!win)
{
for (int i = 0; i < 5; i++)
{
winningWhiteBalls.Add(r.Next(1, 70));
}
winningWhiteBalls.Sort();
winningPowerBall = r.Next(1, 27);

if (yourPowerBall == winningPowerBall)
{
continue;
}

for (int i = 0; i < 5; i++)
{

}
}
}
}
}
//Lab Exercise 6.3.2024 Problem 1
//Author: Tucker Sopha

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Problem_1
{
class Program
{
List<int> yourWhiteBalls = new List<int>();
int yourPowerBall;

List<int> winningWhiteBalls = new List<int>();
int winningPowerBall = -1;
bool win = false;
int numberOfPlays;
Random r = new Random();
static void Main(string[] args)
{
//Declare variables



//Generate 5 numbers
for (int i = 0; i < 5; i++)
{
yourWhiteBalls.Add(r.Next(1, 70));
}
//Sort the list
yourWhiteBalls.Sort();
//Pick the PowerBall
yourPowerBall = r.Next(1, 27);


//Print the picks and the PowerBall
printLottery(yourWhiteBalls, yourPowerBall);


}
static void playLottery()
{
while (!win)
{
for (int i = 0; i < 5; i++)
{
winningWhiteBalls.Add(r.Next(1, 70));
}
winningWhiteBalls.Sort();
winningPowerBall = r.Next(1, 27);

if (yourPowerBall == winningPowerBall)
{
continue;
}

for (int i = 0; i < 5; i++)
{

}
}
}
}
}
No description
5 Replies
reflectronic
reflectronic2mo ago
the problem is that they are instance variables of Program it means you need an instance of Program to access them e.g. new Program().r and you could of course make multiple different instances of Program, which all have different values, which is probably not what you want
TuckBuckk
TuckBuckk2mo ago
ah so make them static
reflectronic
reflectronic2mo ago
yes, that one one way to fix it
TuckBuckk
TuckBuckk2mo ago
sweet ty
Angius
Angius2mo ago
One pattern I really like for making games like this is a game instance
// Game.cs
public sealed class Game
{
private int _score;

public void Run()
{
// ...
}
}
// Game.cs
public sealed class Game
{
private int _score;

public void Run()
{
// ...
}
}
// Program.cs
var gameInstance = new Game();
gameInstance.Run();
// Program.cs
var gameInstance = new Game();
gameInstance.Run();
Could easily make the game replayable too, and make resetting the game state super easy
// Game.cs
public sealed class Game
{
private int _score;

public bool Run()
{
// ...
}
}
// Game.cs
public sealed class Game
{
private int _score;

public bool Run()
{
// ...
}
}
// Program.cs
var gameInstance = new Game();
while (gameInstance.Run())
{
Console.WriteLine("New game begins!");
gameInstance = new Game();
}
Console.WriteLine("Thanks for playing!");
// Program.cs
var gameInstance = new Game();
while (gameInstance.Run())
{
Console.WriteLine("New game begins!");
gameInstance = new Game();
}
Console.WriteLine("Thanks for playing!");