首页 > 解决方案 > 你能告诉我为什么运行时没有输出吗

问题描述

我正在尝试解决https://www.codechef.com/problems/FLOW010问题。我写了`

public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t>0){
            String s=sc.next();
            if(s=="b" || s=="B"){
                System.out.println("BattleShip");
            }
            else if(s=="c" || s=="C"){
                System.out.println("Cruiser");
            }
            else if(s=="d" || s=="D"){
                System.out.println("Destroyer");
            }
            else if(s=="f" || s=="F"){
                System.out.println("Frigate");
            }
            t--;
        }
    }

` 语法上没有错误。请帮助我什么是错误

标签: javastringdebugging

解决方案


像这样替换所有==对 String 的比较:

String s=sc.next();
if(s.equalsIgnoreCase("b") {
    System.out.println("BattleShip");
}
// ... etc.

推荐阅读