0

好的,所以我有一个学校项目的以下代码。到目前为止只完成了一半。我对“居民”功能有疑问。它说我不能从静态上下文中引用非静态变量。

import java.util.Scanner;
class Hotel
{int noofemployees=10;
 int salary2=10000;
 int salary3=8000;
 int salary4=2000;

 String resident1="John Smith";
 String resident2="John Smith";
 String resident3="John Smith";
 String resident4="John Smith";
     String resident5="John Smith";
     String resident6="John Smith";
     String resident7="John Smith";
     String resident8="John Smith";
     String resident9="John Smith";
     String resident10="John Smith";

public static void main()
{   

    Scanner input=new Scanner(System.in);
    System.out.println("     ___o___    ");
    System.out.println("    /   |   \\  ");
    System.out.println(" __/____|____\\__");
    System.out.println("|   x   |  |||  |");
    System.out.println("|  xxx  |  ooo  |");
    System.out.println("|   x   |  |||  |");
    System.out.println("|-------|-------|");
    System.out.println("\\  []   |   $$  /");
    System.out.println(" \\  []  |  $$  /");
    System.out.println("   -----|----- ");
    System.out.println("");
    System.out.println("Welcome to the hotel automated management and guest system.");
    System.out.println("Enter 1 if you're a hotel employee.");
    System.out.println("Enter 2 if you're a resident.");
    System.out.println("Enter 3 to exit.");

    int choice=input.nextInt();
    if(choice==1)
        employee();//user is an employee
    else if(choice==2)
        resident();//user is a resident
    else if(choice==1997)
        easteregg();
    else
        {
         System.out.println("That is not a valid choice.");
        }
}

public static void employee()
    {   clearscreen();
        Scanner input=new Scanner(System.in);
        System.out.println("Enter the employee code");
        int code=input.nextInt();
        if(code==1111)
            {  clearscreen();
                System.out.println("Welcome employee.");
                System.out.println("Choose your rank.");
                System.out.println("1. Manager");
                System.out.println("2. Accounts");
                System.out.println("3. Human resources");
                System.out.println("4. Cleaner");
                System.out.println("5. Return to main menu");


                int rank=input.nextInt();
                if(rank==5)
                    main();
                else employees(rank);
            }
        else
           { 
            System.out.println("You aren't an employee.");
            main();
           }
    }

public static void resident()
    {   clearscreen();
        System.out.println("Greetings, guest!");
        System.out.println("Press 1 to sign in.");
        System.out.println("Press 2 to query guestlist.");
        System.out.println("Press 3 to sign out.");
        System.out.println("Press 4 to return to the main menu.");
        Scanner input=new Scanner(System.in);
        int choice=input.nextInt();

        if(choice==1)
            {System.out.println("Enter your name");
             String name=input.nextLine();
             System.out.println("Enter the room you wish to occupy");
             int room=input.nextInt();

                residents(room,0,1,name);
            }
        else if(choice==2)
            {for(int i=1;i<=10;i++)
                {System.out.println(residents(i,0,0," "));
                }  
            }
        else if(choice==3)
            {
             System.out.println("Enter the resident number.");
             int resno=input.nextInt();

             residents(resno,1,0," ");   
            }

        else if(choice==4)
            {main();
            }
        main();
    }

public static void easteregg()
    {

    }

public static void employees(int rank)
    {
        clearscreen();clearscreen();
        if(rank==1)
            manager();
        else if(rank==2)
            accounts();
        else if(rank==3)
            hr();
        else if(rank==4)
            clean();
    }

public static void manager()
    {
        System.out.println("Welcome manager");
        System.out.println("1. View wages");
        System.out.println("2. Edit wages");
        System.out.println("3. View guests");
        System.out.println("4. Evict guests");
        Scanner input=new Scanner(System.in);
        int choice=input.nextInt();
        if(choice==1)
            {
            }
        else if(choice==2)
            {
            }
        else if(choice==3)
            {for(int i=1;i<=10;i++)
                {System.out.println(residents(i,0,0," "));
                }
             main();
            }
        else if(choice==4)
            {System.out.println("Enter the resident number.");
             int resno=input.nextInt();

             residents(resno,1,0," ");
             main();
            }
    }

public static void accounts()
    {

    }

public static void hr()
    {

    }

public static void clean()
    {

    }

public static String residents(int resname, int evict, int replace, String newname)
    {

     String cur="nobody";

    if(resname==1)
        {if(evict==1)
            resident1="Empty";
         if(replace==1)
            resident1=newname;
         cur=resident1;
        }
    else if(resname==2)
        {if(evict==1)
            resident2="Empty";
         if(replace==1)
            resident1=newname;
         cur=resident2;
        }
    else if(resname==3)
        {if(evict==1)
            resident3="Empty";
         if(replace==1)
            resident1=newname;
         cur=resident3;
        }
    else if(resname==4)
        {if(evict==1)
            resident4="Empty";
         if(replace==1)
            resident1=newname;
         cur=resident4;
        }
    else if(resname==5)
        {if(evict==1)
            resident5="Empty";
         if(replace==1)
            resident1=newname;
         cur=resident5;
        }
    else if(resname==6)
        {if(evict==1)
            resident6="Empty";
         if(replace==1)
            resident1=newname;
         cur=resident6;
        }
    else if(resname==7)
        {if(evict==1)
            resident7="Empty";
         if(replace==1)
            resident1=newname;
         cur=resident7;
        }
    else if(resname==8)
        {if(evict==1)
            resident8="Empty";
         if(replace==1)
            resident1=newname;
         cur=resident8;
        }
    else if(resname==9)
        {if(evict==1)
            resident9="Empty";
         if(replace==1)
            resident1=newname;
         cur=resident9;
        }
    else if(resname==10)
        {if(evict==1)
            resident10="Empty";
         if(replace==1)
            resident1=newname;
         cur=resident10;
        }
    return cur;
}




public static void clearscreen()
   {for(int i=0;i<=2;i++)
       {System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("");
    }
}
}
4

2 回答 2

2

您必须将字符串变量设为静态才能使其正常工作:

 static String resident1="John Smith";
 static String resident2="John Smith";
 static String resident3="John Smith";
 static String resident4="John Smith";
 static String resident5="John Smith";
 static String resident6="John Smith";
 static String resident7="John Smith";
 static String resident8="John Smith";
 static String resident9="John Smith";
 static String resident10="John Smith";

这里更好的首选方法是使所有方法和类变量非静态创建类的实例

于 2012-10-20T11:50:54.050 回答
1

首先:当您这样做时,您正在修改一个尚不存在的变量: resident1="Empty";

您需要使所有residents 存在,或者通过使这些字符串静态(通常如上所述),或者在您的中创建一个新的 Hotel 对象,main()这将允许您修改所有居民。

You will also need to change the calls to main(); in employee, resident etc. Replace them with: return;

hth

于 2012-10-20T11:56:52.543 回答