例3-7和例3-8
public class Example3_7{
public static void main(String args[]){
int sum=0,i=0,max=8888,number=7;
while(true){
i++;
sum=sum+i;
if(sum>=max)
break;
}
System.out.println("1+2+...n<"+max+"的最大整数n是:"+(i-1));
for(i=1,max=200,sum=0;i<=max;i++){
if(i%number!=0){
continue;
}
sum=sum+i;
}
System.out.println(max+"内能被"+number+"整除的数字之和:"+sum);
}
}
import java.util.Scanner;
public class Example3_8{
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+"个元素");
}
}