【发布时间】:2020-12-30 13:47:57
【问题描述】:
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private int n;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
string scrambledWord = textBox1.Text;
if (e.KeyCode == Keys.Enter)
{
label3.Text = scrambledWord;
int n = scrambledWord.Length;
permute(scrambledWord, 0, n - 1);
}
}
private void permute(String scrambledWord,
int l, int r)
{
if (l == r)
label3.Text = scrambledWord;
else
{
for (int i = l; i <= r; i++)
{
scrambledWord = swap(scrambledWord, l, i);
permute(scrambledWord, l + 1, r);
scrambledWord = swap(scrambledWord, l, i);
}
}
}
/**
* Swap Characters at position
* @param a string value
* @param i position 1
* @param j position 2
* @return swapped string
*/
public static String swap(String a,
int i, int j)
{
char temp;
char[] charArray = a.ToCharArray();
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
string s = new string(charArray);
return s;
}
}
}
它只打印排列的第一行,我希望它打印所有排列。我是 WinForms 的新手。请帮忙,谢谢!
【问题讨论】:
-
多行?使用文本框。并将属性 multiline 设置为 true。类似stackoverflow.com/questions/8536958/…
-
Youe Label 可以很好地显示多行,并且是一个比 TextBox 简单得多的控件。当然,您需要插入换行符和/或换行符。它不会自行缠绕;并且用户将能够从标签复制。
-
@TaW 我该怎么做?
-
真的吗?如何将字符或字符串插入到正在构建的字符串中?在为下一行添加数据之前添加
"\n"或"\r"或"\r\n"..! -
@TaW 我试过它不起作用。不过还是谢谢你。对不起,我是一个菜鸟,我 14 岁,刚接触 C#
标签: c# string visual-studio winforms permutation