所以我有这个项目要求我生成一个用户指定长度的单维数组,然后让用户填充数组,我无法弄清楚我应该做什么在
import java.util.*;
public class singledimensionalarray {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner (System.in);
System.out.println("Please input array length: ");
String[] SDArray= new String[keyboard.nextInt()];
System.out.println("Please input " + SDArray.length + " double numbers:");
int n = 0;
for (n = 0; n <= SDArray.length; n++){
SDArray.add (keyboard.nextDouble()); //keep getting an error here
}
}
}
答案 0 :(得分:0)
尝试
import java.util.*;
public class singledimensionalarray {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner (System.in);
System.out.println("Please input array length: ");
double[] SDArray= new double[keyboard.nextInt()];
System.out.println("Please input " + SDArray.length + " double numbers:");
for (int n = 0; n < SDArray.length; n++){
SDArray[n] = (keyboard.nextDouble());
}
keyboard.close();
}
}
答案 1 :(得分:0)
首先请不要使用n编写for循环。用字母i。其次,您尝试在String数组中存储double值。用户究竟想用数组填充数组?
编辑:这是我写过的唯一答案。我是大学二年级学生,并不是特别精通java。请为耶稣的爱打开我的帐户。我花了一个小时写了一个经过深思熟虑的答案,只是为了让它被封锁,因为这个单一的答案。答案 2 :(得分:0)
好吧,所以我最终做的就是Patrick和Khachik所说的;这就是我所拥有的
import java.util。*;
public class singledimensionalarray {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner (System.in);
System.out.println("Please input array length: ");
double[] SDArray= new double[keyboard.nextInt()];
System.out.println("Please input " + SDArray.length + " double numbers:");
for (int n = 0; n < SDArray.length; n++){
SDArray[n] = (keyboard.nextDouble());
}
keyboard.close();
System.out.print("After reversal:");
for (int i = SDArray.length - 1; i >= 0; i--) {
System.out.print(SDArray[i]+",");
}
} }
感谢您的帮助!