java.lang.Error: count cannot be resolved to a variable

java.lang.Error: count cannot be resolved to a variable

//不知道哪儿有问题>_<

package com.imooc.speech;


import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;


//老师让程序员小马统计英文演讲稿speech.txt文件中共有多少个字符,并显示所有内容以便在演讲开始前有个准备,假如你是小马,请完成老师教给你的任务。



public class Speech {


public static void main(String[] args) {

FileInputStream speech;

try {

File file=new File("speech.txt");

speech = new FileInputStream(file);

int n=0; 

int count=0;

System.out.print("文本内容:");

while((n=speech.read())!=-1) {

System.out.print((char)n);

count++;

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

System.out.println("统计结果:"+ file.getName() + "文件中共有" + count + "个字节" );

}


}


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,将变量file与count定义在try语句块中,那么该变量仅在该语句块中起作用,其他位置想调用时,就会找不到,这里应将其放置在try{}外,修改如下:

http://img1.sycdn.imooc.com//climg/60e0385a0995f86e10120624.jpg

祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师