让程序在大多数情况下工作(感谢帮助:))。我只需要添加while循环,这样用户就可以多次获取菜单选项。它开始在继续切换为false的行中给出错误“; expected”。这是代码。
import java.io.*;
import java.util.*;
class Matrix {
double[][] element;
int rows, cols;
Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
element = new double[rows][cols];
}
public double getValue(int row, int col) {
return element[row][col];
}
public void setValue(int row, int col, double value) {
element[row][col] = value;
}
public int getNoRows() { // returns the total number of rows
return rows;
}
public int getNoCols() { // returns the total number of cols
return cols;
}
// The methods for the main calculations
public Matrix AddMatrix(Matrix m2) {
int row1 = getNoRows();
int col1 = getNoCols();
Matrix result = new Matrix(row1, col1);
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
result.setValue(i, j, this.getValue(i, j) + m2.getValue(i, j));
}
}
return result;
}
public Matrix MultiplyMatrix(Matrix m2) {
if (this.getNoCols() != m2.getNoRows()) {
throw new IllegalArgumentException("matrices can't be multiplied");
}
int row2 = this.getNoRows();
int col2 = m2.getNoCols();
Matrix result = new Matrix(row2, col2);
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {
result.setValue(i, j, result.getValue(i, j) + this.getValue(i, j) * m2.getValue(i, j));
}
}
return result;
}
public Matrix TransposeMatrix() {
int row3 = this.getNoCols();
int col3 = this.getNoRows();
Matrix result = new Matrix(row3, col3);
for (int i = 0; i < row3; i++) {
for (int j = 0; j < col3; j++) {
result.setValue(i, j, this.getValue(j, i));
}
}
return result;
}
public void DisplayMatrix() {
for (int i = 0; i < this.getNoRows(); i++) {
for (int j = 0; j < this.getNoCols();
j++) {
System.out.print((this.getValue(i, j)) + " ");
}
System.out.print("\n");
}
}
}
public class Lab1 {
public static void main(String args[]) throws FileNotFoundException {
int choice;
Scanner in = new Scanner(System.in);
Boolean continue = true;
while (continue){
System.out.println("Enter your choice /n");
choice = in.nextInt();
System.out.println("1. Add two matrices \n");
System.out.println("2. Multiplymatrix two matrices \n");
System.out.println("3. Take transpose of a matrix \n");
System.out.println("4. Display a matrix \n");
System.out.println("5. Exit \n");
if (choice == 1) {
Matrix m1 = MatrixReader();
m1.DisplayMatrix();
Matrix m2 = MatrixReader();
m2.DisplayMatrix();
Matrix m3 = new Matrix(m1.getNoRows(), m1.getNoCols());
m3 = m1.AddMatrix(m2);
m3.DisplayMatrix();
}
if (choice == 2) {
Matrix m1 = MatrixReader();
m1.DisplayMatrix();
Matrix m2 = MatrixReader();
m2.DisplayMatrix();
Matrix m3 = new Matrix(m1.getNoRows(), m2.getNoCols());
m3 = m1.MultiplyMatrix(m2);
m3.DisplayMatrix();
}
if (choice == 3) {
Matrix m1 = MatrixReader();
m1.DisplayMatrix();
Matrix m3 = new Matrix(m1.getNoRows(), m1.getNoCols());
m3 = m1.TransposeMatrix();
m3.DisplayMatrix();
}
if (choice == 4) {
System.out.println("Will need to call the DisplyMatrix method for the object \n");
}
if (choice == 5) {
continue = false;
}
else {
System.out.println("Incorrect input. Kindly enter again \n");
}
}
}
public static Matrix MatrixReader() throws FileNotFoundException {
System.out.println("Give the filename for the matrix");
Scanner filescanner = new Scanner(System.in);
Scanner scanner = new Scanner(new File(filescanner.nextLine()));
scanner.nextLine(); // removes the first line in the input file
String rowLine = scanner.nextLine();
String[] arr = rowLine.split("=");
int rows = Integer.parseInt(arr[1].trim());
String colLine = scanner.nextLine();
String[] arr2 = colLine.split("=");
int cols = Integer.parseInt(arr2[1].trim());
Matrix test = new Matrix(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
test.setValue(i, j, scanner.nextDouble());
}
}
return test;
}
}
答案 0 :(得分:3)
除了
之外,一切看起来都不错Boolean continue = true;
和
continue = false;
等
continue
是reserved word,不能用作标识符。将变量重命名为cont
或类似的东西。
顺便说一句,我建议您在此应用程序中使用boolean
代替Boolean
。
另外,我相信你的if结构有错误:
if (choice == 1)
...
if (choice == 2)
...
if (choice == 5)
...
else
...
else
分支将在choice != 5
的所有情况下被采用(即使选择是1,2,3,......所以也许你想把它改成
if (choice == 1)
...
else if (choice == 2)
...
else if (choice == 3)
...
else
...
最后,您可能还想考虑使用switch
进行选择:
switch (choice) {
case 1:
...
break;
case 2:
...
break;
...
default:
...
}
答案 1 :(得分:1)
我不确定这是否会导致冲突,但“continue”也是Java中的一个声明。也许,通过更改继续的变量名称,您的问题将得到解决。附加的链接指的是如何将continue用作Java中的语句:http://download.oracle.com/javase/tutorial/java/nutsandbolts/branch.html您还可以做的是从if()构造中创建一个switch()语句,但这是个人品味:)
答案 2 :(得分:1)
为避免出现“已经定义了m1”错误,请像这样写开关(包括花括号):
switch (choice) {
case 1: {
...
break;
}
case 2: {
...
break;
}
...
default: {
...
}
}
答案 3 :(得分:0)
如果您已转换为'switch',则声明'Matrix m1 = null;'就在switch语句之前。在每个'case'的初始化中,只需使用'm1 = new MatrixReader()'。对'm2'变量做同样的事。