輸入一段字串,去除字串中重複的字元,並輸出

吉他社社長發表於2020-12-16
去除隨機生成字串中的重複字元,並將新的字串輸出。
package Day1216;

import java.util.Scanner;

public class HW4 {

	public static void main(String[] args) {
		// 去除隨機生成字串中的重複字元,並將新的字串輸出。

		Scanner sc = new Scanner(System.in);
		
		String str = sc.next();
		
		StringBuffer sb = new StringBuffer();
		
		for(int i = 0; i<str.length(); i++) {
			char c = str.charAt(i);
			
			if(str.indexOf(c) == i) {
				sb.append(c);
			}
		}
		System.out.println(sb);
	}

}

相關文章