1

我的程序将提供有关建筑物的信息,这些信息存储在在线 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,此时程序将打印“谢谢您的时间”并停止。

任何帮助将不胜感激。

4

3 回答 3

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
         }

}
于 2010-12-22T12:21:10.520 回答
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);
    }
}
于 2010-12-22T12:24:25.360 回答
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
        */      
    }
}
于 2010-12-22T12:25:00.570 回答