如何告诉这个Java方法在一定条件下再次执行自己?
请参阅此UVa OJ问题: 它只适用于一个测试用例。然而,测试用例的数量没有给定的输入,这意味着程序应该只通过读取问题变量的输入就知道什么时候继续,什么时候停止 我正在考虑使用如何告诉这个Java方法在一定条件下再次执行自己?,java,stdout,stdin,Java,Stdout,Stdin,请参阅此UVa OJ问题: 它只适用于一个测试用例。然而,测试用例的数量没有给定的输入,这意味着程序应该只通过读取问题变量的输入就知道什么时候继续,什么时候停止 我正在考虑使用scanner.hasNextLine()方法作为条件;如果这是真的,重新开始,但我不知道怎么做。有线索吗 public static void main(String[] args) { Scanner scanner = new Scanner (System.in); int N = scanner.
scanner.hasNextLine()
方法作为条件;如果这是真的,重新开始,但我不知道怎么做。有线索吗
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*/
}
publicstaticvoidmain(字符串[]args){
扫描仪=新的扫描仪(System.in);
int N=scanner.nextInt();
int B=scanner.nextInt();
int H=scanner.nextInt();
int W=scanner.nextInt();
int[]priceArray=新的int[H];
int[]availableBeds=新int[W];
int cheapestStay=9999999;
对于(int i=0;i您可以将main(…)
方法中的所有代码包装到while(scanner.hasNextLine())
loop只要hasNextLine()
的计算结果为true
,您就可以使用while循环来重复主方法的指令
public static void main(String[] args) {
while(scanner.hasNextLine()){
...
}
}
这是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());
}
publicstaticvoidmain(字符串[]args){
扫描仪=新的扫描仪(System.in);
做{
int N=scanner.nextInt();
int B=scanner.nextInt();
int H=scanner.nextInt();
int W=scanner.nextInt();
int[]priceArray=新的int[H];
int[]availableBeds=新int[W];
int cheapestStay=9999999;
对于(int i=0;我在一个单独的方法中rap您的业务逻辑代码,然后根据条件在main(…)
方法中调用该方法反复调用该方法。