我的伙伴和我正在尝试创建一个阵列程序,用于分隔阵列的名字和姓氏,并显示血压以及最大,最小,标准差和AVG血压。这是我们的计划:
package arrays;
import java.util.*;
import java.io.*;
public class Arrays{
static Scanner input = new Scanner(System.in);
static double[] BP = {103, 111, 90, 72, 143, 144, 77, 88, 103};
static double max = BP[0];
static double min = BP[0];
static double sd=0;
static double avg=0;
static double squareDiff=0;
static double variance=0;
static double calculation=0;
static int i;
static String item;
static String[] pname= {"Jimmy Jones", "John Tod", "Tim Smith", "Mary Smith", "Sally Fields", "Smita Parval", "Sukhen Dey", "Jimmy Chang", "JoAnn Todler"};
static String pfnameArray [] = new String[pname.length];
static String plnameArray [] = new String[pname.length];
static String pfname="";
static String plname= "";
public static void Separate_names() {
for (int i = 0; i < pname.length; i++) {
if(pname[i].substring(i, i+1).equals(' ')) {
pfnameArray[i] = pname[i].substring(0, i);
plnameArray[i] = pname[i].substring(i+1, pname.length+1);
pfname = pfnameArray[i];
plnameArray[i]=plname;
}
}
}
public static void Data_Entry() {
for (int i = 0; i < BP.length; i++) {
for (int i = 0; i< BP.length; i++) {
if (BP[i] < min) min = BP[i];
}
for(int a=0; a<BP.length; a++){
calculation +=BP[a];
}
avg=calculation/BP.length;
for(int v=0; v<BP.length; v++){
squareDiff+= Math.pow((BP[v]-avg),2);
variance=squareDiff/BP.length;
}
{sd =Math.sqrt(variance);
}
}
public static void Print_Data(String [] fName, String [] lName) {
for( int l=0; l<BP.length; l++){
System.out.println(plnameArray[i] + " " + pfnameArray[i] + ", Blood Pressure: " + BP[l]);
}
System.out.println();
System.out.println("Max is " + max);
System.out.println("Min is " + min);
System.out.println ("Average is " + avg);
System.out.println("Standard Deviation:" + sd);
}}
对于名字和姓氏的输出,它显示为null。如何将数组分离到不显示空值的位置?另外,SD的公式是否正确?我们将它与其他人进行了比较,得到了不同的数字。
答案 0 :(得分:1)
像这样使用split
:
pfnameArray[] = pname[i].split(" ");
pfnameArray[0]
将包含名字
pfnameArray[1
]将包含激光名称