脚本与其测试人员之间的交互不良

时间:2013-12-06 17:20:34

标签: java

所以我写了这个脚本:

public class Primes {
public static void main(String[] args) {
}
//Part 4: Question 1
    public static int Binary2int(String b){
        int size = b.length();
        double count, sum=0;
        boolean binary = true;

        for (int i=0; i<b.length() ; i++){
            int digit = b.charAt(i)-'0';
            if (digit>1){
                binary = false;
                i=b.length();
            }
            count = digit*Math.pow(2, size-i-1);
            sum+=count;
        }
        if (!binary){
            System.out.println("Error - "+b+" is not a binary number.");
        }
        int sum1 = (int)sum;
        return sum1;
    }
    //Part 4: Question 2
    public static boolean isBinaryString(String b){
        boolean binary = true;

        for (int i=0; i<b.length() ; i++){
            int digit = b.charAt(i)-'0';
            if (digit>1){
                binary = false;
                i=b.length();
            }
        }
        return binary;
    }
    //Part 4: Question 3
    public static String int2Binary(int n){
        int count=0;
        if (n<=0){
            count = 1;
        }
        for (int i=n; i>0 ;){
            i=i/2;
            count++;
        }

        int arr[] = new int [count];

        for (int i=n, t=0; i>0 ;t++){
            arr[arr.length-1-t] = i%2;
            i = i/2;
        }
        if (n<=0){
            arr[0] = 0;
        }
        String s = Arrays.toString(arr);

        return s;
    }
}

4.1意味着取一个字符串(代表二进制数)然后返回一个整数(代表输入中二进制数表示的整数)。 4.2以字符串的形式获取二进制数,然后返回一个布尔变量(如果是二进制数则为true,否则为false)。 4.3与4.1相反,它采用整数形式的整数,并以字符串的形式返回表示它的二进制数。 哦,当我测试它时一切正常。 现在的问题是检查它的人将使用以下(类型)脚本:

/**
 * This class represents a tester - to be used by students to check Ex3: <br>
 * 1. call all the public functions, check compilation. <br>
 * 2. test some of function of their results. <br> <br>
 * note: for debug change the printFlag to true.
 */
public class BynaryTest {

    /**
     * if set to true - will print a trace of all the checks.
     */
    public static boolean printFlag = true;//false;
    /**
     * number of errors the test program found
     */
    public static int error = 0;

    /**
     * this main function runs the test of the EX3Tester class
     */
    public static void main(String[] a) {
        System.out.println("******* Testing Ex3 - print mode = " + printFlag + " ********");
        checkEx34();
        System.out.println();
        System.out.println("******* U have got " + error + " errors ********");
    }

    public static void checkEx34() {
        if (printFlag) {
            System.out.println("**** Cheking Ex21 ****");
        }
        int[] nums = {0, 1, 12345};
        for (int i = 0; i < nums.length; i = i + 1) {
            String s = Primes.int2Binary(nums[i]);
            int num2 = Primes.Binary2int(s);
            if (nums[i] != num2) {
                error++;
                System.out.println("** Error in EX34:" + num2 + "!=" + nums[i] + " **");
            } else {
                if (printFlag) {
                    System.out.println("num[" + i + "]=" + nums[i] + " binary: " + s + " .. ok");
                }
            }
        }
    }
}

两个脚本之间的交互确实发生了,没有任何崩溃,但由于某种原因,我使用它的唯一结果是:

** Error in EX34:21!=0 **
Error - [1] is not a binary number.
** Error in EX34:21!=1 **
Error - [1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1] is not a binary number.
** Error in EX34:21!=12345 **

******* U have got 3 errors ********

我试图自己解决这个问题,或者找出导致这种不必要结果的原因,但我无法想出任何结果:(

2 个答案:

答案 0 :(得分:2)

你测试过吗?如果你这样做,错误很明显。

例如对于9,您的Int2Binary方法返回以下二进制表示形式:[1, 0, 0, 1]为String。

然后在你的Binary2Int中检查你是否只有0和1这样:

int digit = b.charAt(i)-'0';  // b is [1, 0, 0, 1]
if (digit>1){
   binary = false;
   //... more code ...
}

现在看错了吗?

答案 1 :(得分:0)

在方法intToBinary中,您有以下一行:

String s = Arrays.toString(arr);

我认为你期望这会采用一个数组并将其中的所有值连接成一个字符串。给出这个例子:

int[] array = {1, 0, 0};
System.out.println(Arrays.toString(array));

您希望输出为:

100

实际上它是:

[1,0,0]