处理一维数组 java
processing single dimensional array java
我正在尝试使用对话框中的用户输入创建一个数组。它应该询问用户要输入多少个数字,然后要求用户输入数字。然后代码应该以相反的顺序输出数字。下面是我到目前为止的代码。它不起作用。我在尝试使用用户输入初始化数组时做错了。我是 java 的新手,所以欢迎任何建议。提前致谢。
public static void main(String[] args)
{
String input;
int space;
double [] numbers;
double count;
String numberInput;
double number;
input = JOptionPane.showInputDialog
(null, "How many numbers would you like to enter?");
space = Integer.parseInt(input);
numbers = new double[space];
count = 0;
while (count < space)
{
numberInput = JOptionPane.showInputDialog
(null, "Enter a number to be sorted: ");
number = Double.parseDouble(numberInput);
for (int i = 0; i < numbers.length; i++)
numbers[i] = number;
count++;
}
double[] numbers2 = swapArray(numbers);
JOptionPane.showMessageDialog(null, numbers2);
}
public static double[] swapArray(double[] array)
{
double[] result = new double[array.length];
for (int i = 0, j = result.length - 1;
i < array.length; i++, j--)
{
result[j] = array[i];
}
return result;
}
}
这是我对您的作业的看法。它应该给你一些关于如何处理问题的想法,而不是给你一个复制粘贴的解决方案,也不是最好的(以我自己的能力)structure/logic(但仍然给你提示,让你指出他们的方向):
package so_q33405148;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
public class main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
//TIP: You can use 'Scanner' instead of a 'Reader' here, to avoid having to parse strings into ints (scanner can do it for you)
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many numbers would you like to enter?");
int count = Integer.parseInt(br.readLine());
int input[] = new int[count];
for (int i = 0; i < count; i++) {
System.out.print("Enter a number to be sorted: ");
//TIP: With some clever math, you can invert the array right as it's still filling up here, by manipulating a new int (so 'i' is unchanged, as it's the for-loop's index) using 'i' and 'input.length'...Or, with a little creativity and insight, you may even achieve the same result manipulating 'i' directly somewhere else...
input[i] = Integer.parseInt(br.readLine());
}
System.out.println("Original array:\n" + Arrays.toString(input));
//TIP: Better methods to reverse arrays and/or collections exist.
//Take a look at SO question #3962766 (puritan solution without as much memory-footprint) and also Google about 'Arrays.asList()' and 'Collections.reverse()' (learn about collections-sorting)
int reversedInput[] = new int[input.length];
for (int i = 0; i < count; i++) {
reversedInput[i] = input[count - i - 1];
}
System.out.println("Reversed array:\n" + Arrays.toString(reversedInput));
} catch (IOException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我正在尝试使用对话框中的用户输入创建一个数组。它应该询问用户要输入多少个数字,然后要求用户输入数字。然后代码应该以相反的顺序输出数字。下面是我到目前为止的代码。它不起作用。我在尝试使用用户输入初始化数组时做错了。我是 java 的新手,所以欢迎任何建议。提前致谢。
public static void main(String[] args)
{
String input;
int space;
double [] numbers;
double count;
String numberInput;
double number;
input = JOptionPane.showInputDialog
(null, "How many numbers would you like to enter?");
space = Integer.parseInt(input);
numbers = new double[space];
count = 0;
while (count < space)
{
numberInput = JOptionPane.showInputDialog
(null, "Enter a number to be sorted: ");
number = Double.parseDouble(numberInput);
for (int i = 0; i < numbers.length; i++)
numbers[i] = number;
count++;
}
double[] numbers2 = swapArray(numbers);
JOptionPane.showMessageDialog(null, numbers2);
}
public static double[] swapArray(double[] array)
{
double[] result = new double[array.length];
for (int i = 0, j = result.length - 1;
i < array.length; i++, j--)
{
result[j] = array[i];
}
return result;
}
}
这是我对您的作业的看法。它应该给你一些关于如何处理问题的想法,而不是给你一个复制粘贴的解决方案,也不是最好的(以我自己的能力)structure/logic(但仍然给你提示,让你指出他们的方向):
package so_q33405148;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
public class main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
//TIP: You can use 'Scanner' instead of a 'Reader' here, to avoid having to parse strings into ints (scanner can do it for you)
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many numbers would you like to enter?");
int count = Integer.parseInt(br.readLine());
int input[] = new int[count];
for (int i = 0; i < count; i++) {
System.out.print("Enter a number to be sorted: ");
//TIP: With some clever math, you can invert the array right as it's still filling up here, by manipulating a new int (so 'i' is unchanged, as it's the for-loop's index) using 'i' and 'input.length'...Or, with a little creativity and insight, you may even achieve the same result manipulating 'i' directly somewhere else...
input[i] = Integer.parseInt(br.readLine());
}
System.out.println("Original array:\n" + Arrays.toString(input));
//TIP: Better methods to reverse arrays and/or collections exist.
//Take a look at SO question #3962766 (puritan solution without as much memory-footprint) and also Google about 'Arrays.asList()' and 'Collections.reverse()' (learn about collections-sorting)
int reversedInput[] = new int[input.length];
for (int i = 0; i < count; i++) {
reversedInput[i] = input[count - i - 1];
}
System.out.println("Reversed array:\n" + Arrays.toString(reversedInput));
} catch (IOException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}