我正在编码一种算法,该算法生成随机数,对它们进行排序,然后通过插值搜索查找某个数字,如果找到该数字,则它应显示该数字所在的数组索引,如果没有,则显示数字not找到
所以我有4个文本字段和3个按钮 第一个按钮应生成数字并将其显示在第一个字段中 第二个按钮可以对数字进行排序并将其显示在第二个字段中 如果在第4个文本字段的数组中找到该数字,则第3个按钮应使用我在第3个文本字段中输入的数字“ x”并显示该数字所在的索引。
public int[] array = new int[6];
int l = 0;
int r = array.length - 1;
int versch;
int pos;
public int x;
private void genactionPerformed(java.awt.event.actionEvent evt) {
// TODO add your handling code here:
for (int i = 0; i < 6; i++) {
array [i] = (int) (Math.random() * 49 + 1);
f1.setText(Arrays.toString(array));
}
private void b1actionPerformed(java.awt.event.actionEvent evt) {
// TODO add your handling code here:
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp;
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
t2.setText(Arrays.toString(array));
}}
}
public int inter( ) {
x=Integer.parseInt(a.getText());
while( x >= array[l] && x <= array[r] ){
versch = array[l] - array[r];
pos = l + (int)(((double)r - l) * (x - array[l])
/ versch);
if( x > array[pos] )
{ l = pos + 1; }
else if( x < array[pos] )
{ r = pos - 1; }
else
{return pos; }
}
return -1;
}
private void b3actionPerformed(java.awt.event.actionEvent evt) {
if(x==array[pos])
{aus.setText("the number is located at" + array[pos]);
}
}
程序没有显示任何错误,但是按钮b3应该可以显示数字x所处的位置,该按钮无法正常工作,就像什么也没发生 我已经尝试过用setText部件替换返回部件,但是代码根本不起作用,因此看起来我真的需要返回部件。 但问题是,我认为退回零件弄乱了我的输出,因此找不到我的电话号码,因此不会在最后一个文本字段中显示它? 也尝试将整个代码放在按钮下,但看起来我无法使用返回函数,这就是为什么我决定将代码与现在位于按钮下的输出代码分开的原因 请帮帮我,我真的不知道该怎么做才能使它正常工作。我的Java知识和编程一般来说都不是很好