-4

我目前正在尝试编写一个内部有一个简单文本区域的“网络应用程序”,我希望在其中指出所写文本的字母。

例如,如果我写:

''你今年多大?我19岁了''

当我按下 HTML/CSS 页面上的按钮时,我需要一个代码来告诉我在这句话中使用了多少个“A”、“Y”和“D”(以及从 0 到 26 的所有字母)。

你能告诉我我必须在我的 .JS 文件中写入什么以及我应该在我的 .HTML 文件中写入什么,以便在写入内容时单击一个按钮来执行此操作吗?

我希望我的解释足够详细。谢谢!

编辑(我对我造成的问题感到非常抱歉) - 到目前为止我所做的看起来像这样:

HTML:

<link rel="stylesheet" type="text/css" href="theme.css">
<meta charset="utf-8"> 

<script src="test.js" type="text/javascript"></script>

<div class='header'>
Al.Fa.Be
</div>

<div class='yaz'>
<textarea></textarea>

</div>

<div class='description'>
<a href='http://www.google.com'>Ara</a>
</div>

<div class='description2'>
<input id="clickMe" type="button" value="Hesapla" onclick="doFunction();" />
</div>

CSS:

body{
background:white;
}

selection{
background:#CCC;
}

#clickMe{
background:#CCC;
border:1px solid #333;

}

.header{
font-size:70px;
font-weight:bold;
font-family:Arial;
color:#333;
margin-left:580px;
padding-top:200px;
}

textarea{
width:1210px;
height:40px;
color:black;
margin-top:20px;
margin-left:100px;
padding-left:10px;
padding-top:10px;
font-size:18px;
font-family:Arial;
}

.description{
background:#f2f2f2;
padding:6px;
width:50px;
text-align:center;
border:1px solid #ddd;
font-family:Arial;
margin-left:620px;
margin-top:20px;
font-size:14px;
}

.description a{
color:#555;
text-decoration:none;
}

.description2{
background:#f2f2f2;
padding:6px;
width:60px;
text-align:center;
border:1px solid #ddd;
font-family:Arial;
margin-left:750px;
margin-top:-30px;
font-size:14px;
}

.description2 a{
color:#555;
text-decoration:none;
}

.yaz{
color:white;
}

Javascript:

// Input name. Count number of alphabets a-z
class program                                                          
{
    public static void main(String[] args) 
    {
        String name = args[0];
        int count[] = new int[29];
        int i,p;
        int n = name.length();
        name = name.toUpperCase();
        char c;
        for (i=0; i<29; i++) 
        {
            count[i] = 0;   
        }
        for (i=0; i<n; i++) 
        {
            c = name.charAt(i);
            p = (int) c;
            count[p-65]++;
        }
        for (i=0; i<29 ; i++) 
        {
            if (count[i] >0) 
            {
                System.out.println((char)(i+65) + " occurs " + count[i] + " times");
            }
        }
    }
}

PS:请记住,我真的很讨厌这个,我需要知道如何以特定的方式做到这一点。

4

3 回答 3

1

This is how I would do it.

var count = {};
var msg = "Your message";

for(var i = 0; i < msg.length; i++) {
    var c = msg[i];
    c = c.toLowerCase();
    if(typeof(count[c]) == "undefined") {
        count[c]=1;
    }
    else {
        count[c]++;
    }
}

If you want, you can check for only letters like this:

if('a'.charCodeAt(0) <= c <= 'z'.charCodeAt(0)) 
    //This is a small letter
于 2013-11-09T16:06:21.600 回答
0

尝试这个:

var str = "How old are you? I am 19 years old";
str = str.toLowerCase();// search is case sensitive
var counta = str.match(/a/g);
var countd = str.match(/d/g);
var county = str.match(/y/g);
alert("a:"+counta.length+" d:"+countd.length+" y:"+county.length);

在这里提琴:

于 2013-11-09T15:58:56.433 回答
0

看看这篇文章: http ://davidwalsh.name/javascript-unique-letters-string 。这正是您要寻找的东西。

编辑:HTML:

<link rel="stylesheet" type="text/css" href="theme.css">
<meta charset="utf-8"> 

<script src="test.js" type="text/javascript"></script>

<textarea rows="5" cols="10" id="str" value="Insert the string" />
<input type="button" value="Submit" id="button" />

JS:

document.getElementById('button').onclick = function() {
/* returns the size/length of an object */
Object.size = function(obj) {
    var size = 0;
    for(key in obj) {
        if(obj.hasOwnProperty(key)) size++;
    }
    return size;
}

//initial vars
var str = document.getElementById('str').val;
var letters = new Object;

//loop, figure it out
for(x = 0, length = str.length; x < length; x++) {
    var l = str.charAt(x)
    letters[l] = (isNaN(letters[l]) ? 1 : letters[l] + 1);
}

//output count!
for(key in letters) {
    console.log(key + ' :: ' + letters[key]);
}
console.log(Object.size(letters));

}

PS:你的“javascript”看起来不像 javascript……它看起来像 java……

于 2013-11-09T15:52:42.207 回答