在两个奇数之间插入破折号 [英] Inserting dashes between two odd numbers

查看:151
本文介绍了在两个奇数之间插入破折号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人。在我的程序中,我用一个 String 类型的用户输入,并在之间加上破折号两个奇数。例如:

folks. In my program I take a user input of numbers of a String type and put dashes between two odd numbers. For example:

Input = 99946    Output = 9-9-946
Input = 56730    Output = 567-30 

但在我的代码中,如果我,例如,写 9933444 然后我得到的输出是: 9-9-9-3-3-3-344444 。它正确地用短划线分隔奇数,但添加额外的数字。可能导致此错误的原因是什么?

But in my code, if I, for example, write 9933444 then the ouput that I'm getting is: 9-9-9-3-3-3-344444. It correctly separates the odd numbers by dashes but also adds extra numbers. What could be causing this bug ?

import java.util.Arrays;
import java.util.Scanner;

public class DashInsert {

    public static void main(String[] args)
    {
        Scanner kbd = new Scanner(System.in);
        System.out.println("Enter the numbers: ");
        String myString = kbd.nextLine();
        char[] numbers = myString.toCharArray();
        String result = "";

        for(int i = 1; i < numbers.length; i++)
        {
            int value1 = Character.getNumericValue(numbers[i]);
            int value2 = Character.getNumericValue(numbers[i-1]);


            if(value1 % 2 != 0 && value2 % 2 != 0)
            {
                result += numbers[i-1] + "-" + numbers[i] + "-";
            }
            else 
                result += numbers[i-1] + "" + numbers[i];
        }
        System.out.println(result);

    }

}


推荐答案

代码可以简化一点(以及解决双字符错误):

The code can be simplified a bit (as well as solve the "double char" bug):

String str = "9933444";
char[] numbers = str.toCharArray();
String result = "";

for(int i = 1; i < numbers.length; i++)
{
    int value1 = Character.getNumericValue(numbers[i-1]);
    int value2 = Character.getNumericValue(numbers[i]);
    result += value1;
    if(value1 % 2 != 0 && value2 % 2 != 0) {
        result += "-";
    }
}
result += numbers[numbers.length - 1];
System.out.println(result);

输出

9-9-3-3444

原因双字符错误是每个循环打印地点上的项目 i-1 i 。这意味着 i 将在下一个循环中再次打印(它将变为 i-1 )。

The reason for the "double char" bug is that every loop prints both the items on the places i-1 and i. which means that i will be printed again on the next loop (where it will become i-1).

如果你使用的是Java 8 - 你可以使用 Stream 做一些看起来更像你最初想做的事情:

In case you're using Java 8 - you can use a Stream do something that looks more like what you were originally trying to do:

public static void main(String[] args){
    String str = "9933444";
    List<String> lst = Arrays.asList(str.split(""));
    String res = lst.stream().reduce((a,b) -> {
        if (isOdd(a) && isOdd(b)) {
            return a + "-" + b;
        }
        else {
            return a + b;
        }
    }).get();
    System.out.println(res);
}

// grep the last digit from the string and check if it's odd/even
public static boolean isOdd(String x) {
    if (x.length() > 1) {
        if (x.substring(x.length()-1).equals("-")) {
            x = x.substring(x.length()-3, x.length()-2);
        }
        else {
            x = x.substring(x.length() - 1);
        }
    }
    return Integer.parseInt(x) % 2 == 1;
}

输出

9-9-3-3444

这篇关于在两个奇数之间插入破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆