我正在用Java做一些练习,我决定尝试一些东西,所以我让我的教授给我一些练习的想法,并提出了这个。当我在Eclipse中编译它时,它不起作用,它与公共类Main有关,但我不知道是什么。请告诉我我做错了什么,或者这是否有效。
这是我得到的错误
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指出的,您还需要在单独的文件中声明类 main,或者将类 Forecast 设置为内部类。 我建议将预测类移到主类内 要做到这一点,你必须把它移到Main里面,就像你用函数一样。 (抱歉格式化我在手机上( 问题是你声明
String[] rainyDays;
然后你尝试吸收一个新值,如
String[] rainyDays = new String[] {"Monday", "Friday"};
但您已经将rainyDays
定义为 String[],因此您无法再次执行此操作
将其更改为
rainyDays = new String[] {"Monday", "Friday"};
所以你的类看起来像这样:
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-在其自己的文件中定义主类(与预测分开(
2-使主类成为嵌套类:你的代码将是这样的:
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");
}
}