I need to write a program to solve a linear equation system, it should read in any n*n matrix from the user then print out the result. The Gaus algorithm is no problem for me, but ....how should I ask the user to input the matrix? How should I form the input? Thank you very much!!
Offhand, the simplest/nicest solution that comes to mind is to let the user input the whole matrix at once, separating each element in a row with one type of delimiter (like a space) and separating rows with another delimiter (like a newline). The end of a matrix could be signaled with another character, like a '.'. Use a BufferedReader to read the input, and use readLine() to get one row of the matrix. Then use a StringTokenizer (or the String split() method) to split the line into elements. So a typical matrix might be: 12 34 56 789 0 1 -5 111 21 or using different delimiters maybe: 12,34,56|789,0,1|-5,111,21 Alternately, you could first ask for the value of n, and then ask a series of questions to the user. E.g. for n = 3: What is element [0, 0]? What is element [0, 1]? What is element [0, 2]? What is element [1, 0]? What is element [1, 1]? What is element [1, 2]? What is element [2, 0]? What is element [2, 1]? What is element [2, 2]? Or you could make a GUI component likt a JTable which the user enters the values in. These last two techniques can be easier for the user to understand. But the advantage of the first method is that the whole matrix could be read from a file or something, rather than typed at the keyboard each time you run the program. In a full, well-developed program you might well want to offer both options to users. The choice is yours. Once you decide how you want the user to enter the data, you can work on how to read and parse it.