3道简单的Java笔试题,感兴趣的Java人士可以试一下!

匿名网友 匿名网友 发布于: 2015-08-30 00:00:00
阅读 136 收藏 0 点赞 0 评论 0

1、题目:现在有45个NPC,有3层,每层5个关卡,每个关卡3个NPC,如果得知NPC的ID如何得到他是第几层,第几关的(妹的,这串代码让哥哥头疼了半天,终于想出来了!我只想骂你妹的)
int a = 20
int c = a % 15 == 0 ? a / 15 : a / 15 + 1; //层
int q = (a - (c - 1) * 15) % 3 == 0 ? (a - (c - 1) * 15) / 3 : (a - (c - 1) * 15) / 3 + 1; //关卡
System.out.println(c);
System.out.println(q);

2、将数列1,2,3,…,n 依次进栈,出栈的第一个数为n,问出栈的第i(1<=i<=n)个数是什么?(简单的面试题,有意思么呵呵)
定义一个数组N,他的大小为n。 那么出栈的第i个数字为 N[n-i]


3、统计项目中一共多少行代码
package xxxx;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class CodeCounter {
/** 普通行数 */
private long normalLines = 0;
/** 注释行数 */
private long commentLines = 0;
/** 空白行数 */
private long spaceLines = 0;
/** 总行数 */
private long totalLines = 0;
private static long a = 0;
/***
* 通过java文件路径构造该对象
* 
* @param filePath
* java文件路径
*/
public CodeCounter(String filePath) {
tree(filePath);
}
/**
* 处理文件的方法
* 
* @param filePath
* 文件路径
*/
private void tree(String filePath) {
File file = new File(filePath);
File[] childs = file.listFiles();
if (childs == null) {
parse(file);
} else {
for (int i = 0; i < childs.length; i++) {
System.out.println("path:" + childs.getPath());
if (childs.isDirectory()) {
tree(childs.getPath());
} else {
childs.getName().matches(".*\.java$");
System.out.println("当前" + childs.getName() + "代码行数:");
parse(childs);
getCodeCounter();
}
}
}
}
/**
* 解析文件
* 
* @param file
* 文件对象
*/
private void parse(File file) {
BufferedReader br = null;
boolean comment = false;
try {
br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
line = line.trim();// 去除空格
if (line.matches("^[\s&&[^\n]]*$")) {
spaceLines++;
} else if ((line.startsWith("/*")) && !line.endsWith("*/")) {
commentLines++;
comment = true;
} else if (true == comment) {
commentLines++;
if (line.endsWith("*/")) {
comment = false;
}
} else if (line.startsWith("//")) {
commentLines++;
} else {
normalLines++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 得到Java文件的代码行数
*/
private void getCodeCounter() {
totalLines = normalLines + spaceLines + commentLines;
a += totalLines;
System.out.println("普通代码行数:" + normalLines);
System.out.println("空白代码行数:" + spaceLines);
System.out.println("注释代码行数:" + commentLines);
System.out.println("代码总行数:" + totalLines);
normalLines = 0;
spaceLines = 0;
commentLines = 0;
totalLines = 0;
}
public static void main(String args[]) {
CodeCounter counter = new CodeCounter(
"D:\workSpace\XXX\src");
}
}

评论列表
文章目录