이 작은 프로그램을 재미있게 만들었으며 인터페이스 작동 방식을보고 싶었습니다. eatsOrNot 함수에 "iarba"를 입력하면 dog 객체에 대해 거짓이 발생해야합니다. 부울 함수에서 내가 뭘 잘못하고 있니?부울 함수가 제대로 작동하지 않습니다.
package animals;
public interface Actions {
void Sound();
boolean eatsOrNot(String s);
void guessNumberOfLegs(int x);
}
package animals;
public class Dog implements Actions {
public boolean eatsOrNot(String s)
{
if (s=="paine"||s=="morcovi"||s=="iarba") return false;
else return true;
}
public void Sound() {
System.out.println("Dog goes 'woof'");
}
public void guessNumberOfLegs(int x) {
if (x==4)
System.out.println("Right!");
else System.out.println("Wrong!");
}
}
package animals;
public class Rabbit implements Actions {
public boolean eatsOrNot(String s)
{
if (s=="fier") return false;
else return true;
}
public void Sound() {
System.out.println("Rabbit goes bla");
}
public void guessNumberOfLegs(int x) {
if (x==4)
System.out.println("Right!");
else System.out.println("Wrong!");
}
}
package animals;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class TestAnimals {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner in= new Scanner(System.in);
BufferedReader in1=new BufferedReader(new InputStreamReader(System.in));
Dog dog=new Dog();
Rabbit rabbit=new Rabbit();
System.out.println("Rabbit sound:");
rabbit.Sound();
System.out.println("Dog sound:");
dog.Sound();
System.out.println("Guess number of a dog's legs:");
int legs=in.nextInt();
dog.guessNumberOfLegs(legs);
System.out.println("Guess number of a rabbit's legs:");
legs=in.nextInt();
dog.guessNumberOfLegs(legs);
System.out.println("The dog eats or not : ");
String str=in1.readLine();
dog.eatsOrNot(str);
System.out.println(dog.eatsOrNot(str));
System.out.println("The rabbit eats or not : ");
String str1=in1.readLine();
rabbit.eatsOrNot(str1);
System.out.println(rabbit.eatsOrNot(str1));
}
}
감사합니다. 나는 equals 함수에 대해 머리를 맞대고 지금 사용해 보겠습니다. – 123123d