|
本帖最后由 yangfanconan 于 2013-5-8 11:02 编辑
上课,这节课我们还是实验室课。对String类型进行实验。 - //这节课我们主要折腾一下String类型
- //有一个基本例子进行测试。
- //首先我们输入几个字符然后将这几个字符保存成一个String类型
- //在这个String类型中相同的字符出现的次数输出出来。
- //
- //
- //
- //
- //
- String strTemp="";
- char letter;
- String words = "Begin...";
- void setup() {
- size(640, 360);
- // 创建字体
- textFont(createFont("Georgia", 36));
- }
- void draw() {
- background(0); // 保存背景颜色
- // 居中绘出字符
- textSize(14);
- text("Click on the program, then type to add to the String", 50, 50);
- text("Current key: " + letter, 50, 70);
- text("The String is " + words.length() + " characters long", 50, 90);
-
- textSize(36);
- text(words, 50, 120, 540, 300);
- }
- void keyPressed() {
- // The variable "key" always contains the value
- // of the most recent key pressed.
- if ((key >= 'A' && key <= 'z') || key == ' ') {
- letter = key;
- words = words + key;
- strTemp= strTemp+key;
- // 输出这个字符到控台
- println(key);
- }
- if (key=='\n'){
- AiString();
- }
- }
- void AiString()
- {
- int num=0;
- for (int i=0;i<strTemp.length();i++){
-
- for (int j=0;j<strTemp.length();j++){
- if (strTemp.charAt(i)==strTemp.charAt(j))
- {
- num++;
- }
- }
- println(strTemp.charAt(i)+":num="+num);
- num=0;
- }
- }
复制代码
下课~
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|