• 3137102432_施少兵_实验2


    第一题:

    package cn.shi.mju;

    import javax.swing.text.View;

    public class Main {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    View v = new View();
    }
    }
    package cn.shi.mju;
    //import java.util.*;

    public class Action {
    public void findWord(String str){//划分单词
    String[] arrayWord =str.split(" ");
    Action a = new Action();
    a.frequency(arrayWord);
    }
    public void frequency(String[] arrayWord){
    String[] word = new String[arrayWord.length];//存放遍历过的单词
    int time[]=new int[arrayWord.length];//存放记录单词出现次数
    boolean t = true;
    for(int i=0;i<arrayWord.length;i++){
    for(int j=0;j<arrayWord.length;j++){//遍历已遍历过的单词表
    if(arrayWord[i].equals(word[j])){
    System.out.println("单词重复!!!");
    t=false;//如果单词重复则跳过
    }
    }
    if(t==true){//单词初始次数为1
    word[i]=arrayWord[i];
    time[i]=1;
    for(int j=i+1;j<arrayWord.length;j++){//遍历字符串,记录次数
    if(arrayWord[i].equals(arrayWord[j])){
    time[i]++;
    }
    }
    }
    }
    for(int i = 0;i<arrayWord.length;i++){//遍历输出次数
    if(word[i]!=null){
    System.out.println("单词:"+word[i]+"出现了"+time[i]+"次。");
    }
    }
    }
    //以下是迭代器
    /*public void frequency(String[] arrayWord){
    Map<String,Integer> word = new HashMap<String,Integer>();
    boolean t = true;
    for(int i=0;i<arrayWord.length;i++){
    Iterator<String> it = word.keySet().iterator();
    while(it.hasNext()){
    String key =(String)it.next();
    if(key.equals(arrayWord[i])){
    t=false;
    }
    else{
    t=true;
    }
    }
    if(t==true){
    word.put(arrayWord[i],1);
    int x=1;
    for(int j=i+1;j<arrayWord.length;j++){
    if(arrayWord[i].equals(arrayWord[j])){
    x=x+1;
    word.remove(arrayWord[i]);
    word.put(arrayWord[i], x);
    }
    }
    }
    }
    Iterator<String> i = word.keySet().iterator();
    while(i.hasNext()){
    String key =(String)i.next();
    System.out.println("单词:"+key+"出现了"+word.get(key)+"次。");
    }

    }*/
    }
    package cn.shi.mju;
    import java.util.Scanner;

    public class View {
    public View(){
    Scanner input =new Scanner(System.in);
    System.out.println("请输入字符串,单词用空格隔开,回车结束:");
    String str=input.nextLine();
    Action a = new Action();
    a.findWord(str);
    }
    }

    第二题:
    import java.util.HashMap;
    import java.util.Map;
    public class test2_2 {
    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String a = "how are you";
    String[] b = a.split(" ");
    String c = "";
    for(int d=b.length;d>0;d--)
    {
    c=c+b[d-1]+" ";
    }
    System.out.println(c);
    }
    }

  • 相关阅读:
    php 判断字符串是否以某字符串结尾的方法
    且看新手如何快速新站优化(经验+求指导+找喷) 西安
    xml 序列化和反序列化 西安
    反射+xml 序列化 实际应用 西安
    使用自定义文件缓存提升ASP.NET项目性能 西安
    反射的简单应用(记录) 西安
    java web 中的 controller、service、persistence(mapper)、domain 分别是什么作用?
    nodesass 替换为 sass
    github 访问不了 访问 github,修改 hosts 文件方法
    win10 如何使用管理员权限编辑文件
  • 原文地址:https://www.cnblogs.com/shishaobing/p/5323070.html
Copyright © 2020-2023  润新知