样本输入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我不允许在我的代码中使用复杂的东西,就像我的老师说的那样(是的,如果我这样做了,我会得到分数的)
我的代码:
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发布于 2022-11-06 19:58:52
这个模式似乎是非数字的--后面是正负--然后是数字。我确实会使用Pattern来表示这个正则表达式。然后循环遍历匹配的模式。可能看上去像
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));
}
}产出(按要求)
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 9https://stackoverflow.com/questions/74339170
复制相似问题