Java,使用多个条件语句的问题,while循环,for循环,if语句

时间:2017-12-13 18:57:35

标签: java for-loop if-statement while-loop

我正在制定一项计划,帮助将客人注册到派对上。

党必须存在于数据

当然,所需的门票数量必须少于可用的门票

我真的很难再添加一个条件 - 检查输入是否高于0。

我已将整个for循环放入另一个if语句中,如您所见: if(ticketsWanted> 0)

如果已输入1以下的内容,则打印“请输入正数”

但是,它还会打印“此方不存在或已完全预订”。再次..

基本上在System.out.println之后(“请输入正数”); 我希望程序提供再次输入数字的选项,直到它为正

所以这是我的问题:

我如何使用while循环使其循环直到输入正数?

是否有更有效/更好的方式来做我打算做的事情?

谢谢

 private static void signUp2(Guest validGuest){

              input.nextLine();
              System.out.println("Enter the name of the party");
              String partyName = input.nextLine();
              System.out.println("How many tickets the guest wishes to purchase?");
              int ticketsWanted = input.nextInt();
              boolean check = false;

              if(ticketsWanted > 0) {
               for(Party p: parties){
                 if(p.getPartyName().equals(partyName)){
                     if(ticketsWanted > e.getTickets()){
                 }
                   else{
                     check = true;  
                     validGuest.AddPartiesAndTickets(p, ticketsWanted);
                    }

                 }
              }
            }
             else{
                System.out.println("Please enter a positive number");

               }

            if(check == false){
            System.out.println("This party does not exist or is fully booked.");

5 个答案:

答案 0 :(得分:2)

添加方法:

int getNumberTickets() {
    System.out.println("How many tickets the guest wishes to purchase?");
    int ticketsWanted = input.nextInt();
    return(ticketsWanted);
}

并使用:

调用它
int ticketsWanted = 0;
do {
    try {
       ticketsWanted = getNumberTickets();
       if (ticketsWanted < 1)
          throw new RuntimeException("Unused");
     catch(Throwable e) {
         ticketsWanted = -1;
         System.out.println("Invalid amount");
     }
while (ticketsWanted < 1);
 // now do your checks

答案 1 :(得分:0)

 Using the continue keyword


   boolean check = false;
    boolean valid = false;

   while(valid == false){
   Sop( " how many tickets? ");
   ticketsWanted = input.nextInt ();
   if  (ticketsWanted < 0)  
       continue ;
   else   
    {
  valid = true;
// Rest of the code
 }

}

答案 2 :(得分:0)

{
    "timestamp": 1513192593064,
    "status": 400,
    "error": "Bad Request",
    "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
    "message": "JSON parse error: Can not construct instance of com.algoq.algoq.models.POTDResources: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.algoq.algoq.models.POTDResources: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)\n at [Source: java.io.PushbackInputStream@1710624f; line: 3, column: 17] (through reference chain: com.algoq.algoq.models.POTD[\"resources\"]->java.util.ArrayList[0])",
    "path": "/generatepotd"
}

这很有效。

答案 3 :(得分:0)

driver.FindElement(MobileBy.AndroidUIAutomator("new UiScrollable(
    new UiSelector().scrollable(true).instance(0)).scrollIntoView(
    new UiSelector().resourceId(\"" + id + "\").instance(0))"))

而不是每个循环使用流和过滤器而不是if else循环。然后代码看起来可读。如果你包含更多的嵌套循环..这不是最好的做法。

答案 4 :(得分:0)

这是一个非常简单的方法,可以用来确保用户在提示输入大于0的整数时输入正确的值。

  public static int getNumberGreaterThanZero(String prompt)
  {
      int number = 0;
      while(number == 0)
      {
          System.out.println(prompt);
          String inputFromUser = input.nextLine();
          //check if input matches any negative or positive number
          if(inputFromUser.matches("-\\d+|\\d+"))
          {
              number = Integer.parseInt(inputFromUser);
              if(number <= 0)
              {
                  System.out.println(number + " is not greater than 0");
                  number = 0; //have them try again
              }
          }
          else
              System.out.println("Error - " + inputFromUser + " is not a valid number");

      }
      return number;
  }

然后你可以用你的其他方法调用它

          System.out.println("Enter the name of the party");
          String partyName = input.nextLine();
          int ticketsWanted = getNumberGreaterThanZero("How many tickets the guest wishes to purchase?");
          ...