0
/*
* Compares user input and checks whether they are anagrams
*
*/
import java.util.Scanner;
public class Anagram
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter first sentence: ");
String s1 = sc.nextLine();
System.out.print("Enter second sentence: ");
String s2 = sc.nextLine();
s1.toLowerCase();
String new1 = "";
for(char ch = 'a'; ch <= 'z'; ch++){
int i;
for(i = 0; i <s1.length(); i++){
if(ch == s1.charAt(i)){
System.out.print(ch + " are the letters of " + s1 + " in order ");
break;
}
}
}
s2.toLowerCase();
String new2 = new String();
for(char ch2 = 'a'; ch2 <= 'z'; ch2++){
int i2;
for(i2 = 0; i2 <s2.length(); i2++){
if(ch2 == s2.charAt(i2)){
System.out.print(ch2 + " are the letters of " + s2 + " in order ");
break;
}
}
}
}
}
參考我以前的問題,這是做家庭作業的正確方法一切正常,除了我無法創建一個新的字符串和所有的字符傳遞到新的字符串。然而,這必須不使用stringbuffer或append()是可能的?如何追加而不使用緩衝區或追加方法?
不,它只是說創建一個新的空字符串(準備好存儲按字母順序排列的每個字母)如果字符的字母匹配,則附加到新的字符串,因爲我們還沒有了解陣列但我們不允許使用它 –
@ Ron_ish在這種情況下,按照建議使用'+'或'+ ='運算符。使用 – Thomas
但是它打印一個字符+在不同線路上的下一個等,並且不將其轉換爲字符串 –