gpt4 book ai didi

java - 我想将一系列字符转换为字符串并与另一个字符串进行比较

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

示例输入 1:

Enter the first letter:R
Enter the second letter:A
Enter the third letter:I
Enter the fourth letter:N
Enter the fifth letter:B
Enter the sixth letter:O
Enter the seventh letter:W

示例输出 1:

RAINBOW

示例输入 2:

Enter the first letter:R
Enter the second letter:E
Enter the third letter:I
Enter the fourth letter:N
Enter the fifth letter:B
Enter the sixth letter:O
Enter the seventh letter:W

示例输出 2:

The spelling is wrong

代码:

public class spellcheck {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String str1;
String str2 = "rainbow";
int i = 0;
String a[] = new String[7];
System.out.print("Enter the first letter:");
a[0] = scan.nextLine();
System.out.print("Enter the first letter:");
a[1] = scan.nextLine();
System.out.print("Enter the first letter:");
a[2] = scan.nextLine();
System.out.print("Enter the first letter:");
a[3] = scan.nextLine();
System.out.print("Enter the first letter:");
a[4] = scan.nextLine();
System.out.print("Enter the first letter:");
a[5] = scan.nextLine();
System.out.print("Enter the first letter:");
a[6] = scan.nextLine();
}
}

下一步应该做什么?

最佳答案

String tmp = "";

for(int j = 0; j < a.length; j ++)
tmp += a[j];

System.out.println(tmp);

输出 -

Enter the first letter:r
Enter the first letter:a
Enter the first letter:i
Enter the first letter:n
Enter the first letter:b
Enter the first letter:o
Enter the first letter:w
rainbow

要进行比较,您可以使用内置的字符串函数 .equals -> tmp.equals("Otherstring")

关于java - 我想将一系列字符转换为字符串并与另一个字符串进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60660579/

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