在此赋值中,我必须将整数字符串输入" 1234"存储到整数数组中,程序必须反向输出数组,总和和平均值。它必须继续提示用户输入字符串,除非他们输入" end"。
一切正常它只是程序打印出-14,4,3,2,1,-14,我不知道如何解决这个问题我也不知道输入while循环结束程序的值是什么。任何帮助将不胜感激
import java.util.*;
public class Assign1
{
public static void main(String[] args)
{
int sum=0;
String input;
Scanner scan = new Scanner(System.in);
do{
System.out.println("Enter in a string of integers: ");
input = scan.nextLine();
//makes the size of the array according to the length of the user input
int[] intArr = new int[input.length()];
//loop to store integers in array
for(int i=0; i<input.length(); i++)
{
intArr[i]= input.charAt(i)-'0';
}
//loop to calculate sum of elements in array
for(int i : intArr)
{
sum=sum+i;
}
System.out.println("Sum of array elements is: " + sum);
//calcluates the average of elements in array
double average = sum/input.length();
System.out.println("Average value of array elements is: " + average);
printReversed(intArr);
}
while(input!=" ");
}
public static void printReversed(int[] values)
{
//loop that prints out elements in reverse order
for(int i= values.length-1; i>=0; i--)
{
System.out.println(values[i]);
}
}
}