变量left,right和found都要更新一次====>left或right或found更新.
left或right都在向中心靠. 仔细看一看二分法搜索的书.

解决方案 »

  1.   

    if ( key < list[middle] )
          right = middle - 1 ;
        else if ( key > list[middle] )
          left = middle + 1 ;
    这里只可能符合一个!也只有一个语句会被执行!
    不可能下面两句同时执行!
    right = middle - 1 ;       left = middle + 1 ;while loop会循环的条件是left < right 而且 !found 
    也就是found为假时,循环执行!
    以上实际是个折半查找数据的算法!boolean i=true;
    while i{
      System.out.println("QQQQ");
    }
    就是一个死循环!
      

  2.   

    谢谢2为兄弟的解释,狂人一个你的解释我明白,但我不明白的是while loop循环一次,while(left<right&&!found)这里面的3各变量都必须更新(不能只更新1个或2个,必须3个一起更新),而这里无论执行哪一个条件都是只更新一个变量,另外2各变量都没更新,按理说要执行while loop必须while(condition)小括号里的所有变量都要更新,不能循环1次只更新1个变量而另外一个变量不更新,如果另外一个变量不更新的话,while loop 就会无限循环。可这里只更新了1各变量,但while loop还是执行的,我就想不明白了,大家再看看这1段代码,也是1次只更新1各变量,另外1个变量没更新,但while loop还是执行的。
    import javax.swing.*;public class Solution{ /**
     * PURPOSE: an application that allows a user to simulate driving a car
     *
     * @return  VOID or description of the value returned
     */
    public static void main(String[] args){ //create the car to use throughout the application
    Car myCar = new Car(30,20);
    boolean arrived = false, outOfGas = false; //events to monitor
    String input;
    int command, kilos = 0, speed = 0, litres = 0; //store parameters from input //while we haven't gotten there yet and we have gas in the tank
    while(!arrived && !outOfGas){ //get command from user
    input = JOptionPane.showInputDialog("Enter Command\n 0 = Drive\n 1 = Fill Tank\n 2 = Arrived");
    command = Integer.parseInt(input); //drive: need to get kilometers and speed from user
    if(command == 0){
    input = JOptionPane.showInputDialog("How many kilometres?");
    kilos = Integer.parseInt(input);
    input = JOptionPane.showInputDialog("How fast?");
    speed = Integer.parseInt(input);
    myCar.drive(kilos,speed);
    }
    else
    //fill tank: need to get the number of litres from user
    if(command == 1){
    input = JOptionPane.showInputDialog("How many litres?");
    litres = Integer.parseInt(input);
    myCar.fillTank(litres);
    }
    else
    //arrived: set arrived to true to signal the event
    if(command == 2){
    arrived = true;
    System.out.println("You've arrived at your destination!!");
    } //if we have gas left report how much, or indicate that we are out
    if(myCar.getFuelLevel() <= 0){
    System.out.println("Your out of gas... hope you have CAA!");
    outOfGas = true;
    }
    else
    System.out.println("Current fuel level: " + myCar.getFuelLevel()); }
    System.exit(0);
    }
    }
      

  3.   

    按理说要执行while loop必须while(condition)小括号里的所有变量都要更新,不能循环1次只更新1个变量而另外一个变量不更新,如果另外一个变量不更新的话,while loop 就会无限循环。=======>谁说的?
    int x = 10;
    int count=0;
     while(count < x) {
       count++;
     }