急!!关于JAVA的simple pattern-matching

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

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

}

//i've worked it out see as follows

import java.util.Scanner;
import java.util.regex.Pattern;

public class PatternTest {

static int am = 0;
static int bm = 0;
static int cm = 0;
static Scanner san = new Scanner(System.in);
static String sp = "[^bc]*a*[^c]*b*c*[^ab]*";

// am + bm == cm
public static boolean matchesPattern1(String input){
am = getCharNum('a', input);
bm = getCharNum('b', input);
cm = getCharNum('c', input);
return (am + bm) == cm;

}

//
public static boolean matchesPattern2(String input){
if(! matchesPattern1(input)) {
return false;
} else {
return Pattern.matches(sp, input);

}

}

public static boolean matchesPattern3(String input){
matchesPattern1(input);
if(am != cm) {
return false;
} else {
return Pattern.matches(sp, input);
}
}

public static void testAll(String input) {
System.out.println("Input: " + input);
System.out.println("matches Pattern1? : " + PatternTest.matchesPattern1(input));
System.out.println("matches Pattern2? : " + PatternTest.matchesPattern2(input));
System.out.println("matches Pattern3? : " + PatternTest.matchesPattern3(input));
}

public static void main(String[] args) {
for(int i = 0;i < 5; i ++) {
System.out.print("Input String " + (i + 1) + " :");
String input = san.next();
PatternTest.testAll(input);
}
}

public static int getCharNum(char c, String input){
int num = 0;
char [] chs = input.toCharArray();
for(char ct: chs) {
if(ct == c){
num ++;
}
}
return num;
}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-03-21
其实楼主的题三种方法是大同小异的 只是顺序和个数的问题 不过我刚学习Java 很多东西不清楚 所以帮不了忙(用c的话就好写了)
回答的人少估计因为是英文的关系 那我就帮着简单译下不到位的地方望指出 希望有高手回答 我也学习学习!
写3种方法 都要输入字符串 看是否符合要求 并返回Boolean值(FALSE,TRUE)
名字和要求分别为
matchesPattern1() //格式:anbmc(n + m) (n ≥ 0 m ≥ 0) (即c //的个数等于ab的个数和)
// 其中m和n可能相等 a b c的顺序没有要求
matchesPattern2() //格式:anbmc(n + m) (n ≥ 0 m ≥ 0) (即c的个//数等于ab的个数和)
// 但是这次做了顺序的要求(必须abc依次出现)问可不可以用一个或多
//个布尔变量“flags”测试这个顺序 仍然有mn可能相等
matchesPattern3() //格式:anb*cn (n > 0. (n,*都是次方))*的意思就//是任意个
//仍然有顺序的要求(必须是abc依次出现)

最后的要求:写main方法使输入5个字符串(一次一个)程序要可以比较并输出你的串匹配哪个模式
第2个回答  2009-03-21
e文要很好啊,啃的头痛,先做个标记先,有空来看看吧

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网