import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
public class salary
{
public static void main(String[] args)
{
int n,sum=0,average=0,count1=0,count2=0;
System.out.println("How many people salary you want to enter" );
Scanner in = new Scanner(System.in);
n = in.nextInt();
String[] salary=new String[n];
int[] sal=new int[n];
for (int i=0;i<n;i++)
{
System.out.println("please enter salary with $ sign as prefix" );
salary[i]=in.nextLine();
}
for (int j=0;j<n;j++)
{
salary[j] = salary[j].replaceAll("[$]+", "0");
System.out.println(salary[j]);
sal[j]= Integer.parseInt(salary[j]);
//salary[j] = Integer.parseInt(salary[j].replaceAll("[$]+", " "));
sum=sum+sal[j];
}
average=sum/n;
for(int q=0;q<n;q++)
{
if (sal[q]>average)
{count1=count1+1;
}
else if (sal[q]<average)
{
count2=count2+1;
}}
System.out.println("The average is "+average);
System.out.println("The salary greater than average "+count1);
System.out.println("The salary less than average "+count2);
}
}
获取错误以及for循环无法正常工作
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at salary.main(salary.java:34)
读取以美元符号“$”开头并且后跟非负数的工资金额列表 number,将有效的工资输入保存到数组中并按升序对工资进行排序 订单,计算工资投入的平均值,并计算小于的投入数 /大于平均值。
答案 0 :(得分:0)
将第16行更改为in.next:
for (int i = 0; i < n; i++) {
System.out.println("please enter salary with $ sign as prefix");
salary[i] = in.next();
}