如何从另一个函数/方法调用变量?

时间:2018-09-08 14:36:24

标签: java

我分配了一份简历,以便用户输入并显示它。但是,我不知道如何调用另一个变量来实现打印/显示功能。

代码如下:

import java.util.Scanner;
public class curriculumVitae1{
    public static String firstName;
    public static String middleName, lastName, birthDate, maritalStatus, homeAddress, provincialAddress, mobileNumber, anotherMobile, landlineNumber, anotherLandline, primaryYears; 
    private static void main (String args[]){
        Scanner input = new Scanner(System.in); 
            System.out.print("\nCurriculum Vitae");
            System.out.print("\nInput your  last name: ");
            String lastName; 
            lastName = input.nextLine();
            System.out.print("\nInput your  first name: ");
            String firstName; 
            firstName = input.nextLine();

            System.out.print("\nInput your  middle name: ");
            String middleName; 
            middleName = input.nextLine();


            System.out.print("\nInput your  birthdate: ");
            String birthDate; 
            birthDate = input.nextLine();


            System.out.print("\nInput your  marital status (Married, Widowed, Separated, Divorced, Single) : ");
            String maritalStatus; 
            maritalStatus = input.nextLine();

            System.out.print("\nInput your  home address: ");
            String homeAddress; 
            homeAddress = input.nextLine();

            curriculumVitae1.cv();
    }
    private static void provincial(String args[]){
        Scanner input = new Scanner(System.in); 
            System.out.print("\nDo you have a provincial address? Enter Y if yes, and N if no:  ");
            char provincialQuestion; 
            provincialQuestion = input.nextLine().charAt(0);
            if (provincialQuestion=='Y'){
                System.out.print("\nInput your  provincial address: ");
                String provincialAddress; 
                provincialAddress = input.nextLine();
            }
            else if(provincialQuestion=='N'){
            }
    }
    private static  void mobile(String args[]){
        Scanner input = new Scanner(System.in); 
            System.out.print("\nContact Details ");
                System.out.print("\nInput your mobile number: ");
                String mobileNumber; 
                mobileNumber = input.nextLine();
            System.out.print("\nDo you have another mobile number? Enter Y if yes, and N if no:  ");
            char mobileQuestion; 
            mobileQuestion = input.nextLine().charAt(0);
            if (mobileQuestion=='Y'){
                System.out.print("\nInput another mobile number: ");
                String anotherMobile; 
                anotherMobile = input.nextLine();

            }
            else if(mobileQuestion=='N'){   

            }
    }
    private static void landline(String args[]){
        Scanner input = new Scanner(System.in); 
            System.out.print("\nInput your landline number: ");
                String landlineNumber; 
                landlineNumber = input.nextLine();
                System.out.print("\nDo you have another landline number? Enter Y if yes, and N if no:  ");
            char landlineQuestion; 
            landlineQuestion = input.nextLine().charAt(0);
            if (landlineQuestion=='Y'){
                System.out.print("\nInput another mobile number: ");
                String anotherLandline; 
                anotherLandline = input.nextLine();

            }
            else if (landlineQuestion=='N'){

            }
    }
    private static String email(){
        Scanner input = new Scanner(System.in);
            System.out.print("\nInput your email address: ");
                String emailAddress; 
                emailAddress = input.nextLine();
                return emailAddress;
    }
    private static String tertiary(){
        Scanner input = new Scanner(System.in);
            System.out.print("\nEducation History ");
            System.out.print("\nTertiary Education ");

            System.out.print("\nInput your tertiary education course: ");
                String tertiaryCourse; 
                tertiaryCourse = input.nextLine();

            System.out.print("\nInput your tertiary education school: ");
                String tertiarySchool; 
                tertiarySchool = input.nextLine();

            System.out.print("\nInput your tertiary education inclusive years (xxxx-xxxx): ");
                String tertiaryYears; 
                tertiaryYears = input.nextLine();

            System.out.print("\nDo you have any honors/achivements received during your tertiary education? Enter Y if yes, and N if no:  ");
            char tertiaryQuestion; 
            tertiaryQuestion = input.nextLine().charAt(0);
            if (tertiaryQuestion=='Y'){
                System.out.print("\nInput your honor/s or achivement/s:");
                String tertiaryAchievements; 
                tertiaryAchievements = input.nextLine();
                return tertiaryAchievements;
            }
            else if (tertiaryQuestion=='N'){
                return "------";
            }
    }
    private static void secondary(String args[]){
        Scanner input = new Scanner(System.in);
            System.out.print("\nSecondary Education ");
            System.out.print("\nInput your secondary education school: ");
                String secondarySchool; 
                secondarySchool = input.nextLine();

            System.out.print("\nInput your secondary education inclusive years (xxxx-xxxx): ");
                String secondaryYears; 
                secondaryYears = input.nextLine();

            System.out.print("\nDo you have any honors/achivements received during your secondary education? Enter Y if yes, and N if no:  ");
            char secondaryQuestion; 
            secondaryQuestion = input.nextLine().charAt(0);
            if (secondaryQuestion=='Y'){
                System.out.print("\nInput your honor/s or achivement/s:");
                String secondaryAchievements; 
                secondaryAchievements = input.nextLine();
            }
            else if (secondaryQuestion=='N'){
            }
    }
        public static void primary(String args[]){
            Scanner input = new Scanner(System.in);
            System.out.print("\nPrimary Education ");
            System.out.print("\nInput your primary education school: ");
                String primarySchool; 
                primarySchool = input.nextLine();


            System.out.print("\nInput your primary education inclusive years (xxxx-xxxx): ");
                String primaryYears; 
                primaryYears = input.nextLine();


            System.out.print("\nDo you have any honors/achivements received during your primary education? Enter Y if yes, and N if no:  ");
            char primaryQuestion; 
            primaryQuestion = input.nextLine().charAt(0);
            if (primaryQuestion=='Y'){
                System.out.print("\nInput your honor/s or achivement/s:");
                String primaryAchievements; 
                primaryAchievements = input.nextLine();
            }   
            else{
                System.out.print("------");
            }
        }
        public static void cv(String args[]){
            System.out.println("                                               Curriculum Vitae");
            System.out.print("\nName:" + firstName + " " + middleName + " "+ lastName);
            System.out.print("\nBirthdate:" + birthDate);
            System.out.print("\nMarital Status:" + maritalStatus);
            System.out.print("\nHome Address:" + homeAddress);
            System.out.print("\nProvincial Address:" + provincialAddress);
            System.out.print("\nMobile Number:" + mobileNumber );
            System.out.print("\nAnother Mobile Number:" + anotherMobile);
            System.out.print("\nLandline:" + landlineNumber);
            System.out.print("\nYear: " + primaryYears);


}
}

但是,我总是得到这样的错误

 C:\Users\BEST\Desktop\wew>javac curriculumVitae1.java
curriculumVitae1.java:33: error: method cv in class curriculumVitae1 cannot be applied to given types;
                        curriculumVitae1.cv();
                                        ^
  required: String[]
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

请帮助我如何从其他函数中打印出另一个变量。或我可以做的一些选择。

3 个答案:

答案 0 :(得分:2)

您的方法期望使用(String[] args),但是由于您不使用它们,因此我将其删除。试试

public static void cv() {

答案 1 :(得分:0)

错误详细信息突出显示您正在调用方法而未在方法签名public static void cv(String args[])中提供必需的参数:

  • 必填部分告诉您期望的参数类型,此处String[] found 部分告诉您所看到的参数,此处它没有看到您传递任何参数。

  • 原因告诉您,实际(提供的参数)数量与期望的形式(参数签名定义的形式)数量不同,即数量不足或过多提供了论据。

您也可以从原始错误消息中获得此信息:

error: method cv in class curriculumVitae1 cannot be applied to given types;
                        curriculumVitae1.cv();

它没有明确声明,但是从下面显示的代码行中,您可以看到“给定类型”一无是处,因为该方法的调用没有参数-括号内没有任何内容。

就像彼得·劳里(Peter Lawrey)所说,您可以从方法签名中删除String args[],因为您不使用它。

希望这可以帮助您了解错误消息以及它们告诉您的问题更好!

答案 2 :(得分:0)

对程序进行以下更改:

  • 将main()方法的访问说明符从私有更改为公共。否则,代码将引发错误-Does not contain a main method

  • 由于已将firstName,middleName,lastName,birthDate等声明为静态变量,因此请勿在{{1​​}}中将它们声明为局部变量。将值分配给main()方法中已经声明的静态变量,如下所示:

    main method
    • 由于未使用public static void main (String args[]){ Scanner input = new Scanner(System.in); System.out.print("\nCurriculum Vitae"); System.out.print("\nInput your last name: "); //String lastName; lastName = input.nextLine(); System.out.print("\nInput your first name: "); //String firstName; firstName = input.nextLine(); System.out.print("\nInput your middle name: "); //String middleName; middleName = input.nextLine(); System.out.print("\nInput your birthdate: "); //String birthDate; birthDate = input.nextLine(); System.out.print("\nInput your marital status (Married, Widowed, Separated, Divorced, Single) : "); //String maritalStatus; maritalStatus = input.nextLine(); System.out.print("\nInput your home address: "); //String homeAddress; homeAddress = input.nextLine(); //System.out.println(); cv(); } 方法中的String[] args自变量。

    • 由于cv()cv(),因此可以直接从static method调用它。

    • 从tertiary()方法返回字符串类型可变,因为它给出了编译错误。您可以通过在if-else块外声明main()变量,然后按如下所示返回它来完成此操作:

    tertiaryAchievements