公共类主要不工作,这个代码甚至可能或?

时间:2018-04-30 13:40:50

标签: java main

我正在做一些Java练习,我决定尝试一些事情,所以我让我的教授给我一些想法练习,并想出了这个。当我在Eclipse中编译它时它不起作用,它与公共类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");

    }

}

2 个答案:

答案 0 :(得分:1)

正如aziz_aon指出的那样,您还需要在单独的文件中声明类main,或者将类预测为内部类。 我建议将Forecast类移到Main类中 要做到这一点,你必须将它移动到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 :(得分:1)

您有两种选择:

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

}

}