yangfanconan 发表于 2013-5-8 11:00:43

Processing之旅-----【4课String实验课】

本帖最后由 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;
}
}

下课~

学慧放弃 发表于 2013-5-8 15:30:24

不错!!!!坐沙发:lol:lol

Dark_Angel 发表于 2013-5-13 09:40:49

求教!
text()打印中文字符乱码……有解决办法没……版本2.0b8

yangfanconan 发表于 2013-5-13 19:06:36

Dark_Angel 发表于 2013-5-13 09:40 static/image/common/back.gif
求教!
text()打印中文字符乱码……有解决办法没……版本2.0b8

PFont myfont=createFont("宋体");

Dark_Angel 发表于 2013-5-17 15:46:16

yangfanconan 发表于 2013-5-13 19:06 static/image/common/back.gif
PFont myfont=createFont("宋体");

恩,问完之后已经发现了,只是字库问题。谢谢老大回答!!

cao363835918 发表于 2013-11-8 15:15:50

不错,很好很好
页: [1]
查看完整版本: Processing之旅-----【4课String实验课】