我正在尝试转换在文本框中输入的数字。但我找不到合适的理由,不起作用。你也请看看。谢谢你提前!
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 NumberToText
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void NumberControl()
{
if ((txtNumber.Text.Length>7))
{
MessageBox.Show("Please enter a smaller number");
}
}
public void ReadNumber()
{
try
{
int ones, tens, hundreds, thousands, tenthousands, hundredthousands,
millions;
int number = Convert.ToInt32(txtNumber.Text);
int[] array=new int[7];
for (int j = 0; j < txtNumber.Text.Length; )
{
array[j] = (number / (10 ^ (txtNumber.Text.Length -
(txtNumber.Text.Length - j)))) % 10;
j += 1;
}
if (txtSayi.Text.Length != 7)
{
for (int i = 6; i >= txtNumber.Text.Length; )
{
dizi[i] = 0;
i-=1;
}
}
ones = array[0];
tens = array[1];
hundreds = array[2];
thousands = array[3];
tenthousands = array[4];
hundredthousands = array[5];
millions = array[6];
//Converting to numbers in TURKISH Text
string[] a_ones = { "", "Bir", "İki", "Üç", "Dört", "Beş", "Altı",
"Yedi", "Sekiz", "Dokuz" };
string[] a_tens = { "", "On", "Yirmi", "Otuz", "Kırk", "Elli",
"Altmış", "Yetmiş", "Seksen", "Doksan" };
string[] a_hundreds = { "", "Yüz", "İkiyüz", "Üçyüz", "Dörtyüz",
"Beşyüz", "Altıyüz", "Yediyüz", "Sekizyüz", "Dokuzyüz" };
string[] a_thousands = { "", "Bin", "İkibin", "Üçbin", "Dörtbin",
"Beşbin", "Altıbin", "Yedibin", "Sekizbin", "Dokuzbin" };
string[] a_tenthousands = { "", "On", "Yirmi", "Otuz", "Kırk",
"Elli", "Altmış", "Yetmiş", "Seksen", "Doksan" };
string[] a_hundredthousands = { "", "Yüz", "İkiyüz", "Üçyüz",
"Dörtyüz", "Beşyüz", "Altıyüz", "Yediyüz", "Sekizyüz", "Dokuzyüz" };
string[] a_millions = { "", "Birmilyon", "İkimilyon", "Üçmilyon",
"Dörtmilyon", "Beşmilyon", "Altımilyon", "Yedimilyon", "Sekizmilyon", "Dokuzmilyon"
};
lblText.Text = a_millions[millions] + " " + a_hundredthousands
[hundredthousands] + a_tenthousands[tenthousands] + " " + a_thousands[thousands] + "
" + a_hundreds[hundreds] + " " + a_tens[tens] + " " + a_ones[ones];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void btnConvert_Click(object sender, EventArgs e)
{
NumberControl();
ReadNumber();
}
}
}
答案 0 :(得分:1)
for (int j = 0; j < txtNumber.Text.Length; )
{
array[j] = (number / (10 ^ (txtNumber.Text.Length - (txtNumber.Text.Length - j)))) % 10;
j += 1;
}
(txtNumber.Text.Length - (txtNumber.Text.Length - j))
是j
,所以:
for (int j = 0; j < txtNumber.Text.Length; j += 1)
{
array[j] = (number / (10 ^ j)) % 10;
}
你和j一起10岁?
另外,“Sekizmilyon”不是一个字。你需要在两者之间放一个空格。
答案 1 :(得分:0)
当你可能意味着^
时,看起来你正试图用Math.Pow
来计算10的幂。使用建议的减少 aib ,您的行:
array[j] = (number / (10 ^ (txtNumber.Text.Length - (txtNumber.Text.Length - j)))) % 10;
变为:
array[j] = (number / (int)Math.Pow(10, j)) % 10;
其他一些建议:
更改NumberControl
以返回布尔值,以便在输入的数字过大时跳过对ReadNumber
的呼叫。目前它进入ReadNumber
即使数字超过7位数,这导致数组超出界限错误
以if (txtSayi.Text.Length != 7)
开头的代码块似乎是多余的。