📄 linearsearch.java
字号:
//线性搜索,在一列安中搜索一个数。
import javax.swing.JOptionPane;
public class LinearSearch {
/*主方法体*/
static final int LISTLENGTH=10;
public static void main(String []args){
int []list= new int[LISTLENGTH];
//声明和初始化输出字符串`
String outPut= "";
//随机的创建表并显示
outPut +="这个数列是\n";
for(int i=0;i<list.length;i++){
list[i]=(int)(Math.random()*100);
outPut +=list[i]+" ";
}
//显示表.
JOptionPane.showMessageDialog(null,outPut,"例子 (数列)",
JOptionPane.INFORMATION_MESSAGE);
//提示输入
String keyString = JOptionPane.showInputDialog(null,"输入键值:",
"输入",JOptionPane.QUESTION_MESSAGE);
//转换成整数
int key = Integer.parseInt(keyString);
//Empty the outPut string.
outPut= "";
//寻找值
int index = linearSearch(key,list);
if(index!=-1)
outPut = "找到,其下标是 "+ index;
else
outPut = "键值是 "+ key+" 在列表中没有找到";
//Display the result.
JOptionPane.showMessageDialog(null,outPut,"输出示例",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
/*用于找值的方法*/
public static int linearSearch(int key, int []list){
for(int i=0;i<list.length;i++)
if(key==list[i])
return i;
return -1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -