-简介: 您好,我正在编写一个简单的程序来练习。该程序将成为密码生成器。还处于初期阶段。在测试代码时,我遇到了一个问题。我用谷歌搜索,能够找到类似的案例,但是我仍然无法弄清楚。
-问题: 问题出现在同时(规范== 0)之后的几行中。当用户在扫描仪中输入Y或N时,它应该属于“ else if”语句,并在输出屏幕上打印出一个数字,但是它会继续打印“请输入Y或N”。这意味着它会将我的“ else if”语句作为不正确的匹配传递。我该如何解决?
-我尝试过的事情: 我尝试了Nc.toString()。equals(“ Y”),还尝试了其他人们共享的小代码,但它们根本是错误的或没有用。
-当前输出:
openMenu = (scroll,e) => {
this.setState(prevState => ({ showMenu: !prevState.showMenu }),
() => {
document.addEventListener("click", this.closeMenu);
/*
Define a scrollHandler in this closure with "scroll" passed to closeMenu().
Use this scrollHandler reference for both addEventListener() and subsequent
removeEventListener() call
*/
this.scrollHandler = () => this.closeMenu(scroll);
/*
Pass scrollHandler as handler for scroll event
*/
scroll.addEventListener('scroll', this.scrollHandler)
}
);
};
closeMenu = (scroll,e) => {
this.setState({ showMenu: false }, () => {
document.removeEventListener("click", this.closeMenu);
/*
If scrollHandler is defined, remove it as a handler from the element's scroll
event, and clear the reference to clean up
*/
if(this.scrollHandler) {
scroll.removeEventListener('scroll',this.scrollHandler)
this.scrollHandler = undefined;
}
});
};
-预期输出:
Sys: How long will the Password be? (Numbers only)
User: 5
Sys: Do you want to include Special Characters? (Y/N)
User: Y
Sys: Please type Y or N
Sys: Do you want to include Special Characters? (Y/N)
User: N
Sys: Please type Y or N
Sys: Do you want to include Special Characters? (Y/N)
User: Yes
Sys: Please type Y or N
Sys: Do you want to include Special Characters? (Y/N)
User: 5
Sys: Please type Y or N!
Sys: Do you want to include Special Characters? (Y/N)
-完整代码:
Case 1
Sys: How long will the Password be? (Numbers only)
User: 5
Sys: Do you want to include Special Characters? (Y/N)
User: Y
Sys: 1
Case 2
Sys: Do you want to include Special Characters? (Y/N)
User: N
Sys: 2
Case 3
Sys: Do you want to include Special Characters? (Y/N)
User: Yes
Sys: Please type Y or N
Case 4
Sys: Do you want to include Special Characters? (Y/N)
User: 5
Sys: Please type Y or N!
Sys: Do you want to include Special Characters? (Y/N)
答案 0 :(得分:0)
要从扫描仪中读取单词,请使用next():
String userInput = Nc.next();
if(userInput.equals("Y")){
// some code
}