public class Test5 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入需要相加的数(输入x结束):"); int sum = 0; int i = 0; while (scanner.hasNextDouble()){ double i1 = scanner.nextDouble(); sum +=i1; i++; } System.out.println("您输入了"+i+"个数,相加的数值为:"+sum); } }