java matching_急!!关于JAVA的simple pattern-matching

急用```麻烦帮我看下怎么写最合适```1.WriteamethodnamedmatchesPattern1()thatacceptsaStringasinput.ThismethoddetermineswhetherornottheinputStringhasthefollowingproperty:Thes...

急用```麻烦帮我看下怎么写最合适```

1.Write a method named matchesPattern1() that accepts a String as input. This method determines whether or not the input String has the following property:

The string has a number of c's equal to the total number of a's and b's.

In other words, every word in this language is of the form:

anbmc(n + m), where n ≥ 0 and m ≥ 0. (n,m,(n+m)都是次方)

n and m may be equal, but this is not required.

Thus, bc, abcc, abccac, aabbcccc and abbbbccccc are all valid words in this language.

Your method should return a boolean value: true if the input string satisfies these requirements, and false if it does not.

For example, given a String such as aabbcc, your method should return false.

2.Write a method named matchesPattern2() that accepts a String as input. This method determines whether or not the input String has the following properties:

The string has a number of c's equal to the total number of a's and b's.

All a's come before any b's, and all b's come before any c's. (HINT: Can you test for this using one or more boolean variable "flags"?)

In other words, every word in this language is of the form:

anbmc(n + m), where n ≥ 0 and m ≥ 0. (n,m,(n+m)都是次方)

n and m may be equal, but this is not required.

Thus, aabbcccc and abbbbccccc are valid words in this language, but abaccc and abc are not.

Your method should return a boolean value: true if the input string satisfies these requirements, and false if it does not.

For example, given a String such as aabbcc, your method should return false.

This pattern is identical to the one above, except it imposes a specific ordering requirement on the input string.

3.Write a method named matchesPattern3() that accepts a String as input. This method determines whether or not the input String has the following properties:

The string has an equal (nonzero) number of a's and c's, and any number of (zero or more) b's.

All a's come before any b's, and all b's come before any c's.

In other words, every word in this language is of the form:

anb*cn, where n > 0. (n,*都是次方)

Thus, aabbcc and ac are valid words in this language, but abaccc and aabc are not.

Your method should return a boolean value: true if the input string satisfies these requirements, and false if it does not.

For example, given a String such as aabbaa, your method should return false.

4.Fill in the body of your main() method so that it prompts the user to enter 5 input strings, one at a time. For each input string, your program should compare it to each pattern, and print out whether the string matches each pattern.

用来写的源文件是

// Matcher.java

import java.util.*;

public class Matcher

{

public static void main (String [] args)

{

Scanner sc = new Scanner(System.in);

// The statement/loop that calls matchesPattern1(),matchesPattern(2),

// and matchesPattern3() goes in this method

}

// Define your pattern-matching methods here. Be sure to make them static!

//

// Each method should have a header with the following form:

//

// public static boolean matchesPatternX (String input)

//

// where 'X' is replaced by 1, 2, or 3.

// You may define any extra (static) helper methods that you want

}

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值