2017-03-23 85 views
-2

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

这是我有:

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

任何帮助表示赞赏

+2

'POS!= 0 || pos == 0' [始终为真](https://en.wikipedia.org/wiki/Law_of_excluded_middle) – dasblinkenlight

+0

'pos'是一个'int' - 因为它可以接受'0'和'非0'数字 –

回答

2

变化反过来

你的逻辑
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)); 
} 

As @Vinod said

if (pos < len || pos > len) 

可以更好地写成

if (pos != len) 

如果这确实是你的意图