Roulette game hangs on while loop - c#

I am trying to get this while loop to keep playing a roulette game until my budget is empty. When I run the code it just hangs.
Here is the while code including the loop that hangs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnPlay_Click(object sender, EventArgs e)
{
int Budget = Convert.ToInt32(txtMyBudget.Text);
int Bet = Convert.ToInt32(txtMyBet.Text);
int Outcome = Convert.ToInt32(txtMyNum.Text);
//generates a random number between 0 and 38
Random num = new Random();
int numSpun = num.Next(0, 38);
lblBudgetError.Text = numSpun.ToString();
//checks to make sure the budget is set
if (txtMyBudget.Text == "")
{
lblBudgetError.Text = "Please set your budget";
}
int myBudget = Budget;
while (Budget >= Bet)
{
myBudget++;
lblValidate.Text += myBudget.ToString() + "<br />";
if (numSpun == Outcome)
{
int newBudget = Bet * 35 + Budget;
txtMyBudget.Text = newBudget.ToString();
}
else
{
int newBudget = Budget - Bet;
txtMyBudget.Text = newBudget.ToString();
}
}
}
Thank you for your help.

You are changing the myBudget not Budget which is used in the loop. Either change the condition (use myBudget, like while (myBudget >= Bet)) or increase the Budget.
BTW, if you come from a background with two-way binding, it is not the case here, i.e., changing the value of the TextBox doesn't change the value of the Budget and you should update its value, for example in a getter.

Related

Cannot get int to be displayed in text box for dice roller app

I'm trying to get back into programming and I'm having trouble getting the final int answers into the text boxes at the end. It has been a few years since I've coded, so if I messed up big time, please let me know.
{
int dice_total;
int dice_num;
int diff_num;
int succ_num = 0;
int ones = 0;
Boolean comp_num = false;
string Succ;
string Comp;
dice_total = int.Parse(Dice.Text);
diff_num = int.Parse(Diff.Text);
Random random = new Random();
dice_num = random.Next(dice_total);
if (dice_num >= diff_num)
{
succ_num++;
}
else
{
if (dice_num == 1)
{
ones++;
}
}
if (ones >= succ_num)
{
comp_num = true;
}
else
{
comp_num = false;
}
Succ = succ_num.ToString();
Comp = comp_num.ToString();
}```
[your text box name].Text=[some int].ToString();
For example:
label1.Text = product.BuyingPrice.ToString();
I can't seem to get it count up the successes. For those who don't know, WoD has you roll d10s and you are given a difficulty. You have to roll that difficulty number or higher to get successes. If there are more (or equal) 1s than total successes, its a complication. This isn't even including code for when you roll a 10 (which has you roll again while still counting as a success).
Ex. Roll 5d10s with a difficulty of 6
6, 8, 10, 1, 1 = 3 success
Roll again for the 10: 1
Total: 3 successes and a complication
using Accord.Math.Distances;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Roller
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int dice_total;
int dice_add;
int dice_num;
int diff_num;
int succ_num = 0;
int ones = 0;
Boolean comp_num = false;
public void button1_Click(object sender, EventArgs e)
{
dice_total = int.Parse(Dice.Text);
diff_num = int.Parse(Diff.Text);
for (dice_add = 0; dice_add < dice_total; dice_add++)
{
Random random = new Random();
dice_num = random.Next(dice_total);
if (dice_num >= diff_num)
{
succ_num++;
}
else
{
if (dice_num == 1)
{
ones++;
}
}
if (ones >= succ_num)
{
comp_num = true;
}
else
{
comp_num = false;
}
}
Succ.Text = succ_num.ToString();
Comp.Text = comp_num.ToString();
}
}
}

How can you use a button and a text box to change a label?

I'm just starting out and learning how to code in C Sharp. We have a class project where we need to make a program that is a guessing game of a random number between 1 and 100. I have that working (currently for testing I'm only doing 1-10), but I wanted to go a step past the basic code and add in a few touches of my own. I have four labels, two with words to describe what they are for, and two that I want the data in to change. Say you're guessing between 1 and 100, and you guess 25 and its to low and its the highest number you've guessed that is to low, I want that displayed, same if you guess a number like 70 and its to high I want that displayed in the label. It seems easy enough but I'm not having any luck with it. Being new to coding I'm going to post my whole code as I'm not sure where the error is. Please note since the code is not working, I only have it set up for the low number until I figure it out.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class frmMain : Form
{
Double count = 0;
int randomNumber;
Random rand = new Random();
public frmMain()
{
InitializeComponent();
randomNumber = rand.Next(10) + 1;
lblLowShow.Text = "0";
lblHighShow.Text = "100";
}
private void btnGuess_Click(object sender, EventArgs e)
{
count++;
int guessedNumber;
guessedNumber = int.Parse(txtEnterGuess.Text);
if (guessedNumber > randomNumber)
{
MessageBox.Show("To High! Please try again");
}
if (guessedNumber < randomNumber)
{
int lowguess;
MessageBox.Show("To Low! Please try again");
lowguess = Convert.ToInt32(lblLowShow);
if (guessedNumber < lowguess) // Changes the number of the highest, low guess that is displayed.
{
string LowShow;
LowShow = lowguess.ToString();
lblLowShow.Text = LowShow;
}
}
else
{
MessageBox.Show("Congratulations! You won in " + count + " tries!");
count = 0;
randomNumber = rand.Next(10) + 1;
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnNewGame_Click(object sender, EventArgs e)
{
}
}
}
Visual studio isn't doing any of the red dots to show an error in the code, so that's not helping me track down the mistake(s).
You are missing a simple else statment before the condition of if the guessed number is lower then the desired number. That's why the computer goes directly to the next if statment and prints a message that the user won the game.
just this:
else//
if (guessedNumber < randomNumber)
{
int lowguess;
MessageBox.Show("To Low! Please try again");
lowguess = Convert.ToInt32(lblLowShow);
if (guessedNumber < lowguess) // Changes the number of the highest, low guess that is displayed.
{
string LowShow;
LowShow = lowguess.ToString();
lblLowShow.Text = LowShow;
}
}

Problems about checking for prime number in a Windows Form app

My app is to test for prime number. First time I enter a prime number, the result is true and number is displayed to user. But second time, the prime number check is not functioning as expected. Here is my code:
private void button1_Click(object sender, EventArgs e)
{
label3.Text = textBox1.Text;
float a = (float)Convert.ToDouble(textBox1.Text);
if (check_Number(ref a) == true)
{
ResultCheck.Text = "Input Number is Prime";
}
else if (check_Number(ref a) == false)
{
ResultCheck.Text = "Input Number is not Prime";
}
}
here is an example program that uses trial division.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PrimeCheck
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = textBox1.Text;
double testthis = 0;
if (double.TryParse(textBox1.Text, out testthis))
{
if (CheckForPrime(testthis))
{
MessageBox.Show("prime time!!");
}
}
}
bool CheckForPrime(double number)
{//there are better ways but this is cheap and easy for an example
bool result = true;//assume we are prime until we can prove otherwise
for (double d = 2; d < number; d++)
{
if (number % d == 0)
{
result = false;
break;
}
}
return result;
}
}
}

I'm having trouble putting together a system that creates a receipt

I have been given a coding assignment by my recruiter for a job as a junior developer. There were three choices and I went with a problem that requires calculating a purchase. All items are supposed be taxed 10% unless they are books, food or medicine. Anything imported is taxed an extra 5%, even if they are tax exempt. So I created a form that allows a user to type in the name of the item, two check boxes for whether they are imported or tax exempt, a textbox for inputing price, and a text box for each input. Underneath is a textbox that is supposed to calculate the total sales tax, underneath that is a textbox for the total. The first checkbox is named "Item1Import" the next one is called "Item1Exempt." The price text box is named "Item1Price" and the other "Item1Output." And for each item the number would change, Item2Import, Item3Import, etc. The last two textboxes are called "SalesTax" and "Total."
Here is the code I have so far.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Item1Price_TextChanged(object sender, EventArgs e)
{
if(Item1Exempt.Checked && Item1Import.Checked)
{
Item1Output.Text = ((Convert.ToInt32(Item1Price.Text)) + (Convert.ToInt32(Item1Price.Text) * 0.05).ToString("C2"));
}
else if(Item1Exempt.Checked && !Item1Import.Checked)
{
Item1Output.Text = (Convert.ToInt32(Item1Price.Text)).ToString("C2");
}
else if(!Item1Exempt.Checked && Item1Import.Checked)
{
Item1Output.Text = ((Convert.ToInt32(Item1Price.Text) + (Convert.ToInt32(Item1Price.Text) * 0.1) + (Convert.ToInt32(Item1Price.Text) * 0.05)).ToString("C2"));
}
else
{
Item1Output.Text = ((Convert.ToInt32(Item1Price.Text)) + (Convert.ToInt32(Item1Price.Text) * 0.1)).ToString("C2");
}
}
private void Item2Price_TextChanged(object sender, EventArgs e)
{
if (Item2Exempt.Checked && Item2Import.Checked)
{
Item2Output.Text = ((Convert.ToInt32(Item2Price.Text)) + (Convert.ToInt32(Item2Price.Text) * 0.05).ToString("C2"));
}
else if (Item2Exempt.Checked && !Item2Import.Checked)
{
Item2Output.Text = (Convert.ToInt32(Item2Price.Text)).ToString("C2");
}
else if (!Item2Exempt.Checked && Item2Import.Checked)
{
Item2Output.Text = ((Convert.ToInt32(Item2Price.Text) + (Convert.ToInt32(Item2Price.Text) * 0.1) + (Convert.ToInt32(Item2Price.Text) * 0.05)).ToString("C2"));
}
else
{
Item2Output.Text = ((Convert.ToInt32(Item2Price.Text)) + (Convert.ToInt32(Item2Price.Text) * 0.1)).ToString("C2");
}
}
private void Item3Price_TextChanged(object sender, EventArgs e)
{
if (Item3Exempt.Checked && Item3Import.Checked)
{
Item3Output.Text = ((Convert.ToInt32(Item3Price.Text)) + (Convert.ToInt32(Item3Price.Text) * 0.05).ToString("C2"));
}
else if (Item3Exempt.Checked && !Item3Import.Checked)
{
Item3Output.Text = (Convert.ToInt32(Item3Price.Text)).ToString("C2");
}
else if (!Item3Exempt.Checked && Item3Import.Checked)
{
Item3Output.Text = ((Convert.ToInt32(Item3Price.Text) + (Convert.ToInt32(Item3Price.Text) * 0.1) + (Convert.ToInt32(Item3Price.Text) * 0.05)).ToString("C2"));
}
else
{
Item3Output.Text = ((Convert.ToInt32(Item3Price.Text)) + (Convert.ToInt32(Item3Price.Text) * 0.1)).ToString("C2");
}
}
private void Item4Price_TextChanged(object sender, EventArgs e)
{
if (Item4Exempt.Checked && Item4Import.Checked)
{
Item4Output.Text = ((Convert.ToInt32(Item4Price.Text)) + (Convert.ToInt32(Item4Price.Text) * 0.05).ToString("C2"));
}
else if (Item4Exempt.Checked && !Item4Import.Checked)
{
Item4Output.Text = (Convert.ToInt32(Item4Price.Text)).ToString("C2");
}
else if (!Item4Exempt.Checked && Item4Import.Checked)
{
Item4Output.Text = ((Convert.ToInt32(Item4Price.Text) + (Convert.ToInt32(Item4Price.Text) * 0.1) + (Convert.ToInt32(Item4Price.Text) * 0.05)).ToString("C2"));
}
else
{
Item4Output.Text = ((Convert.ToInt32(Item4Price.Text)) + (Convert.ToInt32(Item4Price.Text) * 0.1)).ToString("C2");
}
}
private void SalesTax_TextChanged(object sender, EventArgs e)
{
SalesTax.Text = (((Convert.ToInt32(Item1Output.Text) - Convert.ToInt32(Item1Price.Text)) + ((Convert.ToInt32(Item2Output.Text) - Convert.ToInt32(Item2Price.Text)) + ((Convert.ToInt32(Item3Output.Text) - Convert.ToInt32(Item3Price.Text)) + ((Convert.ToInt32(Item4Output.Text) - Convert.ToInt32(Item4Price.Text)).ToString("C2"));
}
private void Total_TextChanged(object sender, EventArgs e)
{
Total.Text = ((Convert.ToInt32(Item1Output)) + (Convert.ToInt32(Item1Output)) + (Convert.ToInt32(Item1Output)) + (Convert.ToInt32(Item1Output)).ToString("C2"));
}
}
}
The first problem I'm having is whenever I do type into Item1Price, it outputs to Item1Output but it doesn't work with the others, and the "salestax" and "total" textboxes don't show anything either.
The second problem is I can't type a number like "O.OO" but I can do "00" and whenever I delete the number, it crashes.
Any help would be really appreciated. Thank you in advance.
First thing its not in good fashion to post coding assignment in a forum to get help with for a job. If you cant complete the coding assignment by yourself I would suggest you reevaluate your skills and ask yourself if your ready for the position that you're trying to obtain. With that said I remember the days in my beginning when I was going after those positions I probably had no business being in at the time so I can sympathize with that. Now to your code.
First of all, you need to surround your conversions in a try catch block, the reason your application is crashing is because 0.00 will not convert into a integer since there is a decimal there. 00 will work but when you delete them your textbox.text value is now nothing which is not going to convert either and your program will crash. So you will need to add logic to handle an empty string value and not do a conversion. I would suggest you use decimal datatype when dealing with money values. As suggested also I would create a method in which you could pass the check box values and return a string value back to set your textbox values. This could clean up your event handler code cause it seems your calculations are the same.

C# Guessing Game - Windows Form App - How to configure a 'PlayAgainButton' and keep total score

I'm new with c# and I need some help with my little app.
I want to have a button which starts the game over again after you are done, but the program needs to keep the total score. (you receive points for guessing)
Thanks in advance,
Philip
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GuessingGame
{
public partial class TheUltimateGuessingGame : Form
{
public TheUltimateGuessingGame()
{
InitializeComponent();
}
int number;
int numberoftries;
int points;
int total = 0;
private void Form1_Load(object sender, EventArgs e)
{
Random generator = new Random();
number = generator.Next(0, 101);
MessageBox.Show("Try to guess a number between 1 and 100");
}
private void PlayAgainButton_Click(object sender, EventArgs e)
{
InitializeComponent();
}
private void button_guess_Click(object sender, EventArgs e)
{
int guess = Convert.ToInt32(textBox_guess.Text);
if (guess > number)
{
textbox_showdifference.Text = ("Guess was too high, try again!");
}
if (guess < number)
{
textbox_showdifference.Text = ("Guess was too low, try again!");
}
if (guess == number)
{
textbox_showdifference.Text = ("Awesome, guess was right!");
MessageBox.Show("It took you " + Convert.ToString(numberoftries) + " tries
to guess the right number");
if (numberoftries <= 3)
{
points = 10;
}
if (numberoftries >= 4 && numberoftries <= 6)
{
points = 5;
}
if (numberoftries >= 7)
{
points = 2;
}
total = total + points;
ScoreBoard1.Text = ("Here are your points --> " + points + "\nHere are your total points --> " + total);
}
++numberoftries;
}
private void exitbutton_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Why not this?
private void PlayAgainButton_Click(object sender, EventArgs e)
{
Random generator = new Random();
number = generator.Next(0, 101);
numberoftries = 0;
MessageBox.Show("Try to guess a number between 1 and 100");
}
InitializeComponent is the method that parses your XAML to build the form. It's not intended to reset the state of your object.

Categories

Resources