做问题java

时间:2010-12-22 12:18:23

标签: java while-loop do-while

我的程序将提供有关建筑物的信息,这些信息存储在在线Oracle数据库中。我得到它要求用户输入建筑物编号,然后运行并显示几个查询的结果。

这是我到目前为止所得到的:

String userbnumber = null;
try {
    BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Please enter the building number: "); 
    userbnumber = obj.readLine();
} catch (IOException e) {   
}
// A Lot of queries which use userbnumber as the basis for getting data from tables

然而,我无法弄清楚的是如何/在何处使用do-while子句继续询问建筑物编号,直到用户输入0,此时程序将打印“谢谢你的时间”和停止。

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:3)

 BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
 do {
     try {
         System.out.print("Please enter the building number: "); 
         userbnumber = obj.readLine();
         if(!usernumber.equals("0"){
         //perform action here
         }
     } catch (IOException e) { 
         //handle it
     }
 } while(!userbnumber.equals("0"));  

更好的方法是

while(true){
        try {
             System.out.print("Please enter the building number: "); 
             userbnumber = obj.readLine();
             if(usernumber.equals("0"){
                     break;
             }
             //perform action here
         } catch (IOException e) { 
             //handle it
         }

}

答案 1 :(得分:1)

package pack34;

import java.sql.*;
import java.io.*;

public class Letstryagain34 {
    public static void main(String args[]) 
    {
    String userbnumber = null;
    try {
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Please enter the building number: "); 
        userbnumber = obj.readLine();
        }
        catch (IOException e) { 
             // handle it!
        }
        /*A Lot of queries which use userbnumber as the basis for getting data from tables
        */      
do{
/* Fetch the Question from Database & write back the answer here*/

        System.out.print("Want to continue :  "); 
int choice = obj.readLine();
}while(choice!=0);
    }
}

答案 2 :(得分:1)

package pack34;

import java.sql.*;
import java.io.*;

public class Letstryagain34 {
    public static void main(String args[]) 
    {
    String userbnumber = null;
    try {
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
        do{
            System.out.print("Please enter the building number: "); 
            userbnumber = obj.readLine();
        }while(!"0".equals(userbnumber))
            System.out.println("Thank you for your time");
        }
        catch (IOException e) { 
            System.out.println("Argh! Nasty error..");
        }
        /*A Lot of queries which use userbnumber as the basis for getting data from tables
        */      
    }
}