系统采用无限输入

时间:2018-03-30 05:19:09

标签: java string

当我运行以下程序的代码时,系统正在获取无限输入:

  

编写程序以输入字符串。在自己的位置反转每个单词后打印新字符串。

     

示例输入:这是一个程序

     

样本输出:sihT SI a MARgrop

这是我到目前为止所做的:(在BlueJ IDE中)

import java.util.*;
import java.io.*;
class revword{
    public static void main(String args[])throws IOException{
        int i=0;//loop variable initialisation
        String temp="";//temporary variable which stores each word
        char ext;//stores character at each iteration
        String rev="";//stores the reversed string present in temp
        String sen="";//stores the new string 
         Scanner sc=new Scanner(System.in);//creating object of Scanner class
         System.out.println("Enter a string");
         String str=sc.nextLine();
         str=str+" ";//adding space after the last word stored in str
         for(i=0;i<str.length();i++){//for loop starts here
             ext=str.charAt(i);//extracting character present at ith index of str
             if(ext!=' '){
                 temp=temp+ext;//storing characters other than whitespace
                }
                else{//this condition works when character extracted from str is not whitespace
                    for(i=temp.length()-1;i>=0;i--){//loop for reversing each word
                        rev=rev+temp.charAt(i);//reversing the word pesent in temp
                    }

            temp="";//reinitialising variable temp
            sen=sen+rev+" ";//new string gets concatenated with each word and a space
            rev="";//reinitialising variable rev;
        }
    }
        System.out.println("The new String is "+sen);
    }
}

该程序已成功编译。但是在输入字符串后,系统希望获取更多字符串的输入。情况如下:

Snippet shows that the cursor is still blinking

我能够提供我想要的尽可能多的输入但是应该显示在屏幕上的最终字符串没有显示出来。系统只接受输入。 我做错了什么?

2 个答案:

答案 0 :(得分:0)

您已为内圈和外圈使用了相同的变量。 所以该计划不会按预期工作

import java.util.*;
    import java.io.*;
    class revword{
        public static void main(String args[])throws IOException{
            int i=0;//loop variable initialisation
            String temp="";//temporary variable which stores each word
            char ext;//stores character at each iteration
            String rev="";//stores the reversed string present in temp
            String sen="";//stores the new string 
             Scanner sc=new Scanner(System.in);//creating object of Scanner class
             System.out.println("Enter a string");
             String str=sc.nextLine();
             str=str+" ";//adding space after the last word stored in str
             for(i=0;i<str.length();i++){//for loop starts here
                 ext=str.charAt(i);//extracting character present at ith index of str
                 if(ext!=' '){
                     temp=temp+ext;//storing characters other than whitespace
                    }
                    else{//this condition works when character extracted from str is not whitespace
                        for(int j=temp.length()-1;j>=0;j--){//loop for reversing each word
                            rev=rev+temp.charAt(j);//reversing the word pesent in temp
                        }

                temp="";//reinitialising variable temp
                sen=sen+rev+" ";//new string gets concatenated with each word and a space
                rev="";/reinitialising variable rev
            }
        }
            System.out.println("The new String is "+sen);
        }
    }

答案 1 :(得分:0)

在这段代码中

for(i=0;i<str.length();i++){//for loop starts here
         ext=str.charAt(i);//extracting character present at ith index of str
         if(ext!=' '){
             temp=temp+ext;//storing characters other than whitespace
            }
            else{//this condition works when character extracted from str is not whitespace
                for(i=temp.length()-1;i>=0;i--){//loop for reversing each word
                    rev=rev+temp.charAt(i);//reversing the word pesent in temp
                }

        temp="";//reinitialising variable temp
        sen=sen+rev+" ";//new string gets concatenated with each word and a space
        rev="";//reinitialising variable rev
    }

您对两个循环使用相同的变量i。只需将新变量用于另一个循环。变量i在else块中重新初始化,因此循环不会停止