简体   繁体   English

为什么我没有输出?

[英]Why am I not getting output?

Given question is: A string can contain only a, b or c. 给定的问题是:字符串只能包含a,b或c。 There cannot be 2 consecutive same character. 不能有2个连续的相同字符。 First and last character cannot be same. 第一个字符和最后一个字符不能相同。 Now given a string with 'a', 'b', 'c' or '?'. 现在给出一个带有'a','b','c'或'?'的字符串。 We need to find the string replacing '?' 我们需要找到替换'?'的字符串 that satisfy the above conditions. 满足以上条件。 For multiple answer display lexicographically smallest string. 对于多个答案,按字典顺序显示最小的字符串。 For no answer possible display “Not Possible”. 如果没有答案,请显示“不可能”。

import java.util.*;

class Replace {
    public static void main(String args[]) {
        char[] arr = { 'a', 'b', 'c' };
        char Pre, Suc;
        Scanner in = new Scanner(System.in);
        String str = new String();
        String str2 = new String();
        System.out.println("Enter the String");
        while (in.hasNextLine()) {
            str = in.nextLine();
        }
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == '?') {
                Pre = str.charAt(i - 1);
                Suc = str.charAt(i + 1);
                for (int j = 0; j < 3; i++) {
                    while (arr[j] != Pre && arr[j] != Suc) {
                        str2 = str.substring(0, i) + arr[j]
                                + str.substring(i + 1, (str.length() - 1));
                    }
                }
            }
        }
        System.out.println(str2);
    }
}

The code is compiling without any errors. 代码正在编译,没有任何错误。 I still have to add a couple of things to the code as per question but I was trying to check if the code was correct so far but I am not getting any Output. 我仍然必须根据问题向代码中添加一些内容,但是我试图检查到目前为止的代码是否正确,但是我没有得到任何输出。 Any tips/suggestions to improve the code is welcome. 欢迎任何改进代码的技巧/建议。

  • The code Pre = str.charAt(i-1); 代码Pre = str.charAt(i-1); and Suc = str.charAt(i+1); Suc = str.charAt(i+1); is problematic when "?" 什么时候有问题? is the first/ last letter. 是第一个/最后一个字母。 It will cause then a java.lang.StringIndexOutOfBoundsException 这将导致java.lang.StringIndexOutOfBoundsException
  • At present you are not leaving the while -loop used for reading the input, hence System.out.println(str2); 当前,您不会离开while -loop用来读取输入的内容,因此是System.out.println(str2); is never reached. 永远不会到达。

The problem is the program gets stuck in your while(in.hasNextLine()) { str = in.nextLine(); } 问题是程序陷入了while(in.hasNextLine()) { str = in.nextLine(); } while(in.hasNextLine()) { str = in.nextLine(); } loop. while(in.hasNextLine()) { str = in.nextLine(); }循环。 There is no exit condition. 没有退出条件。 hasNextLine will block until a new line is entered. hasNextLine将阻塞,直到输入新行。 As per the Javadoc: 根据Javadoc:

This method may block while waiting for input. 等待输入时,此方法可能会阻塞。

You need a condition to break the first while loop. 您需要一个条件来打破第一个while循环。 When the user insert the input string he press enter, so the Scanner get the second input as an empty string. 用户插入输入字符串时,请按Enter键,以便扫描程序将第二个输入作为空字符串获取。 You could check the empty string and exit from the while loop. 您可以检查空字符串并退出while循环。

import java.util.*;

class Replace
{
    public static void main(String[] args)
    {
        char[] arr = {'a','b','c'}; 
        char Pre,Suc;
        Scanner in = new Scanner(System.in);
        String str = new String();
        String str2 = new String();
        System.out.println("Enter the String");
        while(in.hasNextLine())
        {
            if(str.isEmpty()) break;
            str = in.nextLine();
        }
        for(int i=0;i<str.length();i++)
        {
            if(str.charAt(i)=='?')
            {
                Pre = str.charAt(i-1);
                Suc = str.charAt(i+1);
                for(int j=0;j<3;i++)
                {
                    while(arr[j]!=Pre && arr[j]!=Suc)
                    {
                        str2 = str.substring(0,i)+arr[j]+str.substring(i+1,(str.length()-1));
                    }
                }
            }
        }
        System.out.println(str2);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM