Java:为什么每次我在终端上运行时输出都不同

时间:2014-10-01 02:15:39

标签: java

Java:逻辑错误,我不想要多个类,需要帮助它会杀了我 嗨,我是社区的新手,我迫切需要帮助,我正在拔头发。 我有使用Blue J的Java 6,它只是不适合我的工作。

有两个班级,他们不会互相高效地说话。 看看,(是的,你不必给我建议,因为这是家庭作业,但我只能解决这个问题,我的老师没时间了,我明天要参加考试。)

我很生气地看着驱动程序类。

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

我做错了什么,是的,我可以通过开关使这更有效率,但我只是用我所知道的,所以你们有什么可以帮助我的。这意味着很多。 我需要帮助解释为什么这个输出与我想要的输出完全不同。
由于
附:我需要2节课!和 2个输出不必是同一个老师。

2 个答案:

答案 0 :(得分:0)

尝试更改代码,以便在设置正确的数据后返回

e.g。

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

当然,如果你的构造函数有name catchphraseroomnumber

,那么它会更简洁,重复性更低

答案 1 :(得分:0)

如果rnum不是225,123,237,您无法告诉您的代码不执行最后2个语句:您可以做的是:

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 Wombat的回答也会这样做。