首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的密码怎么了?它不会打印我认为我的代码在做什么。这是来自加拿大计算机竞赛的问题-2022 J3竖琴调校

我的密码怎么了?它不会打印我认为我的代码在做什么。这是来自加拿大计算机竞赛的问题-2022 J3竖琴调校
EN

Stack Overflow用户
提问于 2022-11-06 19:41:38
回答 1查看 48关注 0票数 -1

样本输入1

AFB+8HC-4

样本输入的输出1

AFB收紧8

HC松4

对样本输出1的解释

输入包含两个调优指令: AFB+8和HC-4。

样本输入2

AFB+8SC-4H-2GDPE+9

样本输入的输出2

AFB收紧8

SC松4

H松开2

GDPE收紧9

对样本输出2的解释

输入包含四个调优指令: AFB+8、SC-4、H-2和GDPE+9。

我试过使用Array和String,不起作用。FYI我不允许在我的代码中使用复杂的东西,就像我的老师说的那样(是的,如果我这样做了,我会得到分数的)

我的代码:

代码语言:javascript
复制
import java.util.Scanner;
public class Main 
{
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        String[] input = {in.nextLine()};
        String num = "1234567890";
        for (int i = 0 ;i < input.length;i++)
        {
            if (num.contains(String.valueOf(input.length)))
            {
                input[i]=input[i]+"\n";
                //continue;
            }//end of if
            if (input[i]=="+")
            {
                input[i]=" tighten ";
            }//end of if
            if (input[i]=="-")
            {
                input[i]=" loosen ";
            }//end of if
        }// end of for
        printArry(input);
   }// end of main

    public static void printArry(String[]a)
    {
           for (int j = 0; j < a.length; j++) 
           {
              System.out.print(a[j]);
           }// end of for
    }// end of printArry
}//end of class
EN

回答 1

Stack Overflow用户

发布于 2022-11-06 19:58:52

这个模式似乎是非数字的--后面是正负--然后是数字。我确实会使用Pattern来表示这个正则表达式。然后循环遍历匹配的模式。可能看上去像

代码语言:javascript
复制
String[] inputs = { "AFB+8HC-4", "AFB+8SC-4H-2GDPE+9" };
Pattern p = Pattern.compile("(\\D+)([+-])(\\d+)");
for (String s : inputs) {
    System.out.printf("For %s%n", s);
    Matcher m = p.matcher(s);
    while (m.find()) {
        System.out.printf("%s %s %s%n",
                m.group(1),
                m.group(2).equals("+") ? "tighten" : "loosen",
                m.group(3));
    }
}

产出(按要求)

代码语言:javascript
复制
For AFB+8HC-4
AFB tighten 8
HC loosen 4
For AFB+8SC-4H-2GDPE+9
AFB tighten 8
SC loosen 4
H loosen 2
GDPE tighten 9
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74339170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档