【问题标题】:How do I loop my main method in a way?如何以某种方式循环我的主要方法?
【发布时间】:2014-04-16 03:38:37
【问题描述】:

例如,程序会要求用户输入,然后它会产生一些输出然后它就完成了,但是如果用户想通过 main 方法,我怎样才能让它再次循环呢?

这是我的代码:

public static void main(String[] args) {
    Scanner sc = new Scanner(System. in );
    System.out.print("Please enter the first name of the person you would love to know about : ");
    String hisName = sc.next();
    printSomeInfoAbout(hisName);
}

如果用户决定再次运行,我将如何让它再次运行?

【问题讨论】:

标签: java loops main


【解决方案1】:
  public static void main(String[] args) {
    Scanner sc = new Scanner(System. in );
    System.out.print("Please enter the first name of the person you would love to know about : ");
    String hisName = sc.next();
        printSomeInfoAbout(hisName);

  System.out.print("AGAIN (Y/N) : ");  // ask the input from user
    String var= sc.next();
   if(var.equalsIgnoreCase("Y")){// Matches "Y" or "y"
      main(null); // if input is Y then call main again. 
   }
}

【讨论】:

  • 我猜应该是 if(var.equals("Y")) :)
  • @Stunner 是的.. 改变了它。
  • 谢谢!工作 :) 加上我不知道我是否想要重复主要方法我必须添加一个(null)。
【解决方案2】:
String x=null; 
Scanner sc = new Scanner(System. in ); 
String hisName=null; 
 do{    
    System.out.print("Please enter the first name of the person you would love to know about : ");
    hisName = sc.next();
    printSomeInfoAbout(hisName);
    System.out.print("y/n");
    x=sc.next();
    }while(x.equals("y"));

【讨论】:

    【解决方案3】:
    boolean exit = false;
    
    while(!exit){
        Scanner sc = new Scanner(System. in );
        System.out.print("Please enter 'exit' to exit or, the first name of the person you would love to know about : ");
        String hisName = sc.next();
        if(!"exit".equals(hisName)) {
            printSomeInfoAbout(hisName);
        }else{
             exit = true;
        }
    }
    

    【讨论】:

      【解决方案4】:

      创建一个获取输入的方法并在用户选择输入另一个名称时调用它

      【讨论】:

        【解决方案5】:

        一个简单的循环,它将检查用户输入的任何空字符串作为其将退出的名字。

        public static void main(String[] args) {
            Scanner sc = new Scanner(System. in );
            do {
                System.out.print("Please enter the first name of the person you would love to know about : ");
                String hisName = sc.next();
                if (hisName.equals("")) {
                    printSomeInfoAbout(hisName);
                }
            } while (!hisName.equals(""));
        }
        

        【讨论】:

          【解决方案6】:

          请找到下面的代码段,它可能对你有所帮助..

          public static void main(String[] args) {

              String name;
              Scanner scn = new Scanner(System.in);
          
              boolean flag = false;
          
              do {
          
                  System.out.println("Please enter the first name of the person you would love to know about : ");
                  name = scn.next();
                  System.out.println("Your friend " +name+ " is a great guy..!");
                  System.out.println();
                  System.out.println("Enter 1 to continue giving other" +
                          " names or Enter 2. to quit");
                  System.out.println();
          
                  int choice = scn.nextInt();
                  if( choice == 1 ) {
                      flag = true;
                  } else {
                      System.out.println("ThankU.. Bye");
                      flag = false;
                  }
          
              } while(flag);
          
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-03-18
            • 1970-01-01
            • 2021-07-30
            • 2023-02-14
            • 1970-01-01
            • 2019-12-15
            • 2016-11-25
            • 1970-01-01
            相关资源
            最近更新 更多