打印2次donno为什么

时间:2014-09-10 15:47:35

标签: java printing

我遇到问题,当我从删除部分突破代码打印主菜单选项两次

代码中没有错误,它运行正常,但为什么打印两次而不是一次?

 public void mainloop() {
    for (int i = 0; i < 100; i++) {

        String x;

        System.out.println("Please Select your option");
        System.out.println("............................");
        System.out.println("1 ADD NAME AND NUMBER \n2 REMOVE NAME AND NUMBER \n3 SEARCH NAME AND NUMBER \n0 EXIT");
        System.out.println("............................");

        x = input.nextLine();

        if (x.equalsIgnoreCase("0")) {
            System.out.println("Thank you!");
            break;
        }
        if (x.equalsIgnoreCase("1")) {
            String Name;
            String Number;

            System.out.println("Please Enter your Name below");
            Name = input.nextLine();
            System.out.println("Please Enter your Number below");
            Number = input.nextLine();
            System.out.println("");
            System.out.println("Your Name " + Name + " and Number " + Number + " has been saved!\n");

            objectclass objectclassObject = new objectclass(Name, Number);
            Test.add(objectclassObject);
        }
        if (x.equalsIgnoreCase("2")) {

            System.out.println("-------ALL NAME AND NUMBERS-------");
            System.out.println("");

            for (int j = 0; j < Test.size();) {
                objectclass p = Test.get(j++);
                System.out.println(j + ". Name: " + p.getName() + " - " + p.getNumber());
            }
            for (int j = 0; j < Test.size(); j++) {
                System.out.println("");
                System.out.println("Enter Index number to remove Contact from Phonebook!");
                int v = input.nextInt();
                int temp = (v - 1);
                if (v >= 1 && v <= Test.size()) {
                    System.out.println("Name: " + Test.get(temp).getName() + " And Number: " + Test.get(temp).getNumber() + " has been removed!!");
                    System.out.println("");
                    Test.remove(temp);
                } else {
                    System.out.println("Please enter number properly!!");
                }
                break;
            }
        }
        if (x.equalsIgnoreCase("3")) {
            String y;
            System.out.println("*** Enter your Name below for search ***");
            y = input.nextLine();

            for (objectclass p : Test) {
                String z = p.getName();
                if (z.equalsIgnoreCase(y)) {
                    System.out.println("Your Name is: " + p.getName() + "\nYour Number is: " + p.getNumber());
                    System.out.println("");
                }
                if (!z.equalsIgnoreCase(y)) {
                    System.out.println("Contact not found!!!");
                }
            }
        }
    }
}

}

System.out.println("Please Select your option");
System.out.println("............................");
System.out.println("1 ADD NAME AND NUMBER \n2 REMOVE NAME AND NUMBER \n3 SEARCH NAME AND NUMBER \n0 EXIT");
System.out.println("............................");

打印两次:/〜

2 个答案:

答案 0 :(得分:1)

如@Aeshang所述,请使用Switch代替if。 其次

if (x.equalsIgnoreCase("2")) {

块在

之前没有结束
if (x.equalsIgnoreCase("3")) {

您的删除部分还包括搜索部分。不确定这是否能解决问题,但首先要纠正这些问题并检查答案。

答案 1 :(得分:1)

如果打印两次,你通过循环2次。尝试在读取后显示变量“x”。我打赌你的合法输入之间有额外的空字符串。

nextInt()不会消耗换行符。因此,下次读取x时,请读取值v。

之后的行尾