运算符,语句和表达式有关的程序
来源:互联网 发布:淘宝店铺模块制作 编辑:程序博客网 时间:2024/05/19 02:02
使用while循环和折半法查找一个整数是否在一个排序的int类型数组中。
import java.util.Scanner;
public class Example1_4{
public static void main(String args[]){
int n,start,end,middle;
int a[]={-2,1,4,5,8,12,17,23,45,56,90,100};
start=0;
end=a.length;
middle=(start+end)/2;
int count=0;
Scanner reader=new Scanner(System.in);
System.out.print("请输入一个整数:");
n=reader.nextInt();
while(n!=a[middle]){
if(n>a[middle]){
start=middle;
}
else if(n<a[middle]){
end=middle;
}
middle=(start+end)/2;
count++;
if(count>a.length/2)
break;
}
if(count>a.length/2)
System.out.println(n+"不在数组中");
else
System.out.println(n+"是数组中的第"+middle+"个元素");
}
}
结果:
- 运算符,语句和表达式的有关程序
- 运算符,语句和表达式的有关程序
- 运算符,语句和表达式的有关程序
- 运算符,语句和表达式有关的程序
- java的运算符,表达式和语句
- 运算符、表达式和语句
- 运算符、表达式和语句
- 运算符、表达式和语句
- 运算符、表达式和语句
- 运算符、表达式和语句
- 运算符、表达式和语句
- 运算符、表达式和语句
- 运算符表达式和语句
- 运算符、表达式和语句
- 运算符表达式和语句
- C语言的运算符、表达式和语句
- 常量、变量、运算符、表达式和赋值语句的使用
- java的运算符,表达式和语句概述
- itunes备份的文件在哪个目录的
- 大话存储系列17——GFS 文件系统
- qt编译过程
- 运算符,语句和表达式的有关程序
- 求数组的最长递增子序列VS吉哥系列故事——完美队形
- 运算符,语句和表达式有关的程序
- printf函数原理以及可变参函数
- WCF笔记(6)消息拦截与篡改
- DataGridView简单介绍
- ios登陆界面
- c语言作业第三次
- Android各种控件的事件监听 (转)
- html radio
- Oracle Internal Events Introduction(eygle)