此程序用于提示用户输入三个整数,将整数存储在三个单独的变量中,并按降序输出三个整数(从最高到最低值)。
import java.util.Scanner;
public class ProgramToo
{
public static void main(String [] args)
{
Scanner kbd = new Scanner(System.in);
System.out.println("Enter the first number:");
int num1 = kbd.nextInt();
System.out.println("Enter the second number:");
int num2 = kbd.nextInt();
System.out.println("Enter the three number:");
int num3 = kbd.nextInt();
int result = largeSmall(num1, num2, num3);
System.out.println(result);
}
public static int largeSmall(int one, int two, int three)
{
if(one > two && two > three)
{
System.out.println(one + " " + two + " " + three);
}
else if(two > one && one > three)
{
System.out.println(two + " " + one + " " + three);
}
else if(three > two && two > one)
{
System.out.println(three + " " + two + " " + one);
}
else
{
System.out.println(one + " " + three + " " + two);
}
return largeSmall(one, two, three);
}
}
当我运行这个程序时,它输出整数一百万次并崩溃。为什么呢?
答案 0 :(得分:1)
您的解决方案确实过度设计。做这样的事情:
public static void main(String [] args)
{
Scanner kbd = new Scanner(System.in);
System.out.println("Enter the first number:");
int num1 = kbd.nextInt();
System.out.println("Enter the second number:");
int num2 = kbd.nextInt();
System.out.println("Enter the three number:");
int num3 = kbd.nextInt();
Integer[] arr = new Integer[3]
arr[0] = num1;
arr[1] = num2;
arr[2] = num3;
Arrays.sort(arr, Collections.reverseOrder());
System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);
}
答案 1 :(得分:0)
看起来你差不多了。我假设你是一名新生。如果您只是将方法更改为无效(不需要返回值),您只需调用该方法即可获得所需的答案。你有方法和你的主要循环通过println。我只删除了几行并更改了方法签名以使其正常工作。
public class Application {
public void start() {
Scanner kbd = new Scanner(System.in);
System.out.println("Enter the first number:");
int num1 = kbd.nextInt();
System.out.println("Enter the second number:");
int num2 = kbd.nextInt();
System.out.println("Enter the three number:");
int num3 = kbd.nextInt();
largeSmall(num1, num2, num3);
}
public static void largeSmall(int one, int two, int three)
{
if(one > two && two > three)
{
System.out.println(one + " " + two + " " + three);
}
else if(two > one && one > three)
{
System.out.println(two + " " + one + " " + three);
}
else if(three > two && two > one)
{
System.out.println(three + " " + two + " " + one);
}
else
{
System.out.println(one + " " + three + " " + two);
}
}//end start method
}//end application class
答案 2 :(得分:0)
blm
的解决方案可行,但我认为了解你的问题可能会有所帮助。你不断地一遍又一遍地调用同样的功能。要解决此问题,请执行以下操作
void
int result = largeSmall(num1, num2, num3);
System.out.println(result);
替换为largeSmall(num1, num2 num3);
。答案 3 :(得分:0)
Scanner k = new Scanner(System.in);
System.out.println("Enter the first number");
int c = k.nextInt();
System.out.println("Enter the second number");
int c2 = k.nextInt();
System.out.println("Enter the third number");
int c3 = k.nextInt();
int max = 0, mid = 0, min = 0;
if (c > c2 && c > c3) {
max = c;
mid = (c2 > c3) ? c2 : c3;
min = (c2 > c3) ? c3 : c2;
System.out.println("In ascending :"+min+","+mid+","+max);
System.out.println("In desascending :"+max+","+mid+","+min);
} else if (c2 > c && c2 > c3) {
max = c2;
mid = (c > c3) ? c : c3;
min = (c > c3) ? c3 : c;
System.out.println("In ascending :"+min+","+mid+","+max);
System.out.println("In desascending :"+max+","+mid+","+min);
} else if (c3 > c && c3 > c2) {
max = c3;
mid = (c > c2) ? c : c2;
min = (c > c2) ? c2 : c;
System.out.println("In ascending :"+min+","+mid+","+max);
System.out.println("In desascending :"+max+","+mid+","+min);
}
答案 4 :(得分:0)
运行此程序,但输入文本而不是整数。程序应该崩溃,并告诉您nextInt方法引发了哪种异常。将此代码包装在try / catch块中,在其中捕获引发的异常。添加循环,以便用户在输入文本时必须再次输入数字。
int num=0;
Scanner kbd = new Scanner(System.in);
num = kbd.nextInt();
System.out.println(num);