为什么这样做?Java是否会逐渐减少?

Aph*_*hex 1 java string substring

public class StringTest{
  public static void main(String[] args){
       String a = "Aphex";
       int len  = a.length();
       int lenA = len/2-1;
       int lenB = len/2+2;
       if(len%2==0){
           System.out.println("Number of letters has to be odd");
       }else{
           System.out.println(a.substring(lenA, lenB))   }}}

Output:
phe
Run Code Online (Sandbox Code Playgroud)

这个简单的代码可以使用任何具有奇数字母的字符串并找到三个中心字符串.我的问题是-为什么是居中的一个点从中间(下去len/2-1),然后两个景点了它不工作的其他方式(?len/2-2len/2+1).

Java总是四舍五入吗?

Mur*_*nik 5

您正在整数上下文中进行分割.除以两个int时,只保留结果的整数部分,其余部分将被丢弃.例如,29/10将导致2.