-2
    else if (answer.equals("check value")){
        System.out.println("Of what?");
        System.out.println("Tax, Donations, Income, Profit, Expenditure, Services, Military, City or Economy.");
        System.out.println("Do not use caps and spell it correctly otherwise it will not be recognised.");
        String valueToCheck = scan.nextLine();
        if (valueToCheck == "tax"){
            System.out.println(tax);
        }else if (valueToCheck == "donations"){
            System.out.println(donations + "donations per turn");
        }else if(valueToCheck == "income"){
            System.out.println(incomePerTurn + " income per turn");
        }else if(valueToCheck == "profit"){
            System.out.println(profitPerTurn + " profit per turn");
        }else if(valueToCheck == "expenditure"){
            System.out.println(expenditurePerTurn + "expenditure per turn");
        }else if(valueToCheck == "services"){
            System.out.println(servicesBudget + " to services budget");
        }else if(valueToCheck == "military"){
            System.out.println(militaryBudget + " to military budget");
        }else if(valueToCheck == "city"){
            System.out.println(cityBudget + " to cities budget");
        }else if(valueToCheck == "economy"){
            System.out.println(economy  + " to total economy");
        }
        newTurn();}

你好,每当我运行我的程序并输入“检查值”时,我都没有收到任何错误,实际上我什么也没得到。我只是想知道为什么什么都没有返回。谢谢你的帮助!

4

1 回答 1

2

什么都没有出现,因为这些条件都没有得到满足。"==" 运算符比较引用,但您想比较内容。由于您正在为每个比较创建新字符串,因此它们与存储在“valueToCheck”中的字符串不匹配。

有关更多详细信息,请参阅此问题

另外,以后请大家尽量猜测错误在哪里,发帖前先去论坛搜索一下。例如,这里有很多字符串比较,而您想要的输出是这些比较的结果。当您没有得到您想要的结果时,您应该在此处或其他地方查找“java 中的字符串比较”,以确保您在发布之前正确执行。

于 2013-08-21T20:09:59.987 回答