Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

把BufferedReader那行拿到循环外面

Posted by frkstyc at 2006-04-08 21:04:55 on Problem 2000
In Reply To:Re:为什么总是RuntimeError的,会有什么原因导致这样的? Posted by:victor286 at 2006-04-08 17:49:42
我是new to java,不过我觉得是因为这样
你的stdin的带buffer的,new了之后就把输入都读走了,等下一次new的时候输入已经空了,于是stdin.readLine()返回null,parseInt就抛异常了

> import java.io.*;
> import java.util.*;
> public class Main
> {
> 
> 	public static void main(String args[])throws IOException
> 	{Vector v=new Vector();
> 	Vector k=new Vector();
>         
> 		while(true){
> 	
> 		BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
> 		String s=stdin.readLine();
> 		int n=Integer.parseInt(s);
> 		if(n==0)break;
> 		k.add(""+n);
> 		int suma=0;
> 		int i=1;
> 		while(true)
> 		{
> 			suma=suma+i;
> 			
> 		if(suma>n)break;
> 		i++;
> 	}
> 	int sumb=0;
> 	for(int j=1;j<=i;j++)
> 	{
> 		sumb=sumb+j*j;
> 		
> 		}
> 		
> 		
> 	v.add(""+(sumb-(suma-n)*i));
> }
> for(int c=0;c<v.size();c++)
> {
> 	String s1=(String)k.get(c);
> 	String s2=(String)v.get(c);
> 	int a=Integer.parseInt(s1);
> 	int b=Integer.parseInt(s2);
> 	System.out.println(a+" "+b);
> }
> }
> }

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator