我写了一个比较2个两位无符号整数的新程序.比较汉明距离.但我的算法并不完美.你能告诉我这段代码有什么问题:(感谢很多!!
这是我的计算方法;
int countHammDist(unsigned int n, unsigned int m)
{
int i=0;
unsigned int count = 0 ;
for(i=0; i<8; i++){
if( n&1 != m&1 ) {
count++;
}
n >>= 1;
m >>= 1;
}
return count;
}
Run Code Online (Sandbox Code Playgroud)
a和b 8位二进制文件.
PrintInBinary(a);
PrintInBinary(b);
printf("\n %d", countHammDist(a,b));
Run Code Online (Sandbox Code Playgroud)
让我告诉你输出;
Enter two unsigned integers (0-99): 55 64
Your choices are 55 and 64
Number A: 00110111
Number B: 01000000
Hamming distance is ; 5
Run Code Online (Sandbox Code Playgroud) 首先,这是我的代码中有问题的部分; 这些都是非常基础的课程
public Passenger(String Name, String adress, String number, String password){
count++;
accId+=count;
this.Name=Name;
this.adress=adress;
this.number=number;
if(checkPw(password)==true){
this.password=password;
}
}
private boolean checkPw(String password){
int length;
length = password.length();
if(length != 6){
return false;
}
else if(password.charAt(0)==0){
return false;
}
else {
for (int i = 0; i < password.length();i++){
if((password.charAt(i))==(password.charAt(i+1))){
return false;
}
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
识别TestClass:
public static void main(String[] args){
Passenger gokhan=new Passenger("Gokhan","Istanbul","xxx","254651");
System.out.println(gokhan.password);
}
Run Code Online (Sandbox Code Playgroud)
所以,我认为问题出在Passenger类.这是我第一次在课堂上上课(我的意思是if(checkPw(密码)== true)部分).在测试类中,它看起来非常清晰,我从未想过会出现此错误.我该如何避免这条消息?
完整错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index …Run Code Online (Sandbox Code Playgroud) 我正在练习我的C编程语言技能,当我写这个代码编译器显示一堆错误但我没有看到这有什么问题.我在互联网上学到了这个代码所以希望你能帮助我:)
这是代码;
struct rect {
int x1;
int y1;
int x2;
int y2;
};
struct rect intersection(struct rect m, struct rect n) {
struct rect intersection;
if((n.x1 > m.x1) && (n.x1 < m.x2) && (n.y1 > m.y1 ) && (n.y1 < m.y2)){
rect intersection = {.x1=n.x1, .y1=n.y2, .x2=m.x2, .y2=m.y2};
return intersection;
}
else if((m.x1 > n.x1) && (m.x1 < n.x2) && (m.y1 > n.y1 ) && (m.y1 < n.y2)){
rect intersection = {.x1=m.x1, .y1=m.y2, .x2=n.x2, .y2=n.y2};
return intersection;
}
return …Run Code Online (Sandbox Code Playgroud)