代码测试

死线 发布于

测试一下下面几段代码:

Java支持示例,此程序是显示如何处理跳过换行与回车字符影响的猜字游戏程序chenneal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.io.*;

class test {
public static void main(String[] args)
throws java.io.IOException {
char ch, answer = 'K';
do {
do {
ch = (char)System.in.read();
} while(ch == '\r' | ch == '\n');
if(ch == answer) System.out.println("right answer");
else System.out.println("wrong answer");
} while (ch != answer);
}
}
C++支持示例,选自C++ Primer习题答案chenneal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string*> spvec; //读取vector 对象
string str;
cout << "Enter some strings(Ctrl+Z to end)" << endl;
while (cin >> str) {
string *pstr = new string; //指向string 对象的指针
*pstr = str;
spvec.push_back(pstr);
} //输出每个string 的内容及其相应的长度
vector<string*>::iterator iter = spvec.begin();
while (iter != spvec.end()) {
cout << **iter << (**iter).size() << endl;
iter++;
} //释放各个动态分配的string 对象
iter = spvec.begin();
while (iter != spvec.end()) {
delete *iter;
iter++;
}
return 0;
}

看来Hexo默认代码高亮插件表现还是相当不错的^_^。

题图

2015年7月新番《Charlotte》女主友利奈绪。