2014-10-01 58 views
-2

Java:邏輯錯誤,不想讓我想在多個類中,需要幫助它正在讓我失望 嗨,我是新來的社區,我拼命需要幫助,我正在拉我的頭髮。 我有使用藍色J的Java 6,它只是不適合我想要的東西。Java:爲什麼我的輸出每次都不一樣我每次去終端運行

有2個班,他們不會有效地說對方。 看一下(是的,你不必給我建議,因爲這是作業,但我只是無法弄清楚這一點,我的老師跑出時間,我明天有考試。)

我是去看看司機類。

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 2014-10-01 02:24:05

+0

@ jdog1218爲什麼你不使用開關盒? – 2014-10-01 02:37:46

+0

我的老師現在沒有向我們介紹任何關於它和它最近的一個項目。我一定會在下次使用它。這太難了。 – jdog1218 2014-10-01 03:42:52

回答

0

你沒有辦法告訴你的代碼不執行最後2條語句如果一個構造函數可以將其用作少重複清潔rnum不是225,123,237你可以做的是這樣的:

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袋熊的回答會做的一樣好。

+0

我一直忘記其他,如果說法。謝謝 – jdog1218 2014-10-01 02:38:20

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; 
} 

當然,如果你有這樣的花namecatchphraseroomnumber

+0

它在構造函數類中自動具有void返回值。 – jdog1218 2014-10-01 02:24:27

+1

是的,但是因爲你沒有調用它,它會繼續到'System.out.println(「我在那個房間裏沒有老師。「);' – 2014-10-01 02:26:12

+0

@ jdog1218在void方法中調用'return'並不意味着該方法必須或將要或必須返回一些東西,這意味着你不會執行比'return'語句更多的內容 – qbit 2014-10-01 02:30:35

相關問題