Public class Main 不工作,这段代码是否可行?
Public class Main not working, is this code even possible or?
我正在用 Java 做一些练习,我决定尝试一些东西,所以我请我的教授给我一些练习的想法,然后想出了这个。当我在 Eclipse 中编译它时,它不起作用,它与 public class Main 有关,但我不知道是什么。请告诉我我做错了什么,或者这是否可行。
This is the error I get
public class Forecast
{
public int temperature;
public int pressure;
}
public class Main
{
public static void changeTheString(String weather)
{
weather = "sunny";
}
public static void changeTheArray(String[] rainyDays)
{
rainyDays[1] = "Sunday";
}
public static void changeTheObject(Forecast forecast)
{
forecast.temperature = 35;
}
public static void main (String[] args)
{
String weather = "rainy";
changeTheString(weather);
String[] rainyDays;
System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);
String[] rainyDays = new String[] {"Monday", "Friday"};
changeTheArray(rainyDays);
System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);
Forecast forecast = new Forecast();
forecast.pressure = 700;
forecast.temperature = 20;
changeTheObject(forecast);
System.out.println("The temperature is " + forecast.temperature + " Celsius");
}
}
正如 aziz_aon 所指出的,您还需要在单独的文件中声明 class main 或将 class Forecast 设为内部 class。
我建议将 Forecast class 移动到 Main class 内
为此,您必须将它移动到 Main 中,就像使用函数一样。
(抱歉格式化我在手机上)
问题是你声明
String[] rainyDays;
然后您尝试指定一个新值,例如
String[] rainyDays = new String[] {"Monday", "Friday"};
但是您已经将 rainyDays
定义为 String[],所以您不能再这样做了
将其更改为
rainyDays = new String[] {"Monday", "Friday"};
所以你的 class 看起来像这样:
public class Forecast
{
public int temperature;
public int pressure;
}
public class Main
{
public static void changeTheString(String weather)
{
weather = "sunny";
}
public static void changeTheArray(String[] rainyDays)
{
rainyDays[1] = "Sunday";
}
public static void changeTheObject(Forecast forecast)
{
forecast.temperature = 35;
}
public static void main (String[] args)
{
String weather = "rainy";
changeTheString(weather);
String[] rainyDays;
System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);
rainyDays = new String[] {"Monday", "Friday"};
changeTheArray(rainyDays);
System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);
Forecast forecast = new Forecast();
forecast.pressure = 700;
forecast.temperature = 20;
changeTheObject(forecast);
System.out.println("The temperature is " + forecast.temperature + " Celsius");
}
}
你有两个选择:
1- 在其自己的文件中定义主要 class(与 Forecast 分开)
2- 将主 class 设为嵌套 class:您的代码将如下所示:
public class Forecast
{
public int temperature;
public int pressure;
public class Main
{
public static void changeTheString(String weather)
{
weather = "sunny";
}
public static void changeTheArray(String[] rainyDays)
{
rainyDays[1] = "Sunday";
}
public static void changeTheObject(Forecast forecast)
{
forecast.temperature = 35;
}
}
public static void main (String[] args)
{
String weather = "rainy";
changeTheString(weather);
String[] rainyDays;
System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);
String[] rainyDays = new String[] {"Monday", "Friday"};
changeTheArray(rainyDays);
System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);
Forecast forecast = new Forecast();
forecast.pressure = 700;
forecast.temperature = 20;
changeTheObject(forecast);
System.out.println("The temperature is " + forecast.temperature + " Celsius");
}
}
我正在用 Java 做一些练习,我决定尝试一些东西,所以我请我的教授给我一些练习的想法,然后想出了这个。当我在 Eclipse 中编译它时,它不起作用,它与 public class Main 有关,但我不知道是什么。请告诉我我做错了什么,或者这是否可行。
This is the error I get
public class Forecast
{
public int temperature;
public int pressure;
}
public class Main
{
public static void changeTheString(String weather)
{
weather = "sunny";
}
public static void changeTheArray(String[] rainyDays)
{
rainyDays[1] = "Sunday";
}
public static void changeTheObject(Forecast forecast)
{
forecast.temperature = 35;
}
public static void main (String[] args)
{
String weather = "rainy";
changeTheString(weather);
String[] rainyDays;
System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);
String[] rainyDays = new String[] {"Monday", "Friday"};
changeTheArray(rainyDays);
System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);
Forecast forecast = new Forecast();
forecast.pressure = 700;
forecast.temperature = 20;
changeTheObject(forecast);
System.out.println("The temperature is " + forecast.temperature + " Celsius");
}
}
正如 aziz_aon 所指出的,您还需要在单独的文件中声明 class main 或将 class Forecast 设为内部 class。 我建议将 Forecast class 移动到 Main class 内 为此,您必须将它移动到 Main 中,就像使用函数一样。 (抱歉格式化我在手机上) 问题是你声明
String[] rainyDays;
然后您尝试指定一个新值,例如
String[] rainyDays = new String[] {"Monday", "Friday"};
但是您已经将 rainyDays
定义为 String[],所以您不能再这样做了
将其更改为
rainyDays = new String[] {"Monday", "Friday"};
所以你的 class 看起来像这样:
public class Forecast
{
public int temperature;
public int pressure;
}
public class Main
{
public static void changeTheString(String weather)
{
weather = "sunny";
}
public static void changeTheArray(String[] rainyDays)
{
rainyDays[1] = "Sunday";
}
public static void changeTheObject(Forecast forecast)
{
forecast.temperature = 35;
}
public static void main (String[] args)
{
String weather = "rainy";
changeTheString(weather);
String[] rainyDays;
System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);
rainyDays = new String[] {"Monday", "Friday"};
changeTheArray(rainyDays);
System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);
Forecast forecast = new Forecast();
forecast.pressure = 700;
forecast.temperature = 20;
changeTheObject(forecast);
System.out.println("The temperature is " + forecast.temperature + " Celsius");
}
}
你有两个选择:
1- 在其自己的文件中定义主要 class(与 Forecast 分开)
2- 将主 class 设为嵌套 class:您的代码将如下所示:
public class Forecast
{
public int temperature;
public int pressure;
public class Main
{
public static void changeTheString(String weather)
{
weather = "sunny";
}
public static void changeTheArray(String[] rainyDays)
{
rainyDays[1] = "Sunday";
}
public static void changeTheObject(Forecast forecast)
{
forecast.temperature = 35;
}
}
public static void main (String[] args)
{
String weather = "rainy";
changeTheString(weather);
String[] rainyDays;
System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);
String[] rainyDays = new String[] {"Monday", "Friday"};
changeTheArray(rainyDays);
System.out.println("The rainy days were on " + rainyDays[0] + " and " + rainyDays[1]);
Forecast forecast = new Forecast();
forecast.pressure = 700;
forecast.temperature = 20;
changeTheObject(forecast);
System.out.println("The temperature is " + forecast.temperature + " Celsius");
}
}