java编程单词大全(java 常用单词)
java编程
数组长度:int n;用输入控制数组的元素个数
设置数组: String [] a = new String[n];
for循环输入数组的值
统计时将数组的值拼接用String接收
for循环截取比较,再设置一个计数器
然后下面的就可以用String的substring(0,4)去截取与重要单词比较
java编程:我是第几个单词
private?static?void?get(String?sentence,String?word){
StringBuilder?builder?=?new?StringBuilder();
try?{
Pattern?pattern?=?Pattern.compile(word);
Matcher?matcher?=?pattern.matcher(sentence);
if(matcher.find()){
String?[]?values?=?sentence.split("?");
for?(int?i?=?0;?i??values.length;?i++)?{
if(values[i].equalsIgnoreCase(word)){
builder.append("\r\n位置:"+(i+1));
}
}
System.out.println(builder.toString());
}else{
System.out.println("字符总数:"+sentence.length());
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
JAVA 编程
public?static?void?main(String[]?args)?{
System.out.println("请输入英文句子:");
//?读取键盘输入的一行(以回车换行为结束输入)
Scanner?in?=?new?Scanner(System.in);
String?readLine?=?in.nextLine();
//?处理标点符号
readLine?=?readLine.replaceAll("'",?"?");
readLine?=?readLine.replaceAll(",",?"?");
readLine?=?readLine.replaceAll("\\.",?"?");
//?按空格分词
String[]?words?=?readLine.split("?");
//?只记录最多的词
int?max?=?0;
String?maxWord?=?null;
MapString,?Integer?result?=?new?HashMapString,?Integer();
for?(String?word?:?words)?{
if?(word?!=?null??word.length()??0)?{
int?i?=?result.get(word)?==?null???0?:?result.get(word);
result.put(word,?++i);
if?(i??max)?{
max?=?i;
maxWord?=?word;
}
}
}
//?输出最多的单词和次数
System.out.println(maxWord?+?":"?+?max);
//?输出所有单词和次数
//?System.out.println("----all?word:----");
//?for?(Map.EntryString,?Integer?entry?:?result.entrySet())?{
//?System.out.println(entry.getKey()?+?":"?+?entry.getValue());
//?}
}
JAVA 编程题不会,求教!
package cn.charise.day;
import java.util.Scanner;
class StringTest
{
public static void main(String[] args)
{
String [] str1 ={"watch","match","noise","risky","stock" };
String str2="" ;
System.out.println("请输入数字:");
Scanner scan = new Scanner(System.in);
str2 = scan.next();
char[] ch = str2.toCharArray();
char[][] cha =new char[6][5];
for(int i =0;ich.length;i++){
switch(ch[i]){
case '2':cha[i]=new char[]{'a','b','c'};break;
case '3':cha[i]=new char[]{'d','e','f'};break;
case '4':cha[i]=new char[]{'g','h','i'};break;
case '5':cha[i]=new char[]{'j','k','l'};break;
case '6':cha[i]=new char[]{'m','n','o'};break;
case '7':cha[i]=new char[]{'p','q','r'};break;
case '8':cha[i]=new char[]{'s','t','u'};break;
case '9':cha[i]=new char[]{'y','w','x'};break;
}
}
int r=0;
String [] result =new String[3*3*3*3*3];
for(int i=0;i3;i++){
result[r]+=cha[0][i];
for(int j=0;j3;j++){
result[r]+=cha[0][j];
for(int k=0;k3;k++){
result[r]+=cha[0][k];
for(int m=0;m3;m++){
result[r]+=cha[0][m];
for(int n=0;n3;n++){
result[r]+=cha[0][n];
r++;
}
}
}
}
}
boolean flag = false;
int i;
for( i=0;iresult.length;i++){
for(int j=0;jstr1.length;j++){
if(result[i].equals(str1[j])){
flag=true;
break;
}
}
}
if(flag)
System.out.println("找到了,这个单词是:"+result[i]);
else
System.out.println("没能找到这个单词");
scan.close();
}
}
提示:该java代码只能输入5个2-9的数字,如果输入不规范可能会出现异常!
Java编程:输入一段英文:1、统计一共有多少个单词;2、每个单词出现的次数;3、按出现次数升或降序排列
自己去调整,随便写的;
package com.baidu.com.java;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MatchWorld {
public static void main(String[] args) {
String str = "Is there anyone who hasn't suffered for the secret love? We alwaysthink that"
+ " love is very heavy, heavy and could be the heaviest thing inthe world. "
+ "But one day, when you look back, you suddenly realize thatit's always light, "
+ "light. We all thought love was very deep, but infact it's very thin. " + "The deepest and heaviest love must grow up withthe time";
// 全部转换成大写
str = str.toUpperCase();
//或许你可以考虑用空格分割
String[] rang = str.split("\\b");
// 自己去调整吧,如果要得到精确的
System.out.println("单词总数大概为:" + rang.length);
// 用来记录的单词
MapString, Integer map = new HashMapString, Integer();
int countSpace = 0;
//统计各个字符出现的次数
for (String s : rang) {
if (s.trim().length() 0) {
s = s.trim();
if (!map.containsKey(s)) {
int count = str.split("\\b" + s.trim() + "\\b").length;
map.put(s, count);
}
} else {
map.put("空格", countSpace++);
}
}
//单词出现次数
for (String key : map.keySet()) {
System.out.println(key + "出现:" + map.get(key) + "次");
}
//把元素添加到list
ListString list = new ArrayListString();
list.addAll(map.keySet());
// 排序前
System.out.println("\n排序前:\n");
for (String s : list) {
System.out.println(s+"出现次数:"+map.get(s));
}
// 开始排序
System.out.println("\n按照出现次数降序排列(AESC):\n");
for (int i = 1; i list.size(); i++) {
for (int j = 0; j list.size() - i; j++) {
if (map.get(list.get(j)) map.get(list.get(j+1))) {
String tmp = list.get(j);
list.set(j, list.get(j+1));
list.set(j + 1, tmp);
}
}
}
// 排序后
System.out.println("\n排序后:\n");
for (String s : list) {
System.out.println(s+"出现次数:"+map.get(s));
}
}
}
JAVA编程
楼主的意思应该是计算一个句子中包含元音字母的单词所占比例吧
import java.io.*;
public class Demon
{
public static void main(String args[])
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str;
String[] word;//单词数组
int count;//包含元音字母的单词个数
do
{
count=0;
System.out.println("Please input the sentence:");
str=br.readLine();
word=str.split("\\s");//句子分割为单词
for(int i=0;iword.length;i++) //遍历单词数组,查找包含元音字母的单词
{
if(word[i].matches("[a-zA-Z]*[aeiouAEIOU][a-zA-Z]*"))
count++;
}
System.out.println("此句中出现包含元音字母的单词所占比例为:"+((double)count/(double)word.length)*100+"%");
System.out.println("Do you want to try another? (y/n)");
str = br.readLine();
}
while(str.equals("y"));
}
catch(IOException ex)
{
System.out.println("发生异常:"+ex.getMessage());
}
}
}