gpt4 book ai didi

java - 迭代对象数组并将其成员与其他数组进行匹配/比较

转载 作者:太空宇宙 更新时间:2023-11-04 12:23:38 25 4
gpt4 key购买 nike

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

如果该数组中索引[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

这里是完整的代码(问题方法是)fromRomanToArabic():

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;
}
}

最佳答案

  1. 将搜索数组元素放入集合中
  2. 迭代每个 RomanToArabicNums 数组
  3. 在每个 RomanToArabicNums 的每个元素上,使用 contains 方法检查当前字符是否在 searchSet 中

关于java - 迭代对象数组并将其成员与其他数组进行匹配/比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38586856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com