無論是哪種語言,都會存在這種問題,對超長數字的處理問題,對這種問題常常使用數組來處理,所以就有了這個程序
/**
* @(#)LongString.java * * 兩種方法實現java讀入超長數字 * @author CplusHua * @version 1.00 2013/4/24 */ import java.util.*; import java.io.*; public class LongString { public LongString() { } public static void main (String[] args) throws IOException { ReadReadTest(); ReadTest1(); } public static void ReadTest() throws IOException { BufferedReader buf; buf=new BufferedReader(new InputStreamReader(System.in)); String s=buf.readLine(); int m[]=new int[s.length()]; for(int i=0;i<s.length();i++){ m[i]=s.charAt(i)-48; } for(int i=0;i<s.length();i++){ System.out.print(m[i]); } System.out.println (); } public static void ReadTest1() throws IOException{ BufferedReader buf; buf=new BufferedReader(new InputStreamReader(System.in)); int m; int i=0; int ss[]=new int[1000]; while((m=buf.read())!='\n'){ ss[i++]=m-48; System.out.print(m-48); } System.out.println (); for(int j=0;j<i;j++){ System.out.print(ss[j]); } System.out.println (); } }