Java 脚本与其测试人员之间的交互不好
所以我写了一个脚本:Java 脚本与其测试人员之间的交互不好,java,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
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;
}
}
/**
* 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 ********
我试着自己修复它,或者找出导致这种不想要的结果的原因,但我想不出什么:(你测试过它吗?如果你测试过,错误是很明显的 例如,对于9,您的
Int2Binary
方法返回以下二进制表示:[1,0,0,1]
作为字符串
然后在Binary2Int
中检查是否只有0和1,如下所示:
int digit = b.charAt(i)-'0'; // b is [1, 0, 0, 1]
if (digit>1){
binary = false;
//... more code ...
}
现在看到错误了吗?在方法
intToBinary
中,您有以下行:
String s = Arrays.toString(arr);
我认为您希望它将数组和其中的所有值合并为一个字符串。给出以下示例:
int[] array = {1, 0, 0};
System.out.println(Arrays.toString(array));
您希望输出为:
100
事实上:
[1,0,0]
我不确定我是否明白你想说的话,不管是什么情况,我都得到了我期望这个脚本给出的正确答案,只是测试脚本引起了麻烦,看起来是相同的错误(返回21?)在它的每一部分。我们想想我上面发布的程序的哪一部分在给定的输入上做了什么。问题就在那里,你只需要找出它;)测试脚本不是问题!它正在按预期工作。你的实现导致了这个问题,用铅笔和纸检查你的
Binary2Int
中的for
循环对输入字符串[1,0,0,1]
做了什么。就像我说的,我已经在调试器中一步一步地测试并看到了它,它给出了我想要的答案。为了以防万一,我再次通过调试器运行了它,它显示的是,当I=0 digit=1,这是可以的,当I=1 digit=0,I=2 digit=0,I=3 digit=1,到目前为止,布尔变量是真的,并且将以相同的方式返回。不,我说的不是isBinaryString
,而是Binary2Int
方法。当i=0时,您没有数字=1,因为字符串中的第一个字符不是1,而是一个[
。添加System.out.println(i+”:“+digit);
在int digit=b.charAt(i)-(0);
之后,然后再次检查……您是说“[”和“,”etc是什么导致了这个问题?我希望它只返回一个包含一个数字的字符串?我仍然不明白,为什么当我返回一个布尔值时,它仍然给出相同的错误?(我想它声称我在所有3个值中返回了21个?)采纳Edgar的建议,用铅笔和纸检查Binary2Int
中的for
循环对输入[1,0,0,1]
做了什么即使for循环不起作用并给出了错误的答案,从布尔变量中只能得到“真”或“假”两个答案,它似乎每次从测试仪上的函数中得到的只是数字“21”,这在这里有意义吗?集成测试之前的单元测试。即测试intToBinary(5)=“101”和BinaryToInt('101')=5。然后您可以测试intToBinary(5)=BinaryToInt('101'),尽管为什么它不会是一个谜。。。