java小程序错误Exception in thread "main" java.lang.NullPointerException at sNum.main(sNum.java:14)
百度了好久也没弄出来,向大神求助importjava.io.BufferedReader;importjava.io.FileReader;publicclasssNum...
百度了好久也没弄出来,向大神求助
import java.io.BufferedReader;
import java.io.FileReader;
public class sNum {
public static void main(String[] args)throws Exception {
//读取文件
FileReader fr=new FileReader("C:\\ihaveadream.txt");
BufferedReader br=new BufferedReader(fr);
double snum=0,sum=0;
while(br.readLine()!=null){
String str=br.readLine();
//每隔随机数个字符长度读入字符并判断‘s’
for(int i=0;i<str.length();i+=Math.random()*10)
{
char c=str.charAt(i);
if(c=='s')
snum++;
sum++;
}
//当读取了1000个字符时跳出循环
if(sum>1000)
break;
}
//计算并输出字符's'百分比
double a=snum/sum*100;
System.out.println("The percent of the number of s is:"+ a +"%");
br.close();
}
} 展开
import java.io.BufferedReader;
import java.io.FileReader;
public class sNum {
public static void main(String[] args)throws Exception {
//读取文件
FileReader fr=new FileReader("C:\\ihaveadream.txt");
BufferedReader br=new BufferedReader(fr);
double snum=0,sum=0;
while(br.readLine()!=null){
String str=br.readLine();
//每隔随机数个字符长度读入字符并判断‘s’
for(int i=0;i<str.length();i+=Math.random()*10)
{
char c=str.charAt(i);
if(c=='s')
snum++;
sum++;
}
//当读取了1000个字符时跳出循环
if(sum>1000)
break;
}
//计算并输出字符's'百分比
double a=snum/sum*100;
System.out.println("The percent of the number of s is:"+ a +"%");
br.close();
}
} 展开
展开全部
你的程序我帮你改完了,你看看吧.
完整的程序如下:
import java.io.BufferedReader;
import java.io.FileReader;
public class sNum {
public static void main(String[] args)throws Exception {
//读取文件
FileReader fr=new FileReader("C:\\ihaveadream.txt");
BufferedReader br=new BufferedReader(fr);
int snum=0,sum=0;
String str="";
while((str=br.readLine())!=null){
//每隔随机数个字符长度读入字符并判断‘s’
for(int i=0;i<str.length();i+=(int)(Math.random()*10))
{
char c=str.charAt(i);
if(c=='s')
snum++;
sum++;
}
//当读取了1000个字符时跳出循环
if(sum>1000)
break;
}
//计算并输出字符's'百分比
double a=(double)snum/sum*100;
System.out.println("The percent of the number of s is:"+ a +"%");
br.close();
}
}
完整的程序如下:
import java.io.BufferedReader;
import java.io.FileReader;
public class sNum {
public static void main(String[] args)throws Exception {
//读取文件
FileReader fr=new FileReader("C:\\ihaveadream.txt");
BufferedReader br=new BufferedReader(fr);
int snum=0,sum=0;
String str="";
while((str=br.readLine())!=null){
//每隔随机数个字符长度读入字符并判断‘s’
for(int i=0;i<str.length();i+=(int)(Math.random()*10))
{
char c=str.charAt(i);
if(c=='s')
snum++;
sum++;
}
//当读取了1000个字符时跳出循环
if(sum>1000)
break;
}
//计算并输出字符's'百分比
double a=(double)snum/sum*100;
System.out.println("The percent of the number of s is:"+ a +"%");
br.close();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询