/*
编写一个Java Application 程序,随机产生10个整数,然后按照冒泡排序法排序,接着等待用户输入一个任意整数,
按照折半查找算法在这10个数中查找,将查找结果显示在一个 200*200的窗口中。如果有则在窗口中显示该数,否则显示“No such number!”。
*/
//: SortTest.java
import java.awt.event.*;
import javax.swing.*;
public class SortTest {
public static void main(String[] args){
new SortFrame().setVisible(true);
}
}
class SortFrame extends JFrame{
private JTextField input;
private JButton bt;
private JLabel inf;
public SortFrame(){
super("Sort");
this.getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,300);
final SortModel model = new SortModel(10);
JLabel label;
label=new JLabel("排序前数据:");
label.setBounds(10,10,60,21);
this.add(label);
for(int i=0; i
label.setBounds(10,34+i*22,60,21);
this.add(label);
}
label=new JLabel("排序后数据:");
label.setBounds(80,10,60,21);
this.add(label);
model.sort();
for(int i=0; i
label.setBounds(80,34+i*22,60,21);
this.add(label);
}
label = new JLabel("查找:");
label.setBounds(150,10,40,22);
this.add(label);
input = new JTextField();
input.setBounds(label.getX()+label.getWidth(),10,50,22);
this.add(input);
inf = new JLabel("结果:");
inf.setBounds(label.getX(),55,200,22);
this.add(inf);
bt = new JButton("开始");
bt.setBounds(150,100,80,22);
this.add(bt);
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try{
int x = Integer.parseInt(input.getText().trim());
int index = model.findNumber(x);
inf.setText(index>-1?"位置: "+(index+1)+" 找到了 "+x:"No such number!");
}catch(Exception ex){
input.selectAll();
inf.setText("查找的只能是数字");
}
input.requestFocus();
}
});
}
}
class SortModel {
private int[] dat;
public SortModel(int len){
init(len);
}
private void init(int len) {
dat=new int[len];
for(int i=0; i
do t=(int)(Math.random()*len*len);
while(contains(t));
dat[i]=t;
}
}
private boolean contains(int t) {
for(int i=0; i
return false;
}
public int[] getData(){return dat;}
//这个是排序法
public void sort(){
for(int i=0; i
dat[i]^=dat[j];
dat[j]^=dat[i];
dat[i]^=dat[j];
}
}
}
}
//这个难道就是传说中的 折半查找法?
public int findNumber(int x){
int min = 0;
int max = dat.length-1;
int mid = (max + min)/2;
while(x!=dat[mid]&&min<=max){
if(dat[mid]>x)
max = mid - 1;
else
min = mid + 1;
mid = (max+min)/2;
}
return dat[mid]==x?mid:-1;
}
}
刚下课,课间临时写的。只要复制到任何文本编辑器下,再编译后运行Test.class文件后就可以执行。。
编译通过
package java;
import javax.swing.*;
import java.util.*;
class Test{
public static void main(String[] args){
int key;
String result;
boolean rs;
Scanner in = new Scanner(System.in);
key = in.nextInt();
System.out.println();
QueryArray qa = new QueryArray(key);
rs = qa.getResult();
if(rs==true) result = ""+key;
else result = "No such number!";
//显示排序好的数组。
int[] array = new int[10];
RandomArray rm = new RandomArray();
array = rm.getArray();
for(int i=0;i<10;i++){
System.out.println (array[i]);
}
//显示图形化界面
JFrame frame = new JFrame();
frame.getContentPane().add(new JLabel(result));
frame.setSize(200,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class RandomArray{
private int[] array = new int[10];
//构造方法,获得按冒泡法排序好的数组
public RandomArray(){
this.array = sequenceArray();
}
//获得随即数组
private int[] setRandomArray(){
int i;
int[] array = new int[10];
for(i=0;i<10;i++){
array[i] = (int)((Math.random())*100);
}
return array;
}
//按冒泡法对数组进行排序
private int[] sequenceArray(){
int j,k,temp;
int[] array = new int[10];
array = setRandomArray();
for(j=0;j<10;j++){
for(k=0;k<9-j;k++){
if(array[k+1]
array[k+1] = array[k];
array[k]=temp;
}
}
}
return array;
}
//设置get方法,是对象可以获取排序好的数组
public int[] getArray(){
return array;
}
}
class QueryArray{
private int key;
//带一个参数构造函数,创建实例时给全局属性赋值
public QueryArray(int key){
this.key = key;
}
public boolean getResult(){
RandomArray ra = new RandomArray();
int[] array = ra.getArray();
int low,high,mid;
low = 0;
high = 9;
while (low<=high){
mid=(int)((low+high)/2);
if(key
else if(key>array[mid])
low = mid+1;
else return true;
}
return false;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class Data {
private Random ran;
private int[] arr;
private BufferedReader buf;
public void setData() {
ran = new Random();
arr = new int [10];
for(int i=0;i
arr[i] = r;
}
sort();
buf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("search one data");
try {
int searchNum = Integer.parseInt(buf.readLine());
search(searchNum);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//冒泡
public void sort() {
int t = 0;
for (int m = 0; m < 9; m++) {
for (int n = 0; n < 9 - m; n++) {
if (arr[n] > arr[n + 1]) {
t = arr[n];
arr[n] = arr[n + 1];
arr[n + 1] = t;
}
}
}
for (int i = 0; i < 10; i++) {
System.out.println(arr[i]);
}
}
//折半
public void search(int searchNum){
int start,end,middle;
start = 0;
end = arr.length-1;
middle = (start + end)/2;
int count=0;
while(searchNum!=arr[middle])
{
if(searchNum>arr[middle])
{
start = middle;
}
else if(searchNum
end = middle;
}
middle=(start + end)/2;
count++;
if(count>arr.length/2)
break;
}
String str = "";
if(count>arr.length/2) {
str = "No such number!";
}
else {
str = String.valueOf(searchNum);
}
Window window = new Window();
window.init(str);
}
public static void main(String[] args) {
Data data = new Data();
data.setData();
}
}
-----------------
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Window extends JFrame{
private static final long serialVersionUID = 1L;
private JLabel lblView;
public void init(String str){
this.setSize(200, 200);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
lblView = new JLabel();
lblView.setText(str);
this.getContentPane().add(lblView);
this.pack();
this.setVisible(true);
}
}