问题是编写一个对三个整数进行排序的程序。 整数从输入对话框输入并分别存储在变量num1,num2和num3中。 程序对数字进行排序,使num1 <= num2 <= num3。
实际上我这样做,但结果仅适用于1,2和3数字!
当我输入任何不同的数字时,它不会向我显示我想要的结果!
这是我的代码..
import javax.swing.JOptionPane; public class number order { public static void main(String[] args) { int num1; int num2; int num3; String n = JOptionPane.showInputDialog(null, "input NUM 1 " ); num1 = Integer.parseInt(n); String u = JOptionPane.showInputDialog(null, "input NUM 2 " ); num2 = Integer.parseInt(u); String m = JOptionPane.showInputDialog(null, "input NUM 3 " ); num3 = Integer.parseInt(m); if (num1<=num2&& num2<=num3) System.out.println( num1+"<="+ num2+"<="+num3 ); if(num2<=num1&&num1<=num3) System.out.println(num2+"<="+num1+"<="+num3); if (num3<=num1&&num1<=num2) System.out.println(num3+"<="+num1+"<="+num2); // TODO code application logic here } }The question is to Write a program that sorts three integers. The integers are entered from the input dialogs and stored in variables num1, num2, and num3, respectively. The program sorts the numbers so that num1 <= num2 <= num3.
actually I do that but the result is available only to 1 ,2 and 3 numbers !
When I enter any different number it doesn't show me the result I want it !
here is my code..
import javax.swing.JOptionPane; public class number order { public static void main(String[] args) { int num1; int num2; int num3; String n = JOptionPane.showInputDialog(null, "input NUM 1 " ); num1 = Integer.parseInt(n); String u = JOptionPane.showInputDialog(null, "input NUM 2 " ); num2 = Integer.parseInt(u); String m = JOptionPane.showInputDialog(null, "input NUM 3 " ); num3 = Integer.parseInt(m); if (num1<=num2&& num2<=num3) System.out.println( num1+"<="+ num2+"<="+num3 ); if(num2<=num1&&num1<=num3) System.out.println(num2+"<="+num1+"<="+num3); if (num3<=num1&&num1<=num2) System.out.println(num3+"<="+num1+"<="+num2); // TODO code application logic here } }最满意答案
问题是你只检查这三个数字的六种可能安排中的三种。 另请注意,即使对于这三个,您实际上并没有对数字进行排序 ,而只是按排序顺序打印它们,即,您永远不会重新分配变量num1 , num2和num3 。
作为检查三个数字的所有可能排列或实现完整排序算法的替代方法,您还可以比较和交换数字对 。 通过这种方式,您可以获得更少的比较,同时仍然可以对三个数字的所有排列进行排序。
如果num1> num2,则交换num1和num2 如果num2> num3,则交换num2和num3 如果num1> num2,则再次交换num1和num2在这三次交换之后,数字按排序顺序排列。
当然,如果你有三个以上的数字这是不切实际的,你应该实现一个完整的排序算法(用于练习)或者使用其中一个内置函数,比如Arrays.sort (用于现实生活)。
The problem is that you check only three out of six possible arrangements of those three numbers. Also note that, even for those three, you are not actually sorting the numbers, but only printing them in sorted order, i.e., you are never reassigning the variables num1, num2, and num3.
Just as an alternative to checking all the possible arrangements of the three numbers or implementing a full sorting algorithm, you can also compare and swap pairs of numbers. This way, you get away with far fewer comparisons while still being able to sort all permutations of three numbers.
if num1 > num2, swap num1 and num2 if num2 > num3, swap num2 and num3 if num1 > num2, swap num1 and num2 againAfter those three swaps, the numbers are in sorted order.
Of course, if you have more than three numbers this gets impractical, and you should rather implement a full sorting algorithm (for exercise) or go with one of the builtins, like Arrays.sort (for real life).
更多推荐
发布评论