2014-10-01 3 views
-2

자바 : 논리 오류, 여러 클래스에 있어야하고 싶지 않아서 나를 죽이는데 도움이 필요합니다. 안녕하세요, 필자는 필자와 커뮤니티에 새로운입니다. 도움이 필요해, 나는 내 머리카락을 꺼내. Blue J를 사용하는 Java 6을 가지고 있고, 내가 원하는 것을 위해 저를 위해 일하는 것이 아닙니다.Java : 터미널에서 실행될 때마다 출력이 다른 이유는 무엇입니까?

두 개의 클래스가 있으며 서로 효율적으로 말하지 않습니다. 보세요 (숙제이기 때문에 제안을 할 필요는 없지만이 사실을 알 수 없으며 선생님은 시간이 없어서 내일 시험을 보게됩니다.)

가는 드라이버 클래스에서 화가 봐.

import java.util.Scanner; 
public class TeacherDriver 
{ 

public static void main (String args[]) 
{ 

System.out.println("My favorite Teachers are in rooms: 225, 123, 237" + 
        "\nEnter a room number to learn more about the teacher (or -1 to Stop): "  ); 

int rmnum2 = 0,rmnum = 0; 
Scanner input2 = new Scanner(System.in); 

for (int counter = 100; counter >= 1; counter--) 
{ 
    rmnum = input2.nextInt(); 
    if (rmnum == -1)break; 
    teacher teacherObject = new teacher(rmnum); 
    System.out.println(teacherObject.toString()); 
}//end of the for loop to keep going through the same processes 
System.out.println("Thanks for playing"); 
}//end of the main method of teacher to run program 
}//end of class for the program complete, Teacher Class 
} 

그리고 여기가 교사 클래스, 내가을 원하는 무엇

public class teacher 
{ 
String name, catchphrase,teacher; 
int roomnumber, rmnum, rmnum2, input2; 

public teacher(int rmnum) 
{ 
if (rmnum == 225){ 
    name = "Mr. Clark"; 
    catchphrase = "Just do it."; 
    roomnumber = 225; 
} 
if (rmnum == 123){ 
    name = "Mr. Harris"; 
    catchphrase = "Do the essays and you will pass."; 
    roomnumber = 123; 
} 
if (rmnum == 237){ 
    name = "Mr. Turley"; 
    catchphrase = "Give a perfect effort."; 
    roomnumber = 237; 
} 
System.out.println ("I don't have a teacher in that room."); 
System.out.println("Always show"); 
}//end of method to input items 
public String toString() 
{ 
String str = "You chose: " + name + 
"\nRoom Number: " + roomnumber + 
"\nCatch Phrase is " + catchphrase ; 
return str; 
}//string of argument that the string is recalled for putting all the items together. 
}//end of teacher class for teachers info 

이 내가에 도움이 필요 꽤 바보 항목입니다

My favorite Teachers are in rooms: 220, 130, 201 
Enter a room number to learn more about the teacher (or ­‐1 to stop): 
130 
You chose: Ms. English 
They're in room: 130 
Their cathcphrase is "This above all; to thine own self be true." 
Type another(-1 to stop) 
201 

You chose: Sra. Spanish 
They're in room: 201 
Their catch phrase is "Via con tacos" 
type another (-1 to stop) 
111 
I don't have a favorite teacher in that room! 

Type another(-1 to stop) 
-1 
Thanks for playing> 

의 출력입니다 아마 내 앞에서 그 권리. 나는 (나는 선생님이 없다) 나타나지 않기를 바란다. 그리고 내가 계속 얻는 것은 여기에있다. 그래서 거기에 아무것도 나를 도울 수 너희들인가, 내가 잘못하고있는 중이 야 도대체 무엇

My favorite Teachers are in rooms: 225, 123, 237 
Enter a room number to learn more about the teacher (or -1 to Stop): 
123 
I don't have a teacher in that room. 
Always show 
You chose: Mr. Harris 
Room Number: 123 
Catch Phrase is Do the essays and you will pass. 
237 
I don't have a teacher in that room. 
Always show 
You chose: Mr. Turley 
Room Number: 237 
Catch Phrase is Give a perfect effort. 
225 
I don't have a teacher in that room. 
Always show 
You chose: Mr. Clark 
Room Number: 225 
Catch Phrase is Just do it. 
-1 
Thanks for playing 

는, 그래, 난 스위치이보다 효율적으로 만들 수 있지만, 난 그냥 내가 알고있는 것을 사용했다. 이것은 많은 것을 의미 할 것입니다. 이 출력이 왜 내가 원하는 것과 다른지에 대한 도움이 필요합니다.
감사합니다.
P. 2 클래스가 필요합니다!,
2 개의 출력이 동일한 선생님 일 필요는 없습니다. 올바른 데이터

예를 설정 한 후 반환하도록 코드를 변경

+1

힌트 : 조건이 작동하는 방법을 배우고 교사 생성자 코드를 살펴 봅니다. – Josnidhin

+0

@ jdog1218 왜 스위치 케이스를 사용하지 않습니까? –

+0

선생님은 지금과 늦은 프로젝트의 종류에 관해 우리에게 아무것도 가르쳐주지 않았습니다. 나는 다음에 그것을 확실히 사용할 것이다. 이것은 너무 힘든 일입니다. – jdog1218

답변

0

했다 생성자가 있다면 덜 반복적으로 깨끗한 것

public teacher(int rmnum){ 
    if (rmnum == 225){ 
     name = "Mr. Clark"; 
     catchphrase = "Just do it."; 
     roomnumber = 225; 
    }else if (rmnum == 123){ 
     name = "Mr. Harris"; 
     catchphrase = "Do the essays and you will pass."; 
     roomnumber = 123; 
    }else if (rmnum == 237){ 
     name = "Mr. Turley"; 
     catchphrase = "Give a perfect effort."; 
     roomnumber = 237; 
    }else 
     System.out.println ("I don't have a teacher in that room."); 
     System.out.println("Always show"); 
} 

또는 @Scary 웜뱃의 대답도 할 것입니다 : rnum은 무엇 당신이 할 수있는 것은이 아니다 225,123,237이다.

+0

else-if 문을 잊어 버렸습니다. 감사합니다. – jdog1218

0

시도 물론

if (rmnum == 225){ 
    name = "Mr. Clark"; 
    catchphrase = "Just do it."; 
    roomnumber = 225; 
    return; 
} 
if (rmnum == 123){ 
    name = "Mr. Harris"; 
    catchphrase = "Do the essays and you will pass."; 
    roomnumber = 123; 
    return; 
} 
if (rmnum == 237){ 
    name = "Mr. Turley"; 
    catchphrase = "Give a perfect effort."; 
    roomnumber = 237; 
    return; 
} 

당신은 당신이 마지막 2 if 문을 실행하지 않도록 코드를 알려줄 수있는 방법이없는 namecatchphraseroomnumber

+0

은 자동으로 void 반환 값을 갖는 생성자 클래스에 있습니다. – jdog1218

+1

네,하지만 부르지 않으므로'System.out.println ("그 방에 선생님이 없습니다."); ' –

+0

@ jdog1218 void 메소드에서'return'을 호출한다고해서 메소드가 반드시 리턴해야 할 것이거나 리턴 할 필요가 없다는 것을 의미하는 것은 아니며, return 문을 실행하지 않아도된다는 의미는 아닙니다. – qbit

관련 문제