2014-10-01 56 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

相关问题