我正在制作一个程序,显示存储在多个阵列中的某些行星值,我需要扫描仪的输入来确定应该显示哪些数组值。
package eindopdrachtse;
import java.util.* ;
import java.util.ArrayList;
public class EindopdrachtSE {
public static void main (String[] args) {
// Arraylist van de namen
ArrayList<String> names = new ArrayList<>();
names.add("0 = Mercurius");
names.add("1 = Venus");
names.add("2 = Mars");
names.add("3 = Jupiter");
names.add("4 = Saturnus");
names.add("5 = Uranus");
names.add("6 = Neptunus");
for (String name : names)
{
System.out.println(name);
}
// Vul planeet nummer in
Scanner in = new Scanner (System.in);
System.out.print("Vul hier uw nummer in:");
int nummer = in.nextInt();
if (nummer==0){
System.out.println("De valversnelling is:"+ valversnelling[nummer] );
double[] valversnelling = new double[7];
valversnelling[0]= 2.78;
valversnelling[1]= 8.60;
valversnelling[2]= 3.72;
valversnelling[3]= 22.9;
valversnelling[4]= 9.05;
valversnelling[5]= 7.77;
valversnelling[6]= 11.0;
double[] massa = new double[7];
massa[0]= 0.0553;
massa[1]= 0.8149;
massa[2]= 0.1074;
massa[3]= 317.94;
massa[4]= 95.181;
massa[5]= 14.531;
massa[6]= 17.135;
int[] temperatuur = new int[7];
temperatuur[0]= 167;
temperatuur[1]= 464;
temperatuur[2]= -63;
temperatuur[3]= -121;
temperatuur[4]= -130;
temperatuur[5]= -205;
temperatuur[6]= -220;
}
}
// Informatie opvragen van planeet
//planeten:
// 0 = Mercurius
// 1 = Venus
// 2 = Mars
// 3 = Jupiter
// 4 = Saturnus
// 5 = Uranus
// 6 = Neptunus
}
截至目前我没有添加所有数组,我只是添加了“valversnelling”。我只需要理解这个原则。
感谢能帮助我的人!
答案 0 :(得分:0)
在初始化之前调用数组值。
这样做:
double[] valversnelling = new double[7];
valversnelling[0]= 2.78;
valversnelling[1]= 8.60;
valversnelling[2]= 3.72;
valversnelling[3]= 22.9;
valversnelling[4]= 9.05;
valversnelling[5]= 7.77;
valversnelling[6]= 11.0;
double[] massa = new double[7];
massa[0]= 0.0553;
massa[1]= 0.8149;
massa[2]= 0.1074;
massa[3]= 317.94;
massa[4]= 95.181;
massa[5]= 14.531;
massa[6]= 17.135;
int[] temperatuur = new int[7];
temperatuur[0]= 167;
temperatuur[1]= 464;
temperatuur[2]= -63;
temperatuur[3]= -121;
temperatuur[4]= -130;
temperatuur[5]= -205;
temperatuur[6]= -220;
if (nummer==0) {
System.out.println("De valversnelling is:"+ valversnelling[nummer]
}
答案 1 :(得分:0)
我需要显示正确的值,如果数字= 0,它应该显示valversnelling [0] = 2.78;但是怎么做呢
您的代码已经在执行此操作,只需删除if语句即可。数组索引将打印相应的数据并在初始化数组后打印它:
//if (nummer==0){ //remove the if-statement
System.out.println("De valversnelling is:"+ valversnelling[nummer] );
完整代码:
您可以初始化数组元素,而无需逐个分配它们:
double[] valversnelling = {2.78, 8.60, 3.72, 22.9, 9.05, 7.77, 11.0};
double[] massa = {0.0553, 0.8149, 0.1074, 317.94, 95.181, 14.531, 17.135};
int[] temperatuur = {167, 464, -63, -121, -130, -205, -220};
Scanner in = new Scanner (System.in);
System.out.print("Vul hier uw nummer in:");
int nummer = in.nextInt();
System.out.println("De valversnelling is:"+ valversnelling[nummer] );