简体   繁体   English

如果在字符串中单词中出现的相邻字母相同,则打印“相同”,否则打印“ diff”

[英]If in a String adjacent letters occurring in the word are same then print “same” otherwise “diff”

I am trying but for sometime input 4 integer it will give wrong output . 我正在尝试,但有时输入4整数会给出错误的输出。 please update my code or give me some idea. 请更新我的代码或给我一些想法。 Below is my sample output. 以下是我的示例输出。 and the code which i am trying. 和我正在尝试的代码。

Example output:- 输出示例:-

input:-
3
remember
occurring
apple



diff
Same
same

Code:- 码:-

Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
int flag=1;

int a=sc.nextInt();
for (int i = 0; i <a; i++) {

    System.out.println("enter a string");
    String s=sc.next();
    char [] sq=s.toCharArray();
    for (int j = 0; j < sq.length-1; j++) {
        if(sq[j]==sq[j+1]) {
            flag=0;
            break;
        }
    }

    if(flag==1) {
        System.out.println("diff");
    } else {
        System.out.println("same");
    }

}

you were not resetting your flag to the initial value between each run. 您没有在每次运行之间将标志重置为初始值。 Notice that I have changed your flag to a Boolean value rather then an integer and it now resets just before the System.out.println(); 注意,我已经将标志更改为布尔值而不是整数,并且现在在System.out.println()之前重置它。

package test;

import java.util.Scanner;

public class test {

public static void main(String[] args) {

    Scanner sc=new Scanner(System.in);
    System.out.println("enter a number");
    boolean flag=true;

    int a=sc.nextInt();
    for (int i = 0; i <a; i++) {

        flag=true;
        System.out.println("enter a string");
        String s=sc.next();
        char [] sq=s.toCharArray();

        for (int j = 0; j < sq.length-1; j++) 
        {

            if(sq[j]==sq[j+1])
            {
                flag=false;
                break;
            }
        }

        if(flag)
        {
            System.out.println("diff");
        }
        else
        {
            System.out.println("same");
        }

    }

}

} }

Just use Regex: short and simpler 只需使用正则表达式即可:更短,更简单

public static void main(String[] args) {
    String s = "dess";
    boolean match = s.matches(".*([A-Za-z])\\1+.*");

    if (match)
        System.out.println("same");
    else
        System.out.println("diff");
}

The problem with you code is that your never reset your flag. 您的代码的问题是您永远不会重置标志。 If you have a 'same' case then you flag will stay at 0 for the rest of the excution time of the program. 如果情况相同,那么在程序的其余执行时间中,标记将保持为0。 And therefore all cases after the first 'same' will also yield 'same'. 因此,在第一个“相同”之后的所有情况也将产生“相同”。 To solve this simple move your flag = 0 code to inside the first while loop. 为了解决这个简单的问题,请将您的flag = 0代码移动到第一个while循环内。 Like somewhere here: 就像这里的某个地方:

for (int i = 0; i <a; i++) {
   flag = 1;//<-- reset your flag variable
   System.out.println("enter a string");
   String s=sc.next();
   char [] sq=s.toCharArray();
   for (int j = 0; j < sq.length-1; j++) 
   {
       etc....

That should solve your problem. 那应该解决您的问题。 I hope this helps :) 我希望这有帮助 :)

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

相关问题 创建一个N x N的字母网格,以便没有两个相邻的字母相同 - Creating an N by N grid of letters so that no two adjacent letters are the same 如何在JAVA的差异原因上为相同的NumberFormatException打印差异消息? - How to print diff Msg for same NumberFormatException on diff cause in JAVA? 如何随机打印 10 个带有“n”个字母的字符串,但每个字符串都有相同的第一个字母 - How would one be able to randomly print 10 strings with 'n' letters, but each string has the same first letter 如果字符串中的所有字母都相同,则尝试返回 true - Trying to return true if all the letters in a string are the same 如何检查字符串中的相同单词? - How to check for the same word in a string? 通过比较单词来比较字符串,只检查单词中相同数量的字母 - Compare strings by comparing words checking only the same number of letters in the word 给定一个 A 字符串,如果是回文则打印 Yes,否则打印 No - Given a A string , print Yes if it is a palindrome, print No otherwise 检查字符串是否与另一个字符串构成的字母相同 - Check if a string is build out of the same letters as another string 如何检查两个字符串是否具有相同的字母,但只打印一次常见的字母? - How can I check if two strings have the same letters, but only print the common letters once? 相同的字母 - Letters having the same equivalence
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM