问题描述
我创建了一个选择排序代码,我非常确定排序部分是否有效但我需要将最终数组显示为单个文本字段名称txtselectionS
我尝试过:
I created a selecting sorting code, im pretty sure the sorting part is working but i need to display the final array into a single text field name "txtselectionS"
What I have tried:
private void btnSelectionSortActionPerformed(java.awt.event.ActionEvent evt) {
int a1= Integer.parseInt(txta1.getText());
int a2= Integer.parseInt(txta2.getText());
int a3= Integer.parseInt(txta3.getText());
int a4= Integer.parseInt(txta4.getText());
int a5= Integer.parseInt(txta5.getText());
int a6= Integer.parseInt(txta6.getText());
int a7= Integer.parseInt(txta7.getText());
int[] arr1 = {a1,a2,a3,a4,a5,a6,a7};
int[] arr2 = doSelectionSort(arr1);
for(int i:arr2){
String ms = Integer.toString(arr2[]);
txtselectionS.setText(ms);
/*
int count=1;
String sa = Integer.toString(i);
if(count<=7)
{
txtselectionS.setText(sa+",");
count++;
}
*/
//txtselectionS.setText(", ");
//System.out.print(i);
//System.out.print(", ");
//}
}
public static int[] doSelectionSort(int[] arr)
{
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Sorting().setVisible(true);
}
});
}
推荐答案
我非常确定排序部分正在工作
im pretty sure the sorting part is working
非常确定是最好的失败方式,有时它是致命的。
当你不理解你的代码在做什么或为什么它做它的作用时,答案是调试器。
使用调试器查看你的代码是在做。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。
[]
[]
调试器在这里显示你的代码是什么做和你的任务是比较它应该做什么。
调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
Being pretty sure is the best way to failure, and sometimes it is deadly.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
这篇关于在文本字段上显示排序结果不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!