如何用Java输入不同的数字(How to input different numbers with Java)

编程入门 行业动态 更新时间:2024-10-26 19:29:51
如何用Java输入不同的数字(How to input different numbers with Java)

问题是编写一个对三个整数进行排序的程序。 整数从输入对话框输入并分别存储在变量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 again

After 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).

更多推荐

本文发布于:2023-07-05 01:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031233.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何用   数字   numbers   Java   input

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!