首先我想爲凌亂代碼道歉。目前在大學一年級,有一個問題,我自己或我的朋友都不知道。該問題要求接受來自用戶的美元輸入,並找出多少二十,幾十,五和一個將增加最少數量的賬單。建立解決方案導致沒有錯誤,但一旦運行並且數據被輸入即刻崩潰。我嘗試使用調試器無濟於事,以及更改代碼(添加'if''then'語句等)。我通常是'while'循環的新手,並且一般編碼,所以如果我正在製作愚蠢的錯誤隨時烤我。 先進的謝謝!Windows窗體應用程序崩潰同時循環
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 Dollars
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void btnFindDem_Click(object sender, EventArgs e)
{
int twenties;
int tens;
int fives;
int singles;
int totDollars;
int sub1;
int sub2;
int sub3;
int sub4;
totDollars = Convert.ToInt32(txtDollars.Text);
twenties = 0;
tens = 0;
fives = 0;
singles = 0;
sub1 = totDollars;
sub2 = totDollars;
sub3 = totDollars;
sub4 = totDollars;
lstOut.Items.Add("Total dollars = " + totDollars.ToString("c"));
while (totDollars >= 20)
{
sub1 = sub1 - 20;
twenties = twenties + 1;
}
while (totDollars >= 10)
{
sub2 = sub2 - 10;
tens = tens + 1;
}
while (totDollars >= 5)
{
sub3 = sub3 - 5;
fives = fives + 1;
}
while (totDollars >= 1)
{
sub4 = sub4 - 1;
singles = singles + 1;
}
lstOut.Items.Add("Twenties: " + twenties);
lstOut.Items.Add("Tens: " + tens);
lstOut.Items.Add("Fives: " + fives);
lstOut.Items.Add("Ones: " + singles);
}
}
}
你是什麼意思的「崩潰」。正在報告什麼異常?使用Visual Studio的調試器運行時會發生什麼? – hatchet
我得到的只是'沒有迴應',它只是坐在那裏,直到我結束這個過程。 –
你爲什麼要從這些'sub'變量中減去而不是'totDollars'?既然你沒有從totDollars中減去,你的第一個循環將是無限的。 – hatchet