简体   繁体   English

Java Switch菜单错误以及如何从另一个类调用方法

[英]Java Switch menu error and how to call a method from another class

Hi Im currently learning Java and Im doing an assignment where I need to *create a Menu that calls several methods. 您好,我目前正在学习Java,我正在做一项作业,我需要*创建一个调用多种方法的菜单。 I have 3 classes (Contacto,Agenda and Principal). 我有3节课(Contacto,议程和校长)。 My assignment is trying to evaluate Constructors and Arrays and some other basic theory. 我的任务是尝试评估构造函数和数组以及其他一些基本理论。

My menu error is: Principal.java:34: error: cannot find symbol while(opcion!=4).* 

I already check and my variable "opcion" is declared. 我已经检查并声明了变量“ opcion”。

public class Principal{

private static void imprimeMenu(){
    Scanner input = new Scanner(System.in);

    String mainMenu = ("Choose an option from the menu: \n"
                        + "1. Add contact\n"
                        + "2. Find contact\n"
                        + "3. Search contact\n"
                        + "4. Exit");

    do{
    System.out.println(mainMenu);
    int opcion = input.nextInt();

    switch(opcion){
        case 1:
            break;
        case 2:
            System.out.println("Search");
            break;
        case 3:
            System.out.println("Erase");
            break;
        default:
            System.out.println("Command not recognize");
            break;
        }
    }
    while(opcion!=4);
}

public static void main(String[] args){
    imprimeMenu();      
}
}

And inside the my cases I need to call 3 methods (Add, Search and Erase contacts) that are inside a class called Agenda. 在我的案例中,我需要调用名为Agenda的类中的3种方法(添加,搜索和擦除联系人)。 The 3 methods are void and receive a parameter. 这三种方法均无效,并接收一个参数。 I tried but I get an error where it says I need some parameters: 我尝试过,但是出现错误,提示我需要一些参数:

case 1:
    Agenda.addContacto(); and also tried Agenda.addContacto(contacto);

My Agenda class looks like this 我的议程课看起来像这样

public class Agenda{
private Contacto [] contactos;
private int numContactos;


public Agenda(){
    this.contactos = new Contacto[10];
    this.numContactos = 0;
}

public Agenda(int x){
    this.contactos = new Contacto[x];
    this.numContactos = 0;
}

public void addContact(Contacto contact){
    if(numContactos<contactos.length){
        this.contactos [numContactos] = contact; 
        numContactos+=1;
    } 
          }

Your problem is that opcion is defined inside the loop, so it's scope ends before the closing while . 您的问题是opcion在循环内定义,因此它的作用域 while结束之前结束。

Move the definition outside the loop to fix the problem: 将定义移到循环外以解决问题:

int opcion = 0;
do{
    System.out.println(mainMenu);
    opcion = input.nextInt();

    switch(opcion){
        case 1:
            break;
        case 2:
            System.out.println("Search");
            break;
        case 3:
            System.out.println("Erase");
            break;
        default:
            System.out.println("Command not recognize");
            break;
        }
} while(opcion!=4);

The correct call of Agenda.addContacto method is indeed Agenda.addContacto(contacto) . Agenda.addContacto方法的正确调用确实是Agenda.addContacto(contacto) You need to make sure that contacto is set to an instance of Contacto object before making the call. 在进行调用之前,您需要确保将contacto设置为Contacto对象的实例。

You have 2 issues with these programme 这些程序有2个问题

1.You need to declare option variable before the while loop. 1.您需要在while循环之前声明option变量。

2.If you want to call Agenda.anyMethod() you need to create an instance/object of the class otherwise you can declare the class Agenda as static.then you can directly call the method as Agenda.addContacto(); 2.如果要调用Agenda.anyMethod(),则需要创建该类的实例/对象,否则可以将Agenda类声明为静态。然后,可以直接将该方法称为Agenda.addContacto();。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM