作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,我有一个学校项目的以下代码。到目前为止只完成了一半。我对“居民”功能有疑问。它说我无法从静态上下文引用非静态变量。
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("");
}
}
}
最佳答案
您必须将字符串变量设为静态才能使其工作:
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";
这里更好的首选方法是使所有方法和类变量成为非静态的,创建类的实例。
关于java - 基本 Java 帮助 : How to fix my code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12988117/
我是一名优秀的程序员,十分优秀!