使用switch-JAVA(作业)调用函数

时间:2015-01-17 10:25:55

标签: java function matrix switch-statement

我正试图解决这个问题:

编写包含以下内容的程序:
(i)读取NΧM矩阵的函数。
(ii)在屏幕上显示NXM矩阵的功能。
(iii)添加这两个矩阵的功能。
(iv)减去这两个矩阵的函数。

程序还必须包含一个main()函数,它首先声明两个3x3矩阵 A和B,允许输入A和B的数据(使用(i)中的函数),并且应该 连续显示如下菜单:
1。添加矩阵
2。减去矩阵
3。出口
请输入您的选择(1-3):  
选择1或2后,程序应显示相应的 结果。什么时候  选择2,程序应该退出。

我试过这样做但是我遇到了switch语句的问题。函数调用带有下划线,我无法理解为什么。

有一条错误消息:方法add(int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int,int)in Number12类型不适用于arguments()

以下是代码:

import java.util.Scanner;
public class Number12 {

public static void read()
{
    int a1,a2,a3,a4,a5,a6,a7,a8,a9;
    int b1,b2,b3,b4,b5,b6,b7,b8,b9;

    Scanner input = new Scanner(System.in);

    System.out.println("You will now enter the values for matrix A.");
    System.out.print("Enter the values for a1 a2 and a3: ");
    a1 = input.nextInt();
    a2 = input.nextInt();
    a3 = input.nextInt();
    System.out.print("Enter the values for a4 a5 and a6: ");
    a4 = input.nextInt();
    a5 = input.nextInt();
    a6 = input.nextInt();
    System.out.print("Enter the values for a7 a8 and a9: ");
    a7 = input.nextInt();
    a8 = input.nextInt();
    a9 = input.nextInt();

    System.out.println("You will now enter the values for matrix B.");
    System.out.print("Enter the values for b1 b2 and b3: ");
    b1 = input.nextInt();
    b2 = input.nextInt();
    b3 = input.nextInt();
    System.out.print("Enter the values for b4 b5 and b6: ");
    b4 = input.nextInt();
    b5 = input.nextInt();
    b6 = input.nextInt();
    System.out.print("Enter the values for b7 b8 and b9: ");
    b7 = input.nextInt();
    b8 = input.nextInt();
    b9 = input.nextInt();

    add(a1,a2,a3,a4,a5,a6,a7,a8,a9,b1,b2,b3,b4,b5,b6,b7,b8,b9);
    subtract(a1,a2,a3,a4,a5,a6,a7,a8,a9,b1,b2,b3,b4,b5,b6,b7,b8,b9);
}

public static void add(int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9,int b1, int b2,int b3,int b4,int b5,int b6,int b7,int b8,int b9)
{
    int x1,x2,x3,x4,x5,x6,x7,x8,x9;
    x1 = a1 + b2;
    x2 = a2 + b2;
    x3 = a3 + b3;
    x4 = a4 + b4;
    x5 = a5 + b5;
    x6 = a6 + b6;
    x7 = a7 + b7;
    x8 = a8 + b8;
    x9 = a9 + b9;

    System.out.println("The result is: ");
    System.out.println(x1+" "+x2+" "+x3);
    System.out.println(x4+" "+x5+" "+x6);
    System.out.print(x7+" "+x8+" "+x9);
}

public static void subtract(int a1,int a2,int a3,int a4,int a5,int a6,int a7,int a8,int a9,int b1, int b2,int b3,int b4,int b5,int b6,int b7,int b8,int b9)
{
    int x1,x2,x3,x4,x5,x6,x7,x8,x9;
    x1 = a1 - b2;
    x2 = a2 - b2;
    x3 = a3 - b3;
    x4 = a4 - b4;
    x5 = a5 - b5;
    x6 = a6 - b6;
    x7 = a7 - b7;
    x8 = a8 - b8;
    x9 = a9 - b9;

    System.out.println("The result is: ");
    System.out.println(x1+" "+x2+" "+x3);
    System.out.println(x4+" "+x5+" "+x6);
    System.out.print(x7+" "+x8+" "+x9);
}


public static void main(String[] args) {

    int choice;

    Scanner input = new Scanner(System.in);
    System.out.println("You are asked to enter values for two 3x3 matrix. Below is the format: ");
    System.out.println("a1 a2 a3");
    System.out.println("a4 a5 a6");
    System.out.println("a7 a8 a9");
    System.out.println("and");
    System.out.println("b1 b2 b3");
    System.out.println("b4 b5 b6");
    System.out.println("b7 b8 b9");

    read();

    System.out.println("");
    System.out.println("****************************************************");
    System.out.println("****\t\t1. Add the matrices\t\t****");
    System.out.println("****\t\t2. Subtract the matrices\t****");
    System.out.println("****\t\t3. Exit\t\t\t\t****");
    System.out.println("****************************************************");
    System.out.print("Please enter 1-3 for your choice.");
    choice = input.nextInt();

    switch(choice)
    {
    case 1:
    {
        add();
        break;
    }
    case 2:
    {
        subtract();
        break;
    }
    case 3:
    {
        break;
    }
    default:
    {
        System.out.print("Please enter a number from 1-3.");
    }
    }

}
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

通过移动读入类成员的int来保持类实例中的整数读取。将静态方法add()read()和substract()更改为实例方法(删除静态单词)。

import java.util.Scanner;
public class Number12 {

    int a1,a2,a3,a4,a5,a6,a7,a8,a9;
    int b1,b2,b3,b4,b5,b6,b7,b8,b9;

public void read()
{

从read中删除add(...)和substract()调用。

然后用非静态add()

替换静态add(a1 ... b9)

在你的主要:

添加行:

Number12 number = new Number12();

使用 number.read()代替读取()

使用 number.add()代替添加()

使用 number.substract()代替 substract()