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

lxkprmvk  于 2023-03-06  发布在  Java
关注(0)|答案(6)|浏览(127)

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

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

当我运行这个程序的时候,它输出了一百万次整数,然后崩溃了,为什么?

iklwldmw

iklwldmw1#

你的解决方案设计过度了。就像这样做吧:

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

goucqfw62#

看起来你几乎做到了。我假设你是一个新学生。如果你只是将方法更改为void(不需要返回值),你将从调用方法中得到你需要的答案。你让方法和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
xbp102n0

xbp102n03#

blm的解决方案会起作用,但我认为知道你的解决方案出了什么问题可能会有用。你不断地一遍又一遍地调用同一个函数。要解决这个问题,请执行以下操作
1.将返回类型更改为void
1.删除return语句递归
1.将int result = largeSmall(num1, num2, num3); System.out.println(result);替换为largeSmall(num1, num2 num3);

yrefmtwq

yrefmtwq4#

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

nmpmafwu5#

int num = 0;扫描仪kbd =新扫描仪(System.in);数值=千字节.下一个整数();系统输出打印输入(编号);运行此程序,但输入文本而不是整数。程序应崩溃并告诉您nextInt方法抛出了哪种异常。将此代码 Package 在try/catch块中,在其中捕获抛出的异常。添加一个循环,以便用户在输入文本时必须再次输入数字。

wr98u20j

wr98u20j6#

import java.util.Scanner;

public class AscendingAndDescending//defining a class AscendingAndDescending  

{

   public static void main(String[] args) //main method

   {

       int n1,n2,n3,min,max,m;

       Scanner in = new Scanner(System.in);//creating Scanner class object

       System.out.print("Enter an integer: ");//print message

       n1 = in.nextInt();//input value

       System.out.print("And another: ");//print message

       n2 = in.nextInt();//input value

       System.out.print("And just one more: ");//print message

       n3 = in.nextInt();//input value

       min = n1; //use min variable that holds value

       max = n1; //use mix variable that holds value

       if (n2 > max) max = n2;//use if to compare value and hols value in max variable

       if (n3 > max) max = n3;//use if to compare value and hols value in max variable

       if (n2 < min) min = n2;//use if to compare value and hols value in min variable

       if (n3 < min) min = n3;//use if to compare value and hols value in min variable

       m = (n1 + n2 + n3) - (min + max);//defining m variable that arrange value

       System.out.println("Ascending: " + min + " " + m + " " + max);//print Ascending order value

       System.out.println("Descending: " + max + " " + m + " " + min);//print Descending order value

   }

}

相关问题