gpt4 book ai didi

java - 如何告诉这个Java方法在某种条件下再次执行自己?

转载 作者:行者123 更新时间:2023-11-30 08:09:13 25 4
gpt4 key购买 nike

请引用这个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*/
}

最佳答案

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

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

关于java - 如何告诉这个Java方法在某种条件下再次执行自己?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30656597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com