我需要修改此代码以便从max到min进行计数,以便将方法stej的结果作为String类型输出到main方法。然后应在主方法中打印stej方法的返回。任何帮助将不胜感激。
public static void main(String[] args) {
int max = 0, min = 0;
Scanner vhod = new Scanner(System.in);
System.out.print("This program counts down from the inputed max and min.\n");
System.out.print("Max must be greater or same as min!\n");
System.out.print("Enter max: ");
max = vhod.nextInt();
System.out.print("Enter min: ");
min = vhod.nextInt();
if(max>=min) {
stej(max, min);
}
else
System.out.print("\nERROR: not valid max and/or min!");
}
static void stej(int max, int min) {
if(max==min) {
System.out.print(min);
}
else {
System.out.print(max + " ");
stej(max-1, min);
}
}
答案 0 :(得分:0)
我简化了main以避免输入,但代码如下所示:
public class Printing {
public static void main(String[] args) {
int max = 10, min = 5;
if (max >= min) {
String result = stej(max, min);
System.out.println("Result is :" + result);
} else
System.out.print("\nERROR: not valid max and/or min!");
}
static String stej(int max, int min) {
if (max == min) {
return String.valueOf(min);
} else {
return max + " " + stej(max - 1, min);
}
}
}