程序连续两次打印菜单

时间:2013-11-24 14:21:25

标签: java

我正在制作类似计划的凭证,而且它非常重要。我放置了一个带有布尔条件的while循环,以便在一个人完成创建凭证时再次运行程序,但是当它循环到程序的开头时,它会打印第一个部分两次!像这样:

run:
     |-----------------------------------------------|
     |Welcome to the Credential Managing System 2013!|
     |-----------------------------------------------|

      Would you like to create or manage a credential?

                 Choose: Manage | Create

                    Waiting for input: Create

Credential name: df
Credential ID: 34243
Credential Password: numbers or words?
Waiting for input: numbers
Credential Password: 13651

                      |-------------------|
                      |Credential created!|
                      |-------------------|




      Would you like to create or manage a credential?

                 Choose: Manage | Create

                    Waiting for input:

从那里再次停止阅读输入。

这是整个代码!

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Carlinhos
*/
import java.util.Scanner;

public class Criação {

public static void main(String[] args){
Scanner e = new Scanner(System.in);



System.out.println("         |-----------------------------------------------|");
System.out.println("         |Welcome to the Credential Managing System 2013!|");
System.out.println("         |-----------------------------------------------|");
System.out.println("");

Boolean autoRun = true;

while(autoRun){

System.out.println("          Would you like to create or manage a credential?");
System.out.println("");
System.out.println("                     Choose: Manage | Create");
System.out.println("");
System.out.print("                        Waiting for input: ");

String op1 = e.nextLine();
autoRun = false;

if(op1.equals("Create")){

    System.out.println();
    System.out.print("Credential name: ");
    String credName = e.nextLine();
    System.out.print("Credential ID: ");
    int credID = e.nextInt();
    System.out.println("Credential Password: numbers or words?");
    System.out.print("Waiting for input: ");
    e.nextLine();
    String credPassCheck = e.nextLine();

    if(credPassCheck.equals("numbers")){

        System.out.print("Credential Password: ");
        double credPassNum = e.nextDouble();
        }

    else if(credPassCheck.equals("words")){

        System.out.print("Credential Password: ");
        String credPassLet = e.nextLine();
        }

    System.out.println("");
    System.out.println("                          |-------------------|");
    System.out.println("                          |Credential created!|");
    System.out.println("                          |-------------------|");
    System.out.println("");
    System.out.println("");
    System.out.println("");
    System.out.println("");

    autoRun = true;

        }
    }
}
}

1 个答案:

答案 0 :(得分:2)

更改此行:

while(autoRun = true)

到此:

while(autoRun)

=不是等式比较运算符,它是赋值运算符。因此,当您检查条件autoRun是否已分配值true时,即使您之前将其设置为false。您可以使用==代替,但由于autoRun是一个布尔变量,因此您只需使用此变量而无需将其与其他任何内容进行比较。

<强>更新

在OP发表评论后,程序在第二次打印提示后开始退出,我再查看了一下代码并找到了以下几行:

if(credPassCheck.equals("numbers")){
    System.out.print("Credential Password: ");
    double credPassNum = e.nextDouble();
}

这基本上消耗的输入少于必要的输入,因此“等待输入”的下一个提示将获得一个换行符作为输入。修复现在应该是显而易见的:

if(credPassCheck.equals("numbers")){
    System.out.print("Credential Password: ");
    double credPassNum = e.nextDouble();
    e.nextLine(); //Add this Line
}