两个类,是模拟一个酒店管理,先输入管理者密码,再1是全部查询,2是个别查询,查不知道为什么,进入后总是要按两次2才能个别查询
import java.io.InputStreamReader;
import java.io.BufferedReader;
class Account {
String h_number1 = "01";  //不用String
  String password1 = "01"; 
  String name1 = "zz";
  int money1 = 200;

String h_number2 = "02";
  String password2 = "02"; 
  String name2 = "kk";
  int money2 = 220; String h_number3 = "03";
  String password3 = "03"; 
  String name3 = "jj";
  int money3 = 230;   String number = "11";
  String passwd = "11";
 
//用户及客人信息   BufferedReader str = new BufferedReader(new InputStreamReader(System.in));
 String getInfo()
 {
  String info = "";
try {
info = str.readLine();
    }
    catch (Exception ex) {
     ex.printStackTrace();
    
    }
    return info;
 }
//获取用户输入字符  
 void adminlogin()
 {
  System.out.print("Welcome sir,please input your number: ");
  if(getInfo().equals(number))
  {
  System.out.println("Thank you for the number");
  System.out.print("Please input your own password: ");
  if(getInfo().equals(passwd))
  {
  System.out.println("Login success");
  }
  else
  {
  System.out.println("Login fail,Retry or exit?");
  System.out.println("Retry(R),Exit(Others)");
  if(getInfo().equals("R"))//R or r
  {
  adminlogin();
  }
  else
  {
  //关闭窗口
  }
  }
  }
  else
  {
  System.out.println("The number wrong,Please check it");
  System.out.println(str);
  System.out.println("Login fail,Retry or exit?");
  System.out.println("Retry(R),Exit(Others)");
  if(getInfo().equals("R"))
  {
  adminlogin();
  }
  else
  {
 
  }
  }
 }
//管理员进入  
 
 void notice()
 {
  System.out.println("***************************** ");
  System.out.println("Please Read Our Readem");
  System.out.println("1:Query All Guests");
  System.out.println("2:Query the Detail of Special Geust");
  System.out.println("3:Exit");
  System.out.println("***************************** ");
 }
//选择菜单  
 
 void all()
 {
  System.out.println("====================================");
  System.out.println("The room number "+h_number1+" Guest is "+name1);
  System.out.println("The room number "+h_number2+" Guest is "+name2);
  System.out.println("The room number "+h_number3+" Guest is "+name3);
  System.out.println("====================================");
 }
//查所有用户  
 
 void alone()
 {
  System.out.print("Please select which Guest Number: ");
  String get = getInfo();
  if(get.equals(h_number1))
  {
  System.out.println("==============================");
  System.out.println("The Guest name is "+name1);
  System.out.println("The Room Number is "+h_number1);
  System.out.println("The Room password is "+password1);
  System.out.println("The Room Charge is "+money1);
  System.out.println("==============================");
  }
  else if(get.equals(h_number2))
  {
  System.out.println("==============================");
  System.out.println("The Guest name is "+name2);
  System.out.println("The Room Number is "+h_number2);
  System.out.println("The Room password is "+password2);
  System.out.println("The Room Charge is "+money2);
  System.out.println("==============================");
  }
  else if(get.equals(h_number3))
  {
  System.out.println("==============================");
  System.out.println("The Guest name is "+name3);
  System.out.println("The Room Number is" +h_number3);
  System.out.println("The Room password is "+password3);
  System.out.println("The Room Charge is "+money3);
  System.out.println("==============================");
  }
  else
  {
  System.out.println("==============================");
  System.out.println("The Number is not exist,Please check it");
  alone();
  }
 }
//查询单独用户  void welcome()
 {
  notice();
  if(getInfo().equals("1"))
  {
  all();
  welcome();
  }
  if(getInfo().equals("2"))
  {
  alone();
welcome();
  }
  else
  {
 
  }
 }
 
}这是第二个类class Counter
{
public static void main(String []args)
{
Account acc = new Account();
acc.adminlogin();
acc.welcome();

}
}
另外想问下如何关闭窗口?
多谢各位

解决方案 »

  1.   

    你这是dos窗口,关闭窗口就System.exit(0);
      

  2.   

    void welcome()中的if(getInfo().equals("2"))语句要输入一次;对alone();的调用String get = getInfo();又要输入一次;当然是2次输入才可以单个查询了啊!
      

  3.   

    还有就是关闭窗口就用System.exit(0);就OK了!
      

  4.   

    在welcome方法里面写错了,你用了两次getInfo,在判断等于1的时候要输入一次,然而你输入不是1的话,第二条if语句又要输入一次了。
    改成如下:
             void welcome()
     {
      notice();
      String tempStr=getInfo();
      if(tempStr.equals("1"))
      {
      all();
      welcome();
      }
      else if(tempStr.equals("2"))
      {
      alone();
    welcome();
      }
      else
      {
     
      }
     }