2014-07-11 38 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