Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JOptionPane降序数组程序问题_Java_Arrays_Eclipse_User Interface_Joptionpane - Fatal编程技术网

Java JOptionPane降序数组程序问题

Java JOptionPane降序数组程序问题,java,arrays,eclipse,user-interface,joptionpane,Java,Arrays,Eclipse,User Interface,Joptionpane,因此,在运行程序之前,我没有看到任何错误。基本上它是一个数组程序,用户在其中输入10个数字。一旦用户输入了数组的数量,它就会弹出一个GUI,上面显示键入一个10的数组。它只允许您执行一次,并且它只会重复用户键入的相同数字10次。这一切都被抬高了,我只在12小时轮班的情况下做了几个星期也没用。如果有人能给我指出正确的方向,那就太棒了 package Array; import javax.swing.JOptionPane; public class Array { public stat

因此,在运行程序之前,我没有看到任何错误。基本上它是一个数组程序,用户在其中输入10个数字。一旦用户输入了数组的数量,它就会弹出一个GUI,上面显示键入一个10的数组。它只允许您执行一次,并且它只会重复用户键入的相同数字10次。这一切都被抬高了,我只在12小时轮班的情况下做了几个星期也没用。如果有人能给我指出正确的方向,那就太棒了

package Array;
import javax.swing.JOptionPane;
public class Array {
    public static void main(String[] args) {
        String response;
        response = JOptionPane.showInputDialog("Enter the numbers : ");
        int n = Integer.parseInt(response);
        int[] a=new int[n];
        int i,j,temp=0;
        JOptionPane.showInputDialog("Enter "+n+" Array Elements : ");
        for(i=0;i<n;i++){
            a[i]=Integer.parseInt(response);
        }
        JOptionPane.showMessageDialog(null,"\nArray Elements Are : ");
        for(i=0;i<n;i++) {
            JOptionPane.showMessageDialog(null,"  "+a[i]);
        }
        for(i=0;i<n;i++) {
            for(j=i+1;j<n;j++) {
                if(a[i]<a[j]) {
                    temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;          
                }
            }
        }
        JOptionPane.showMessageDialog(null,"\nArray Elements in Descending Order : ");
        for(i=0;i<n;i++) {
            JOptionPane.showMessageDialog(null,"   "+a[i]);
        }   
    }
}
封装阵列;
导入javax.swing.JOptionPane;
公共类数组{
公共静态void main(字符串[]args){
字符串响应;
response=JOptionPane.showInputDialog(“输入数字:”);
int n=Integer.parseInt(响应);
int[]a=新的int[n];
int i,j,temp=0;
showInputDialog(“输入“+n+”数组元素:”);

对于(i=0;i而言,核心问题是,您没有要求用户为数组中的每个元素输入一个值,而只是解析最后一个
响应,即数组的元素数

我剥离了您的解决方案并删除了
JOptionPane
,因为这可能不是解决当前问题的最佳解决方案。但是,用
JOptionPane
替换
扫描仪并不需要太多时间,因为它们通常都在做相同的事情

Scanner input = new Scanner(System.in);
System.out.print("Number of elements: ");
String response = input.nextLine();
int numberOfElements = Integer.parseInt(response);
int[] values = new int[numberOfElements];

for (int index = 0; index < numberOfElements; index++) {
    System.out.print("Element " + index + ": ");
    response = input.nextLine();
    int value = Integer.parseInt(response);
    values[index] = value;
}
for (int i = 0; i < numberOfElements; i++) {
    for (int j = i + 1; j < numberOfElements; j++) {
        if (values[i] < values[j]) {
            int temp = values[i];
            values[i] = values[j];
            values[j] = temp;
        }
    }
}

System.out.println("Sorted:");
for (int index = 0; index < numberOfElements; index++) {
    System.out.println(values[index]);
}
扫描仪输入=新扫描仪(System.in);
系统输出打印(“元素数:”);
字符串响应=input.nextLine();
int numberOfElements=Integer.parseInt(响应);
int[]值=新的int[numberOfElements];
for(int index=0;index
是否可以使用JOptionPane创建此数组?我正在尝试在不使用扫描仪的情况下专门创建它。但到目前为止,您提供了极大的帮助*MadProgrammerTake
Scanner
out
JOptionPane
in-重点是,在第一个
for循环中,您必须提示用户输入下一个数字,即y我们的代码不起作用,
JOptionPane
只是混淆了问题(IMHO)-我建议您花点时间理解这个示例,并尝试自己修改它以使用
JOptionPane
-您将从练习中学习,然后简单地复制并粘贴其他解决方案“全部升级”是什么意思?它到底做错了什么,应该做什么?谢谢,欢迎来到Stack Overflow!谢谢。基本上它不会让用户为数组输入变量的数量。谢谢,我是Java的学习者