Problem 1: to write an application, a number of the same characters in the string of biometric input, output and the statistical results.
Code
import java.util.*; public class Test { public static void main(String[] args) { System.out.println ( "Please enter the string:" ); Scanner r = new Scanner(System.in); String str1 = r.nextLine(); int count; for(int i=0;i<str1.length();i++) { count =0; if(str1.indexOf(str1.substring(i,i+1))==i){ for(int j=0;j<str1.length();j++){ if(str1.charAt(i)==str1.charAt(j)) count+=1; } System.out.println(str1.charAt(i)+":"+count+"次"); } } } }
operation result
Problem 2: programming, enter a string, the string is determined whether the letter string consisting of a palindrome.
Code
/**
isPaildrome () method of: determining whether the first character and the last character are equal, are equal, the comparison continues and the second character penultimate characters until all characters match or not been checked.
The main method to realize the input string, call isPaildrome () method.
*/
import java.util.*; public class t { public static void main(String[] args) { System.out.println ( "Please enter the string:" ); Scanner r = new Scanner(System.in); String str1 = r.nextLine(); if(isPaildrome(str1)) { System.out.println (str1 + "palindromic" ); } else{ System.out.println (str1 + "is not a palindrome" ); } public static boolean isPaildrome(String s){ int low=0; int high=s.length()-1; while(high>low){ if(s.charAt(low)!=s.charAt(high)) {return false;} else{low++; high--;} } return true; } }
operation result