如何修复这些错误?java程序
我一直在做一个项目 我不断地发现这些错误:如何修复这些错误?java程序,java,loops,if-statement,Java,Loops,If Statement,我一直在做一个项目 我不断地发现这些错误: StationInformation.java:65: error: non-static variable this cannot be referenced from a static context Station mile_end = new Station(); ^ StationInformation.java:66: error: non-static variable thi
StationInformation.java:65: error: non-static variable this cannot be referenced from a static context
Station mile_end = new Station();
^
StationInformation.java:66: error: non-static variable this cannot be referenced from a static context
Station oxford_circus = new Station();
^
StationInformation.java:67: error: non-static variable this cannot be referenced from a static context
Station kings_cross = new Station();
^
StationInformation.java:68: error: non-static variable this cannot be referenced from a static context
Station stepney_green = new Station();
^
4 errors
我想修复程序。编辑:由于OP决定编辑问题,在编辑过程中删除了他的代码,因此此答案可能看起来过时了 您的代码中有2个错误:
Station
不是静态的,这意味着您不能在静态上下文中实例化它。这将生成您看到的错误消息站点
-class设置为静态(静态类站点
),使用类变量并使用其字段创建字符串来修复错误。您还可以为自己准备信息的
Station
-类实现getInfo()
-方法。这样,您只需调用System.out.println(STATION\u you\u WANT.getInfo())
我花了一点时间为这个问题写了一个有评论的解决方案。其中最令人困惑的部分可能是(
String…
在下面的代码中)的使用。它们基本上允许您将任意数量的参数传递给一个方法,该方法本身将由Java转换为数组
import java.util.HashMap;
导入java.util.Locale;
导入java.util.Scanner;
公共类站点信息{
专用静态类站{
私有字符串名称;
专用int距离平台;
私有布尔无步;
私有字符串[]可选名称;
站点(字符串名称、int-distanceToPlatform、布尔无步、字符串…可选名称){
this.name=名称;
this.distanceToPlatform=distanceToPlatform;
this.stepFree=stepFree;
this.alternateNames=alternateNames;//“String…”使该参数成为可选参数,如果未传递任何值,则会导致“null”
}
字符串getInfo(){
返回名称+“does”+(无步骤?”:“not”)
+“具有无步进访问权限。\n它是从入口到平台的“+距离+”m。”;
}
}
私有静态HashMap站=新HashMap();
公共静态void main(字符串[]args){
createStations();
//节目
扫描仪=新的扫描仪(System.in);
//继续请求输入,直到收到有效数字
int-num;
for(;;){/'for(;;)…'实际上与'while(true)…'相同
System.out.print(“您需要知道多少电台?”);
字符串输入=scanner.nextLine();
试一试{
num=整数.parseInt(输入);
打破
}捕获(异常exc){
//无所事事
}
System.out.println(“请输入正确的数字”);
}
对于(int i=0;i
我已将Station类设置为静态,并从create_messages()返回一个列表
//此程序告诉用户站点是否为无步进访问站点,以及它离平台有多远
导入java.util.Scanner;//导入扫描仪功能以从用户输入数据
导入java.util.ArrayList;
导入java.util.List;
类站信息{
publicstaticvoidmain(String[]args)//方法排序的main方法
{
int numberOfStations=inputint(“您想知道多少个电台?”);
弦站;
对于(int i=1;i您想修复它什么?它当前是否出错?您似乎试图通过create_messages
中的out参数初始化字符串-这在Java中是不可能的。引用是按值传递的,并且不能在方法内以从方法外可见的方式修改。请参阅i get t修改程序后出现错误:错误:无法找到或加载主类StationInformation,原因是:java.lang.ClassNotFoundException:StationInformation
现在可以工作。我已将StationInformation重命名为main。
//this program tells the user whether a station is a step free access station or not and how far is it from the platform
import java.util.Scanner; // imports the scanner function to input data from the user
import java.util.ArrayList;
import java.util.List;
class StationInformation {
public static void main(String[] args) // main method where methods are sequenced
{
int numberOfStations = inputint("how many stations do you want to know about?");
String station;
for (int i = 1; i <= numberOfStations; i++) {
station = inputstring("what station do you want to know about?");
search_station(station);
}
System.exit(0);
}
// A method to input integers
public static int inputint(String message) {
Scanner scanner = new Scanner(System.in);
int answer;
System.out.println(message);
answer = Integer.parseInt(scanner.nextLine());
return answer;
} // END inputInt
public static String inputstring(String message) {
Scanner scanner = new Scanner(System.in);
String answer;
System.out.println(message);
answer = scanner.nextLine();
return answer;
}
public static String create_message(Station station) {
String message;
if (station.step_free_access == true) {
message = (station.name + "does have step free access. " + "it is " + station.distance_from_platform
+ "m away from the entrance");
} else {
message = (station.name + "does not have step free access. " + "it is " + station.distance_from_platform
+ "m away from the entrance");
}
return message;
}
public static List<String> create_messages() {
Station mile_end = new StationInformation.Station();
Station oxford_circus = new Station();
Station kings_cross = new Station();
Station stepney_green = new Station();
mile_end.distance_from_platform = 50;
mile_end.name = "Mile End ";
mile_end.step_free_access = false;
String message1 = create_message(mile_end);
oxford_circus.distance_from_platform = 200;
oxford_circus.name = " Oxford Circus ";
oxford_circus.step_free_access = true;
String message2 = create_message(oxford_circus);
kings_cross.distance_from_platform = 700;
kings_cross.name = " kings cross ";
kings_cross.step_free_access = true;
String message3 = create_message(kings_cross);
stepney_green.distance_from_platform = 300;
stepney_green.name = " Stepney Green ";
stepney_green.step_free_access = false;
String message4 = create_message(stepney_green);
List<String> list = new ArrayList<>();
list.add(message1);
list.add(message2);
list.add(message3);
list.add(message4);
return list;
}
public static void search_station(String station) {
List<String> list = create_messages();
String mileEndMessage = list.get(0);
String oxfordCircusMessage = list.get(1);
String kingsCrossMessage = list.get(2);
String stepneyGreenMessage = list.get(3);
if (station.equals("Mile End")) {
System.out.println(mileEndMessage);
} else if (station.equals("kings cross")) {
System.out.println(kingsCrossMessage);
} else if (station.equals("oxford circus")) {
System.out.println(oxfordCircusMessage);
} else if (station.equals("stepney green")) {
System.out.println(stepneyGreenMessage);
} else {
System.out.println(station + " is not a London underground station ");
}
}
static class Station // a record to store information about stations
{
int distance_from_platform;
String name;
boolean step_free_access;
}
}