1

我有一个对象数组,每个对象都包含一个 char 值和一个 int 值。我正在尝试编写一个程序来检查单独的 char 数组中的任何项目是否与对象数组的任何 char 成员匹配。如果是,则将 int 值添加到 sum。例如:

如果此数组中 index[i] 处的成员

char[] array = { 'a', 'r', 't', 'd', 's', 'f' }; 

匹配此对象数组的任何 char 成员(此处字母 'r' 匹配)

RomanToArabicNums [] table = {
           new RomanToArabicNums('h', 1000),
           new RomanToArabicNums('r', 200)    
       };

然后将该对象数组的 int 值(在本例中为 200)添加到int sum

这是来自RomanToArabic() 的完整代码(问题方法是):

package romantoarabicnums;    
import javax.swing.JOptionPane;    

public class RomanToArabicNums {
    private static String userString;
    private static int userNumber;
    private static char letter;
    private static int letterNum;

    public RomanToArabicNums (String s) throws NumberFormatException {
        for (int i = 0; i < s.length(); i++) {

           if (s.charAt(i) == 'M' ||s.charAt(i) == 'M' ||s.charAt(i) == 'C' ||s.charAt(i) == 'D' ||
               s.charAt(i) == 'X' ||s.charAt(i) == 'L' ||s.charAt(i) == 'V' ||s.charAt(i) == 'I' ) {
               userString += s.charAt(i);
           }
           else {throw new NumberFormatException("Only M,C,D,X,V and I allowed");}
        }
    }
    public RomanToArabicNums (int num) throws NullPointerException {
       if (num >= 1 && num <= 3999) {
           RomanToArabicNums.userNumber = num;
       }
       else {throw new NumberFormatException("numbers between 1 and 3999 only!");}
    }
    public RomanToArabicNums (char letter, int num) {
        this.letter = letter;
        this.letterNum = num;
    }
    public  char getLetter () {return letter;}
    public  int getLetterNum () {return letterNum;} 
    public static void main(String[] args) {
        //RomanToArabicNums r1 = new RomanToArabicNums(4080);
        RomanToArabicNums r2 = new RomanToArabicNums("MCMXCV");
        //System.out.println("the roman representation is: " + r1.fromArabicToRoman());
        System.out.println("the arabic number is: " + r2.fromRomanToArabic("MCMXCV"));

    }
    /* convert Roman characters to Arabic numbers */
    public int fromRomanToArabic (String userString) {
        int sum=0;

        //char [] charArray = userString.toCharArray();
       RomanToArabicNums [] table = new RomanToArabicNums[7];
           table [0] = new RomanToArabicNums('M', 1000);
           table [1] = new RomanToArabicNums('D', 500);
           table [2] = new RomanToArabicNums('C', 100);
           table [3] = new RomanToArabicNums('L', 50);
           table [4] = new RomanToArabicNums('X', 10);
           table [5] = new RomanToArabicNums('V', 5);
           table [6] = new RomanToArabicNums('I', 1);

           for (RomanToArabicNums obj : table) {

               for (int i = 0; i < userString.length(); i++) {
                   if (userString.indexOf(i) == table[i].getLetter()) {

                   if (table[i].getLetter() < table[i+1].getLetter()) {
                       sum += table[i+1].getLetterNum() - table[n].getLetterNum();
                   }

                   sum += table[i].getLetterNum();
                   }
              }    
          }
       return sum;
    }
    /* converr Arabic number to Roman characters */
    public static String fromArabicToRoman () {
        String roman = "";
        int  num = userNumber;
        while (num >= 1000) {roman += "M"; num -= 1000;}
        while (num >= 900) {roman += "CM"; num -= 900;}
        while (num >= 500) {roman += "D"; num -= 500;}
        while (num >= 400) {roman += "CD"; num -= 400;}
        while (num >= 100) {roman += "C"; num -= 100;}
        while (num >= 90) {roman += "XC"; num -= 90;}
        while (num >= 50) {roman += "L"; num -= 50;}
        while (num >= 40) {roman += "XL"; num -= 40;}
        while (num >= 10) {roman += "X"; num -= 10;}
        while (num >= 9) {roman += "IX"; num -= 9;}
        while (num >= 5) {roman += "V"; num -= 5;}
        while (num >= 4) {roman += "IV"; num -= 4;}
        while (num >= 1) {roman += "I"; num -= 1;}

        return roman;
    }        
}
4

2 回答 2

0
  1. 将您的搜索数组元素放在一个集合中
  2. 迭代每个 RomanToArabicNums 数组
  3. 在每个 RomanToArabicNums 的每个元素上,使用 contains 方法检查当前字符是否在 searchSet 中
于 2016-07-26T10:23:50.583 回答
0

没有循环的两种方法如下,

试试这个,使用 String 提供方法的事实contains()

if ((new String(charArray).contains("q"))) {
    // do something
}

还有另一种选择,这次使用indexOf()

if (new String(charArray).indexOf('q') > -1) {
    // do something enter code here`
}
于 2016-07-26T10:32:08.847 回答