s实验2
2.
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String str = scan.nextLine();
String[] str1=str.split(" ");
int[] num=new int[str1.length];
for(int i=0;i<str1.length;i++){
String s=str1[i];
int k=0;
for(int j=0;j<str1.length;j++){
if(s.equalsIgnoreCase(str1[j])){
k++;
}
}
num[i]=k;
}
for(int i=0;i<str1.length;i++){
for(int j=0;j<str1.length && str1[j]!="-1";j++){
if(str1[i].equalsIgnoreCase(str1[j]) && i!=j){
str1[j]="-1";
}
}
}
for(int i=0;i<str1.length;i++){
if(str1[i].equals("-1")){
num[i]=-1;
}
}
for(int i=0;i<str1.length;i++){
if(!(str1[i].equals("-1"))){
System.out.println(str1[i]+":"+num[i]);
}
}
}
}
}
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String str = scan.nextLine();
String[] str1=str.split(" ");
int[] num=new int[str1.length];
for(int i=0;i<str1.length;i++){
String s=str1[i];
int k=0;
for(int j=0;j<str1.length;j++){
if(s.equalsIgnoreCase(str1[j])){
k++;
}
}
num[i]=k;
}
for(int i=0;i<str1.length;i++){
for(int j=0;j<str1.length && str1[j]!="-1";j++){
if(str1[i].equalsIgnoreCase(str1[j]) && i!=j){
str1[j]="-1";
}
}
}
for(int i=0;i<str1.length;i++){
if(str1[i].equals("-1")){
num[i]=-1;
}
}
for(int i=0;i<str1.length;i++){
if(!(str1[i].equals("-1"))){
System.out.println(str1[i]+":"+num[i]);
}
}
}
}
}
单元测试
public class CountDemo{
public void demoCount() throws Exception{
String str=” Hello World My First Unit Test”;
Count count(str);
, }
}
3.
3、public class aa {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "how are you";
String strs[] = str.split(" ");
StringBuffer result = new StringBuffer();
for(int i = strs.length-1; i >= 0; i--){
System.out.println(strs[i]);
result .append(" "+strs[i]);
}
System.out.println("倒序:"+result );
}
}