2014-07-11 35 views
-4

我是編程新手,剛剛開始執行uva問題。在問題11403中,無論我做什麼,我的提交結果始終是運行時錯誤。我不明白的地方是我的問題,或者爲什麼會導致errors.Can請人幫忙Uva中的運行時錯誤11403

/* 
* File Name: Main.java 
* --------------------------------------- 
* This program will take two binary number as input 
* then multiply those number and show the result and 
* also the procedure 
* 11 
* 11 
* --- 
* 11 
* 11x 
* ------ 
* 1001 
*/ 
import java.util.Scanner; 
class Main 
{                                                                          
    public static void main(String[] args) 
    { 

     Scanner input = new Scanner(System.in); 
    while(input.hasNext()) 
     { 
      int num1=input.nextInt(); 
      int num2=input.nextInt(); 
      if(num1==0 && num2==0) break; 
      String []answers=multiply(num1,num2);//will return a array with the mid answers and the main answer 
      int answerlen=answers[answers.length-1].length();//The last element of the array will contain the answer 
      printQuestion(num1,num2,answerlen);//will print the question 
      printAnswers(answers,answerlen); 
     } 
    } 
    /* 
    * Will multiply the given binary digits and return the answers in String array 
    * 
    * @param first the first binary number 
    * @param second the second binary number 
    * @returns a String array with the mid multiply answers and the main answer 
    */ 
    public static String [] multiply(int first, int second) 
    { 
     String temp=""+second;//to turn second number into string 
     String[] answers=new String[temp.length()+1];//creating a array with size +one then second number length 
     int [] midans=new int[temp.length()];//creating a array of size same as second number length 
     int ten=1; // a variable to hold tens power 
     for(int i=second,j=0; 0<i ; i=i/10,j++) 
     { 
      int section=i%10; // will section the second number like if 11 will make it 1 
      midans[j]=first*section; 
      if(midans[j]==0) 
      { 
       answers[j]=""; 
       for(int x=first;x>0;x=x/10) 
       { 
        answers[j]+=midans[j]; 
       } 
      } 
      else answers[j]=""+midans[j];//saving the middle answer 
      midans[j]*=ten;//multiplying the numbers with the power of ten so each level have same number of zero as its level number 
      ten*=10;//increasing the zero 
     } 
     String finalans="";//a temporary variable to save the answer 
     int carry=0;//to save the carry found by doing addition 
     for(int i=midans[midans.length-1]; 0<i;i=i/10)//a loop that will run as many as the last level number with zero 
     { 
      int temp1=0;//to help in addition 
      for(int j=second,k=0; 0<j;j=j/10,k++)//will run as many digit as second number have 
      { 
       int add=midans[k]%10;//getting the last digit of the mid answer 
       midans[k]=midans[k]/(10);//making it a digit shorter 
       if(k==0) 
       { 
        //if its the first time that carry will be added 
        temp1+=add+carry; 
        carry=0; 
       } 
       else 
       { 
        temp1+=add; 
       } 
       for(;temp1>=2;temp1-=2) 
       { 
        //if temp is greater or equal thn two then we count the carry 
        carry++; 
       }    
      } 
      if(i/10==0 && carry !=0) 
      { 
       //for the last carry which will be placed at first 
       finalans+=temp1; 
       finalans+=carry; 
      } 
      else 
      { 
      finalans+=temp1; 
      } 
     } 
     char [] tempans=finalans.toCharArray(); 
     answers[answers.length-1]=""; 
     /* 
     * to reverse the answer and save the real answer in the last 
     * index of answers array; 
     */ 
     for(int i=0 ;i<tempans.length;i++) 
     { 
      answers[answers.length-1]+=""+tempans[tempans.length-1-i]; 
     } 
     return answers; 
    } 
    /* 
    * will print the question 
    */ 
    public static void printQuestion(int one,int two,int length) 
    { 
     String first=""+one; 
     String second=""+two; 
     for(int j=0 ; j<(length-first.length()) ; j++) 
     { 
      System.out.print(" "); 
     } 
     System.out.println(one);  
     for(int j=0 ; j<(length-second.length()) ; j++) 
     { 
      System.out.print(" "); 
     } 
     System.out.println(second); 
     for(int j=0 ; j<(length-second.length()) ; j++) 
     { 
      System.out.print(" "); 
     } 
     for(int j=0 ; j<(second.length()) ; j++) 
     { 
      System.out.print("-"); 
     } 
     System.out.println(); 
    } 
    /* 
    * Printing answers 
    */ 
    public static void printAnswers(String [] answers ,int length) 
    { 
     for(int i=0 ;i<answers.length-1;i++) 
     { 
      for(int j=0 ; j<(length-answers[i].length()-i) ; j++) 
      { 
       System.out.print(" "); 
      } 
      System.out.println(answers[i]); 
     } 
     for(int i=0 ; i<answers[answers.length-1].length(); i++) 
     { 
      System.out.print("-"); 
     } 
     System.out.println(); 
     System.out.println(answers[answers.length-1]); 
    } 
} 
+2

發佈完整的異常堆棧跟蹤。 – Unihedron

+0

爲什麼不自己運行它並使用堆棧跟蹤來查找錯誤?沒有什麼比這更容易。 – MightyPork

+0

我的編譯器沒有給出任何異常。它的UVa在線評判告訴我,在我的程序中存在運行時錯誤 – Mujadded

回答

0

的UVA問題規格相當清楚地表明輸入的是二進制字符串,而不是整數值。這裏是你的代碼的主要問題:

  1. 「你可以假設每個字符串的長度是不超過30」

你輸入讀取nextInt()。當你試圖輸入1111111111111111111111111111111然後你會得到一個InputMismatchException,因爲int類型可以容納的最大值是2 147 483 647.

我建議使用BigInteger類而不是int。更多信息http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html