C# 为什么我的代码不能在Java中工作?

C# 为什么我的代码不能在Java中工作?,c#,java,C#,Java,以下是我的C代码: string month; do { Console.WriteLine("put month"); month = Console.ReadLine(); switch (month) { case "1": Console.WriteLine("dsad")

以下是我的C代码:

        string month;
        do
        {
            Console.WriteLine("put month");

            month = Console.ReadLine();
            switch (month)
            {
                case "1":
                    Console.WriteLine("dsad");
                    Console.ReadLine();
                    Console.WriteLine("\neheheh!");
                    Console.WriteLine("\nhihihihi");
                    break;

            }
        }while(month!="Quit");
这里是Java:

    Console C = System.console();

    String month;
    int year;

    do {
        month = C.readLine("\nPlease put a valid month: \n");
        switch (month) {
            case "1":

                C.readLine("\nPlease put a valid year: \n");
                System.out.println("\nThe month is January!");
                System.out.println("\nJanuary has 31 days!");
                break; 
        }
    } while (month != "Quit");
我的问题是,即使我键入Quit这个词,我的Java代码也不会终止

这是对。

使用相等方法进行相等的后续操作

do{
}while (!month.equals("Quit"));
在Java中,使用代替==来比较字符串:

将此字符串与指定的对象进行比较。当且仅当参数不为null并且是表示与此对象相同的字符序列的字符串对象时,结果才为true

如果使用==或!=,您只是测试引用相等,而不是值相等。

您应该使用.equals而不是==或!=用于Java中的字符串。 还有一点,不要用!month.equalsQuit,它可能会导致NullPointerException


作为有经验的开发人员,他们总是写:!Quit.equalsmonth

看到这个:因为这不是比较字符串的方式。!=正在检查参考值。您可能想从Oracle Java教程开始。另外:谢谢大家。我对java完全是新手,这就是为什么我倾向于首先在c上测试代码是否工作xD