有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java被if语句卡住并混淆

我有点卡住了。我希望我的程序接受0和非0数字,但如果非0数字高于或低于某个整数,它将无法工作。希望这是有道理的

以下是我所拥有的:

    if (pos != 0 || pos == 0)
    {

        System.out.println ("\nLength of int: " + len);
        System.out.println ("Position from right: " + pos);
        System.out.println ("Number selected: " + String.valueOf (nums).charAt (s));

    }

    else if (pos < len || pos > len)
    {
        System.out.println ("\n0");

    }

如果有人想看完整的代码,就在这里:http://pastebin.com/bFVYNhvr

谢谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    从另一个角度改变你的逻辑

    if (pos < len || pos > len)
    {
        System.out.println ("\n0");
    }
    else 
    {
        System.out.println ("\nLength of int: " + len);
        System.out.println ("Position from right: " + pos);
        System.out.println ("Number selected: " + String.valueOf (nums).charAt (s));
    }
    

    正如@Vinod提到的

    if (pos < len || pos > len)
    

    写得更好

    if (pos != len)
    

    如果这确实是你的意图