Console Application doesn't output anything. (Warning: Lots of code) | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Console Application doesn't output anything. (Warning: Lots of code)

Does anyone know why whenever I run this application, it doesn't output anything on the screen? namespace ConsoleApp17 { class Program { class War { public War(Random x, Random y, int cardx, int cardy) { while (cardx > 0 && cardy > 0) { y.Next(10); x.Next(10); if (x.Next(10) > y.Next(10)) { cardx += 2; cardy -= 2; Console.WriteLine("Deck 1: " + cardx + "Deck 2: " + cardy); Console.WriteLine(); Console.WriteLine("Press any key to flip the next cards."); Console.ReadKey(); } else if (y.Next(10) > x.Next(10)) { cardy += 2; cardx -= 2; Console.WriteLine("Deck 1: " + cardx + "Deck 2: " + cardy); Console.WriteLine(); Console.WriteLine("Press any key to flip the next cards."); Console.ReadKey(); } } if (cardx <= 0) { Console.WriteLine("Deck 2 wins!"); } else if (cardy <= 0) { Console.WriteLine("Deck 1 wins!"); } else { Console.WriteLine("It's a tie!"); } } } static void Main(string[] args) { Random card1 = new Random(10); Random card2 = new Random(10); War war = new War(card1, card2, 10, 10); Console.ReadKey(); } } }

7th Jun 2017, 9:38 PM
Unknown Unknown
Unknown Unknown - avatar
8 Antworten
+ 5
If I were to guess I'd say the issue is that it's taking to long since the values are equal its just an infinite loop. The randoms are of the same seed (10) So, they have the same values being returned. When creating the random object try using the current date time or something like that so they have different random values. Random a = new Random( // this is what I'm talking about ); "x.Next(10);" What is the point of these statements? Do you only want to grab a random number once then compare? If so, just keep track of them: int xRand = x.next(10); Then you can do: if(xRand > yRand) // do stuff else { // other case } Also at the end: "else { Console.WriteLine("It's a tie!"); }" This statement cannot, and will never be ran. Even if the random numbers worked. I'll let you think about why 😝.
7th Jun 2017, 10:20 PM
Rrestoring faith
Rrestoring faith - avatar
0
It would help if you describe the desired behaviour of the program. I guess some of the problem is that you generate new random numbers every time you write "x.Next(10);" This function returns a random number, but there is no variable that store the newly generated random number. Did you mean "cardx = x.Next(10);" ? See https://msdn.microsoft.com/en-us/library/zd1bc8e5(v=vs.110).aspx for more info. It is also a bit weird to have the execution of the code in the War-constructor. Constructors are usually used to initialize an object, and then some other method is executed using this object.
7th Jun 2017, 10:13 PM
Martin Øvsthus Christensen
Martin Øvsthus Christensen - avatar
0
I want to do y.Next() and x.Next() and then compare the numbers. If y.Next() is larger, then cardy gets +2 points and cardx gets -2 points. If x.Next() is bigger then cardx gets +2 points and cardy gets -2 points. The while loop ends when cardx or cardy is equal to or below zero. Why doesn't the .Next() method generate a new number everytime the while loop goes through one loop?
7th Jun 2017, 10:26 PM
Unknown Unknown
Unknown Unknown - avatar
0
Even if I don't put 10 in the y.Next() function or the x.Next() function, and instead just leave the parameter blank, the console still does not output anything.
7th Jun 2017, 10:28 PM
Unknown Unknown
Unknown Unknown - avatar
0
Are you running the code on sololearn? It does not seem to handle the Console.ReadKey() very well, so you might have better luck without the ReadKey() if the other bugs are fixed.
7th Jun 2017, 10:42 PM
Martin Øvsthus Christensen
Martin Øvsthus Christensen - avatar
0
I'm running it on visual studio 2017
7th Jun 2017, 10:45 PM
Unknown Unknown
Unknown Unknown - avatar
0
Never mind I fixed it now. I just put Random num1= new Random(); Random num2 = new Random(); under the while loop so every time one loop finishes it creates a new seed, and a new number. I don't really know what a seed in programming is, but I have a general idea of it.
7th Jun 2017, 11:12 PM
Unknown Unknown
Unknown Unknown - avatar
0
I thought that what goes in the parameter of Random() was the range of what numbers can be picked. Like if it was Random(10), it would only pick a number from 0-10. I guess that's not what the parameter is for though.
7th Jun 2017, 11:13 PM
Unknown Unknown
Unknown Unknown - avatar