?? 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 + -