Java for循环不是';t在我的代码中终止

Java for循环不是';t在我的代码中终止,java,loops,for-loop,while-loop,stringbuilder,Java,Loops,For Loop,While Loop,Stringbuilder,出于某种原因,For循环没有以大写的first句子方法终止。我在该行设置了一个断点,条件(I!=-1)未满足,因此循环应该终止,但它没有 当我对条件使用(I>0)时,它工作 我不知道这里发生了什么 import javax.swing.JOptionPane; public class SentenceCapitalizer { //Main Method public static void main(String[] args) { String inp

出于某种原因,For循环没有以大写的first句子方法终止。我在该行设置了一个断点,条件(I!=-1)未满足,因此循环应该终止,但它没有

当我对条件使用(I>0)时,它工作

我不知道这里发生了什么

import javax.swing.JOptionPane;

public class SentenceCapitalizer {


    //Main Method
    public static void main(String[] args) {
        String input; //creates a String to hold keyboard input

        //Prompt the user to enter a String using JOptionPane and set it equal to input
        input = JOptionPane.showInputDialog("Enter a string. ");

        //Display the new String with the first letter of each sentenced capitalized
        JOptionPane.showMessageDialog(null, CapitalizeFirstSentence(input));

        //Exit the program
        System.exit(0);
    }


    //Capitalize first letter of each sentence
    public static String CapitalizeFirstSentence(String in)
    {
        //Creates a StringBuilder object initiralized to the String argument "in"
        StringBuilder temp = new StringBuilder(in);

        //Capitalize first letter of the string if string length is > 0
        if (temp.length() > 0)
        {
            temp.setCharAt(0, Character.toUpperCase(temp.charAt(0)));
        }

        //sets i equal to index of the space, 
        //keep capitalizing first letters of each sentence (loops each time it capitlizes a letter)
        //until very end of the String
        for (int i = temp.indexOf(". ")+1; i != -1; i++)
        {
            //Checks for extra spaces and moves index to first character of next sentence
            while (i < temp.length() && temp.charAt(i) == ' ')
            {
                i++;
            }

            //Capitalize character
            temp.setCharAt(i, Character.toUpperCase(temp.charAt(i)));

            //Index the end of the sentence
            i = temp.indexOf(". ", i);
        }

        //Convert temp to a String and return our new first-sentenced-capitalized String
        return temp.toString();

    }

}
import javax.swing.JOptionPane;
公共类句子资本家{
//主要方法
公共静态void main(字符串[]args){
字符串输入;//创建一个字符串来保存键盘输入
//提示用户使用JOptionPane输入字符串并将其设置为input
input=JOptionPane.showInputDialog(“输入字符串”);
//显示每个字符串的第一个字母大写的新字符串
showMessageDialog(null,大写第一句(输入));
//退出程序
系统出口(0);
}
//将每个句子的第一个字母大写
公共静态字符串大写第一句(字符串输入)
{
//创建初始化为字符串参数“in”的StringBuilder对象
StringBuilder温度=新StringBuilder(英寸);
//如果字符串长度大于0,则将字符串的第一个字母大写
如果(温度长度()>0)
{
临时字符集(0,字符集(0));
}
//设置i等于空间的索引,
//保持每个句子的首字母大写(每次字母大写时循环)
//直到最后一刻
对于(int i=temp.indexOf(“.”+1;i!=-1;i++)
{
//检查是否有多余的空格,并将索引移动到下一句的第一个字符
而(i
这一行:
for(inti=temp.indexOf(“.”+1;i!=-1;i++)
将i初始化为indexOf+1的结果。如果没有命中,IndexOf会给出-1,但在初始化过程中总是将1添加到该值,所以它永远不会小于0


使用
i>0
似乎很好。

首先,在for循环中修改循环控制变量不是一个好主意-读取和理解此类代码非常困难,并且容易出错

现在,以您的示例为例:

for (int i = temp.indexOf(". ")+1; i != -1; i++)
这意味着:

  • i
    初始化为
    temp.indexOf(“.”+1
    ,该值始终>=0
  • 如果
    i==-1
  • 每次迭代后,将
    i
    增加1
因此:

  • 在开始时,循环不会终止,因为初始化总是返回>=0
  • 每次迭代,循环体将设置
    i=temp.indexOf(“.”,i),即>=-1
  • 每次迭代后,
    i
    将增加1,因此现在它将>=0
  • 由于
    i
    始终>=0,因此它永远不会满足条件
    i==-1
    ,因此永远不会终止

您在
字符串变量中提供的
值是什么?可能的重复不是这么简单-OP正在修改循环中的
i
。是的,这是真的。你的答案更好。