该程序的目的是以大写字母输出/替换字符串的常用字母。 INPUT:它应该输出的计算机程序:cOMPuteR PROgRaM
public static void main(String[] args)
{
Scanner x =new Scanner( System.in);
String input="";
System.out.print("Input two words:");
input= x.nextLine();
String[] array = input.split(" ");
String stringa = array[0];
String stringb = array[1];
char[] first = stringa.toCharArray();
char[] second = stringb.toCharArray();
for (int i = 0, max = Math.min(first.length, second.length); i < max; i++) {
char c = first[i];
if (c == second[i])
{
c = Character.toUpperCase(c);
first[i] = c;
second[i] = c;
}
}
System.out.println(new String(first));
System.out.println(new String(second));
}
答案 0 :(得分:0)
String[] array = input.split(" ");
String stringa = array[0];
String stringb = array[1];
char[] a = stringa.toCharArray();
for(char c : a){
String cStr = String.valueOf(c);
int i = stringb.indexOf(cStr)
stringb[i] = cStr.toUpperCase();
}
答案 1 :(得分:0)
为每个字符串创建一个char数组(char[]
)。然后使用位置索引迭代字符。如果每个String中位置索引处的字符相同,则更新两者中的char。最后打印数组(String
以获得清晰易读的内容)。
public static void main(String[] args) {
char[] first = args[0].toCharArray();
char[] second = args[2].toCharArray();
for (int i = 0, max = Math.min(first.length(), second.length()); i < max; i++) {
char c = first[i];
if (c == second[i]) {
c = Character.toUpperCase(c);
first[i] = c;
second[i] = c;
}
}
System.out.println(new String(first));
System.out.println(new String(second));
}
在澄清您的问题之后,这里的代码应该有效:
public static void main (String[] args) {
args = new String[] {"godi","doge"};
char[] first = args[0].toCharArray();
char[] second = args[1].toCharArray();
for (int i = 0; i < first.length; i++) {
char c = first[i];
char upper = Character.toUpperCase(c);
boolean changed = false;
for (int j = 0; j < second.length; j++) {
if (second[j] == c) {
second[j] = upper;
changed = true;
}
}
if (changed) {
for (int j = 0; j < first.length; j++) {
if (first[j] == c) {
first[j] = upper;
}
}
}
}
System.out.println(new String(first));
System.out.println(new String(second));
}