将按降序输出三个整数的程序

Program that will output three integers in descending ordrer

该程序旨在提示用户输入三个整数,将整数存储在三个单独的变量中,并按降序(从高到低的值)输出三个整数。

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);
  }
  }       

当我 运行 这个程序时,它输出整数一百万次并崩溃。为什么?

您的解决方案确实设计过度了。只需做这样的事情:

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]);
}   

看起来你几乎已经做到了。我假设你是一个新学生。如果您只是将方法更改为 void(不需要 return 值),您只需调用该方法即可获得所需的答案。你有方法和你的 main 都通过 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

blm 的解决方案会起作用,但我认为了解您的解决方案可能会有用。你不断地一遍又一遍地调用你的同一个函数。要修复它,请执行以下操作

  1. 将 return 类型更改为 void
  2. 移除return语句递归
  3. int result = largeSmall(num1, num2, num3); System.out.println(result);替换为largeSmall(num1, num2 num3);
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);
}

整数=0;扫描仪 kbd = 新扫描仪 (System.in);数 = kbd.nextInt(); System.out.println(数量); 运行 这个程序只是输入文本而不是整数。该程序应该会崩溃并告诉您 nextInt 方法抛出了哪种异常。将此代码包装在 try/catch 块中,您可以在其中捕获抛出的异常。添加一个循环,以便用户在输入文本时必须再次输入数字。