【问题标题】:How to tell this Java method to execute itself again under a certain condition?如何告诉这个Java方法在某种条件下再次执行自己?
【发布时间】:2015-06-05 00:50:20
【问题描述】:

请参考这个UVa OJ问题:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=show_problem&problem=2595

它适用于单个测试用例。但是,对于测试用例的数量没有给定的输入,这意味着程序应该只通过读取问题变量的输入来知道何时继续以及何时停止

我正在考虑使用scanner.hasNextLine()方法作为条件;如果是真的,请重新开始 main ,但我不知道该怎么做。有什么线索吗?

public static void main(String[] args) {
    Scanner scanner = new Scanner (System.in);
    int N = scanner.nextInt();
    int B = scanner.nextInt();
    int H = scanner.nextInt();
    int W = scanner.nextInt();
    int [] priceArray = new int [H];
    int [] availableBeds = new int [W];
    int cheapestStay = 999999999;
    for (int i=0; i<H; i++){
        priceArray[i] = scanner.nextInt();

        for (int j=0; j<W; j++){
            availableBeds[j] = scanner.nextInt();
            if (availableBeds[j] >= N && priceArray[i]*N <= B && priceArray[i]*N < cheapestStay){
                cheapestStay = priceArray[i]*N;
            }

        }

    }

    if (cheapestStay != 999999999){
        System.out.println(cheapestStay);
    }else{
        System.out.println("stay home");
    }
    /*if (!scanner.hasNextLine)
        repeat*/
}

【问题讨论】:

  • 将您的业务逻辑代码包装在一个单独的方法中,然后根据条件一次又一次地调用该方法在main(..) 方法中调用该方法。

标签: java stdout stdin


【解决方案1】:

只要 hasNextLine() 的计算结果为 true,您就可以使用 while 循环来重复 main 方法的指令。

public static void main(String[] args) {
    while(scanner.hasNextLine()){
        ...
    }
}

【讨论】:

    【解决方案2】:

    您可以将 main(...) 方法中的所有代码包装到 while(scanner.hasNextLine()) 循环中

    【讨论】:

      【解决方案3】:

      这是 do-while 的最佳用例

      public static void main(String[] args) {
          Scanner scanner = new Scanner (System.in);
          do {
      
              int N = scanner.nextInt();
              int B = scanner.nextInt();
              int H = scanner.nextInt();
              int W = scanner.nextInt();
              int [] priceArray = new int [H];
              int [] availableBeds = new int [W];
              int cheapestStay = 999999999;
              for (int i=0; i<H; i++){
                  priceArray[i] = scanner.nextInt();
      
                  for (int j=0; j<W; j++){
                      availableBeds[j] = scanner.nextInt();
                      if (availableBeds[j] >= N && priceArray[i]*N <= B && priceArray[i]*N < cheapestStay){
                          cheapestStay = priceArray[i]*N;
                      }
      
                  }
      
              }
      
              if (cheapestStay != 999999999){
                  System.out.println(cheapestStay);
              }else{
                  System.out.println("stay home");
              }
              /*if (!scanner.hasNextLine)
                  repeat*/
          } while (scanner.hasNextLine());
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-14
        • 2011-06-18
        • 2018-06-14
        • 2013-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多