在while语句中使用嵌套的if和else if语句

时间:2014-06-20 08:58:13

标签: java if-statement while-loop nested-loops

我一直在让我的程序执行我需要它做的事情。代码中没有语法错误。

首先,我的程序设置为要求用户输入名字和姓氏最多3次,用户应该能够提前结束输入。

有没有办法可以把System.out.print(“你想添加一个新名字(Y / N)”); if语句之外?因此,在我输入我的第三个名字后,它会停止,或者输入2个名字并输入N.

非常感谢任何帮助。

import java.util.Scanner;

public class SOF {


 public static void main(String test[]){




    @SuppressWarnings("resource")
    Scanner scanner = new Scanner (System.in);

    System.out.print("Do you want to add a new name (Y/N)");
    String newname = scanner.next();

    int AddName = 0;

    while (AddName < 3) {


        if (newname.equalsIgnoreCase("y")) 
        {


            System.out.println("Enter first name: ");
            String fname = scanner.next();

            System.out.println("Enter last name: ");
            String lname = scanner.next();

            AddName = AddName + 1;

            System.out.print("Do you want to add a new name (Y/N)");
            String newnamee = scanner.next(); //had to add an extra e, since I had two varibles
                                              // with the same time. This might be the issue.
                                              //I'm unsure though



        }

        else if (newname.equalsIgnoreCase("n")) {

            System.out.println("Goodbye");
            AddName = AddName + 3;
        }
    }
}

}

3 个答案:

答案 0 :(得分:2)

您可以将System.out.print("Do you want to add a new name (Y/N)");放在while循环的开头,如下所示:

import java.util.Scanner;

public class SOF {
public static void main(String test[]){
@SuppressWarnings("resource")
Scanner scanner = new Scanner (System.in);
int AddName = 0;

while (AddName < 3) {
System.out.print("Do you want to add a new name (Y/N)");
String newname = scanner.next();

    if (newname.equalsIgnoreCase("y")) 
    {
        System.out.println("Enter first name: ");
        String fname = scanner.next();
        System.out.println("Enter last name: ");
        String lname = scanner.next();
        AddName = AddName + 1;         

    }

    else if (newname.equalsIgnoreCase("n")) {
        System.out.println("Goodbye");
        AddName = AddName + 3;  //alternatively you can use break statement.
    }
   }
  }
 }

答案 1 :(得分:1)

改为使用do:

public static void main(String test[]) {

        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        int AddName = 0;

        do  {

            System.out.print("Do you want to add a new name (Y/N)");
            String newname = scanner.next();

            if (newname.equalsIgnoreCase("y")) {

                System.out.println("Enter first name: ");
                String fname = scanner.next();

                System.out.println("Enter last name: ");
                String lname = scanner.next();

                AddName = AddName + 1;

            }

            else if (newname.equalsIgnoreCase("n")) {

                System.out.println("Goodbye");
                AddName = AddName + 3;
            }

        } while (AddName < 3);
    }

答案 2 :(得分:0)

您的第二个变量从未使用过:

String newnamee = scanner.next(); //<-- remove the declaration

newname = scanner.next();//<-- try this instead