我当前的家庭作业有一种方法,提示用户输入矩阵值并返回数组。将字符串值输入到标题为" userInput"的数组中的扫描程序在索引0处由于某种原因而不提示。
以下是方法:
public static double[][] setArray()
{
//initiate variables
String stringValue = "";
double doubleValue = 0;
//instantiate return array
double[][] array = new double[3][4];
//instantiate string array for user input values
String[] userInput = new String[3];
//prompt user for array values by row
System.out.println("Enter matrix values by row: ");
userInput[0] = in.nextLine();//???
userInput[1] = in.nextLine();
userInput[2] = in.nextLine();
System.out.println();
for(int eachString = 0; eachString < 3; eachString++)
{
for(int index = 0; index < userInput[eachString].length(); index++)
{
//if string does not contain a space, period, or valid digit
//while index holds invalid character
while(userInput[eachString].charAt(index) < '0'
|| userInput[eachString].charAt(index) > '9')
{
if(userInput[eachString].charAt(index) == ' '
|| userInput[eachString].charAt(index) == '.')
{
break;
}
else
{
System.out.println("Invalid input. Digits must be in integer"
+ " or double form. (i.e. 4 5.0 2.3 9)");
System.out.println("Re-enter row " + eachString + ": ");
userInput[eachString] = in.nextLine();
}
}
}
//given string is valid at this point//
int valueCounter = 0;
//for each index in string value
for(int eachIndex = 0; eachIndex < userInput[eachString].length(); eachIndex++)
{
//if value != ' '... += string... if value == ' ' stop loop
if(userInput[eachString].charAt(eachIndex) != ' ')
{
stringValue += userInput[eachString].charAt(eachIndex);
}
//parse string to double value and add to array
//last digit will not parse unless condition also contains..
//userInput[eachString].length() - 1
if(userInput[eachString].charAt(eachIndex) == ' '
|| eachIndex == userInput[eachString].length() - 1)
{
doubleValue = Double.valueOf(stringValue);
array[eachString][valueCounter] = doubleValue;
valueCounter++;
//value counter is column index
stringValue = "";//clear string
}
}
}
return array;
}
这是程序的其余部分,包括另一种方法和主要方法。完整程序的目的是对多维数组中列的元素求和。解释为什么会发生这种情况(除非它是一个简单的解决方案)将受到高度赞赏,谢谢!
public static double getSum(int col, double[][] num)
{
//initiate return value
double sum = 0;
//sum column of multidimensional array
for(int row = 0; row < num.length; row++)
{
//row increments, user selected col is a fixed value
sum += num[row][col];
}
return sum;
}
public static void main(String[] args)
{
//TEST//
// double[][] array =
// {
// {3,4,5},
// {3,4,5},
// {3,4,5},
// };
//
// //sum col 0 == 9, col 1 == 12, col 2 == 15
// double sum = getSum(0, array);
// System.out.println(sum);
double[][] array = new double[3][4];
double[] columnSum = new double[4];
int userSelection = -1;
System.out.println("Welcome to the sum column program.");
// TEST
// for(int index = 0; index < array.length; index++)
// {
// for(int col = 0; col < 4; col++)
// {
// System.out.println("index " + index + " at " + col + " is " + array[index][col]);
// }
//
// }
// column[0] = getSum(0, array);
// column[1] = getSum(1, array);
// column[2] = getSum(2, array);
//
// System.out.println(column[0]);
//---
do
{
System.out.println("0) Exit program");
System.out.println("1) Run program");
System.out.print("Enter choice: ");
while(!in.hasNextInt())
{
System.out.print("Invalid choice. Re-enter: ");
in.next();
}
userSelection = in.nextInt();
switch(userSelection)
{
case 0: System.exit(0); break;
case 1:
{
array = setArray();//prompt user for array
columnSum[0] = getSum(0, array);
columnSum[1] = getSum(1, array);
columnSum[2] = getSum(2, array);
columnSum[3] = getSum(3, array);
System.out.println("Sum in column 1 is " + columnSum[0]);
System.out.println("Sum in column 2 is " + columnSum[1]);
System.out.println("Sum in column 3 is " + columnSum[2]);
System.out.println("Sum in column 4 is " + columnSum[3]);
}break;
}
}while(userSelection != 0);