Java直接跳转到else语句?

时间:2014-10-30 10:48:54

标签: java

我一直在尝试编写一个火车预订系统,这是我的学校项目,但我不知道为什么我的if语句直接跳转到else即使if 1}}陈述是真的。删除else语句可以解决问题,但我需要它,因为必须打印相应的错误消息。

for (int j=0;j<len;j++)
    {
        if (trainno[j]==tno)
        {
            stna = trainname[j];
            stno = tno;
            sto = origin[j];
            std = destination[j];
            stf = fare[j];
        }
        else if (tno==0)
        {
            System.out.println("Thanks for visiting our website!");
            return;
        }
        else
        {
            System.out.print("Wong input! Try again....");
        } //I wasn't able to assign an error message due to some problem because of which it was always jumping to else statement even if trainno[j] was equal to tno, do tell me if you can find the problem
    }

整个代码

import java.io.*;
class PROJECT
{
    BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
    void main () throws IOException
    {
    System.out.println("Welcome to IRCTC Railway Reservation System! Please proceed further to book your train!");
    System.out.println();
    String[] trainname = {"Rajdhani EXP", "AUG Kranti EXP", "Chennai EXP", "Aravali EXP", "Paschim EXP", "Gareeb Rath", "Punjab Mail", "Dehradun EXP", "Swaraj EXP", "Aravali EXP"};
    int[] trainno = {57835, 87612, 15384, 16512, 65265, 51654, 31543, 56416, 85484, 78455};
    String[] origin = {"Mumbai", "Mumbai", "Delhi", "Kolkata", "Mumbai", "Goa", "Durg", "Aligarh", "Jaipur", "Bhuj"};
    String[] destination = {"Surat", "Delhi", "Chennai", "Mumbai", "Ajmer", "Mumbai", "Bikaner", "Agra", "Madurai", "Buxar"};
    int[] fare = {650, 950, 1100, 1200, 1050, 600, 1100, 1250, 1300, 1100};
    int len = trainname.length;
    int len1 = trainno.length;
    int len2 = origin.length;
    int len3 = destination.length;
    int len4 = fare.length;
    String stna = "";
    int stno = 0;
    String sto = "";
    String std = "";
    int stf = 0;
    System.out.println("Train Name\t\tTrain No.\tOrigin\t        Destination\tFare");
    System.out.println();
    for (int i=0;i<len;i++)
    {
        System.out.println(trainname[i]+"\t\t"+trainno[i]+"\t\t"+origin[i]+"\t\t"+destination[i]+"\t\t"+fare[i]); 
    }
    System.out.println();
    System.out.print("Enter train no. to select train or enter 0 to exit :- ");
    int tno = Integer.parseInt (br.readLine());
    for (int j=0;j<len;j++)
    {
        if (trainno[j]==tno)
        {
            stna = trainname[j];
            stno = tno;
            sto = origin[j];
            std = destination[j];
            stf = fare[j];
        }
        else if (tno==0)
        {
            System.out.println("Thanks for visiting our website!");
            return;
        }
        else
        {
            System.out.print("Wong input! Try again....");
        } //I wasn't able to assign an error message due to some problem because of which it was always jumping to else statement even if trainno[j] was equal to tno, do tell me if you can find the problem
    }
    System.out.print("Enter number of passengers (max 10) :- ");
    int nopt = Integer.parseInt (br.readLine()); 
    if (nopt>10)
    {
        System.out.print("Uh-Oh! No. of passengers are more than 10, please try again");
        return;
    }
    String[] pn = new String[nopt];
    int[] age = new int[nopt];
    for (int k=0;k<nopt;k++)
    {
        System.out.print("Enter passenger's name :- ");
        pn[k] = br.readLine();
        System.out.print("Enter passenger's age :- ");
        age[k] = Integer.parseInt (br.readLine());
        System.out.println();
    }
    double tf = (nopt*stf)*1.12;
    System.out.println("Booking Details :-");
    System.out.println();
    System.out.println("No. of passengers :- "+nopt);
    for (int z=0;z<nopt;z++)
    {
        System.out.println("Name of passenger travelling :- "+pn[z]);
    }
    System.out.println("Train Name :- "+stna);
    System.out.println("Train Number :- "+stno);
    System.out.println("Train Origin :- "+sto);
    System.out.println("Train Destination :- "+std);
    System.out.println("Train Fare/Person :- "+stf);
    System.out.println("Total Fare (12% tax added) :- "+tf);
    System.out.println();
    System.out.println("Thanks for your booking! Your seats have been confirmed. Have a good day!");
}
}

4 个答案:

答案 0 :(得分:1)

尝试这样做:

System.out.print("Enter train no. to select train or enter 0 to exit :- ");
int tno = Integer.parseInt (br.readLine());

if(nto==0){
    System.out.println("Thanks for visiting our website!");
    return;
}else
{
    int j=0;
    while(trainno[j]!=tno && j<=trainno.length())
    {j++;
    }
     if (trainno[j]==tno)
     {
        stna = trainname[j];
        stno = tno;
        sto = origin[j];
        std = destination[j];
        stf = fare[j];
     }
     if(j>trainno.length())
     {
        System.out.print("Wong input! Try again....");
     } 
    }
}

答案 1 :(得分:1)

尝试删除else if(tno==0)并将其替换为else。我认为else if部分没有要求。

此外,继续使用AMbrish所说的内容也会使代码运行。

答案 2 :(得分:0)

只需在if语句之前添加一些sop / log,以检查布尔表达式是返回true还是false,然后在值为true后查看是否进入else块。复制日志并粘贴到此处,然后我们可以进一步帮助。感谢

答案 3 :(得分:0)

如果您更改了代码(如下所示),那么您应该得到预期的结果:

    int tno = Integer.parseInt(br.readLine());
    boolean foundTrain = false;
    if (tno == 0) {
        System.out.println("Thanks for visiting our website!");
        return;
    } else {
        for (int j = 0; j < len; j++) {
            if (trainno[j] == tno) {
                foundTrain = true;
                stna = trainname[j];
                stno = tno;
                sto = origin[j];
                std = destination[j];
                stf = fare[j];
                break;
            }
        }
    }

    if (!foundTrain) {
        System.out.print("Wong input! Try again....");
        return;
        // I wasn't able to assign an error message due to some problem because of which it was always jumping to else
        // statement even if trainno[j] was equal to tno, do tell me if you can find the problem
    }