gpt4 book ai didi

java - 程序连续两次打印菜单

转载 作者:行者123 更新时间:2023-12-02 06:29:48 26 4
gpt4 key购买 nike

我正在制作一个类似证书的程序,它还处于开发阶段。我放置了一个带有 boolean 条件的 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;

}
}
}
}

最佳答案

更改此行:

while(autoRun = true)

对此:

while(autoRun)

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

更新:

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

关于java - 程序连续两次打印菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20175613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com