/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sequenceSearch;
/**
*
* @author lupohsun
*/
import javax.swing.JOptionPane;
import java.util.Random;
public class Main {
/**
* @param args the command line arguments
*/
private int count = 0; //計算時間複雜度
public void setCount(int c) {
this.count = c;
}
public int getCount() {
return this.count;
}
//計算時間複雜度方法
public void count() {
int c = getCount();
c = c+1;
setCount(c);
}
public static void main(String[] args) {
// TODO code application logic here
String s;
s = JOptionPane.showInputDialog("請輸入變數的數量");
int n;
n = Integer.parseInt(s); /*變數的數量為n*/
s = JOptionPane.showInputDialog("請輸入key值\n請注意變數的值為0到99");
int x;
x = Integer.parseInt(s);
s = "變數的數量為:" +n +"\nkey值為:" +x;
JOptionPane.showMessageDialog(null,s); /*顯示已輸入的變數數量以及key值*/
/*以下為亂數產生,產生值的範圍為0~99*/
Random random = new Random(); /*random是一個static method,不能直接呼叫*/
int[] array = new int[n]; /*宣告一個大小為n的陣列*/
for(int i=0;i
System.out.println("array[" +i +"]:" +array[i]); /*印出陣列中的所有變數*/
}
/*sequencesearch*/
Main main = new Main();
int result = 0;
for(int i=0;i
if(array[i] == x)
result = i;
}
if(result != 0)
s = "key值的位置在 array[" +result +"]\n比較次數為" +main.getCount();
else
s = "所有變數中找不到此key值\n比較次數為" +main.getCount();
JOptionPane.showMessageDialog(null,s); //顯示key值的位置
}
}
留言列表