基本Java帮助:如何修复我的代码?

时间:2012-10-20 11:48:36

标签: java syntax

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

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("");
    }
}
}

2 个答案:

答案 0 :(得分: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";

这里更好的首选方法是制作所有方法&amp;类变量非静态创建类的实例

答案 1 :(得分:1)

首先:您正在修改当前执行此操作时尚不存在的变量: resident1="Empty";

你需要让所有resident存在,或者通过使这些字符串保持静态(通常是错误的),或者在main()中创建一个新的酒店对象,这将允许你修改所有居民。

您还需要更改员工,居民等的main();来电。将其替换为: return;

HTH