java编程求修改~~
importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importj...
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = new File(""/** 文件名 */
);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String sb = "";
int count = 0;
sb = br.readLine();
while (sb != null) {
String[] array = sb.split(" "/** 文件分隔符 */
);
for (String string : array) {
if (string.equals("while")) {
count++;
}
}
sb = br.readLine();
}
System.out.println(count);
}
}
这个程序我只能记while的个数,如果想增加记int,void,for这些个数该怎么修改?? 展开
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = new File(""/** 文件名 */
);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String sb = "";
int count = 0;
sb = br.readLine();
while (sb != null) {
String[] array = sb.split(" "/** 文件分隔符 */
);
for (String string : array) {
if (string.equals("while")) {
count++;
}
}
sb = br.readLine();
}
System.out.println(count);
}
}
这个程序我只能记while的个数,如果想增加记int,void,for这些个数该怎么修改?? 展开
1个回答
展开全部
加几个变量countwhile,countint,countvoid,countfor
将计数循环改为
for (String string : array) {
if (string.equals("while")) {
countwhile++;
}
if (string.equals("int")) {
countint++;
}
if (string.equals("void")) {
countvoid++;
}
if (string.equals("for")) {
countfor++;
} }
将计数循环改为
for (String string : array) {
if (string.equals("while")) {
countwhile++;
}
if (string.equals("int")) {
countint++;
}
if (string.equals("void")) {
countvoid++;
}
if (string.equals("for")) {
countfor++;
} }
追问
如果我想用数组来记录个数,比如a[0]=countwhile,a[1]=coutint,该怎么添加?如何输出!!!
追答
将count定义为数组即可
int[] cout=new int[4];
将计数循环改为
for (String string : array) {
if (string.equals("while")) {
count[0]++;
}
if (string.equals("int")) {
count[1]++;
}
if (string.equals("void")) {
count[2]++;
}
if (string.equals("for")) {
count[3]++;
} }
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询