无法从扫描仪的输入中识别.equals(String)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法从扫描仪的输入中识别.equals(String)相关的知识,希望对你有一定的参考价值。
- 简介:你好,我正在编写一个简单的程序来练习。该程序将成为密码生成器。它处于初始阶段。当我测试我的代码时,我遇到了一个问题。我用Google搜索并找到了类似的案例,但我还是想不通。
- 问题:问题在While(Spec == 0)之后开始耦合线当用户在扫描仪中输入Y或N时,它应该属于'else if'语句并打印出一个数字到输出屏幕,但它保持打印“请键入Y或N”。这意味着它将我的'else if'语句作为错误匹配传递。我该如何解决?
- 我尝试过的:我尝试了Nc.toString()。equals(“Y”),我还尝试了其他人共享的小代码,但它们只是错误或无法正常工作。
- 电流输出:
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)
- 完整代码:
// This is what I have currently.
import java.util.Scanner;
import java.math.*;
public class Main {
// Ask questions: how long char, include special letters or numbers.
// How long PW gonna be
private static int Char = 0;
// Special Letter Y = 1, N = 2
private static int Spec = 0;
// Include Number? Y = 1, N = 2
private static int Num = 0;
public static void main(String[] args){
// While character is 0, it's looping until value changes
while(Char == 0){
// Reads the user input
Scanner Sc = new Scanner(System.in);
// Asks the question to the user
System.out.println("How long will the Password be? (Numbers only)");
// If it has an integer
if(Sc.hasNextInt()){
Char = Sc.nextInt();
}
// If it doesn't have an integer
else {
System.out.println("Please type number only!");
}
}
// While Spec is 0, it's looping until value changes
while(Spec == 0){
// Reads the user input
Scanner Nc = new Scanner(System.in);
// Asks the question to the user
System.out.println("Do you want to include Special Characters? (Y/N)");
// If it has an integer, prompts to redo
if(Nc.hasNextInt()){
System.out.println("Please type Y or N!");
}
// If user types Y, Yes then changes spec to 1
else if (Nc.equals("Y")) {
Spec = 1;
System.out.println(Spec);
}
// If user types N, No then changes spec to 2
else if (Nc.equals("N")) {
Spec = 2;
System.out.println(Spec);
}
// If neither above, prompts to redo
else {
System.out.println("Please type Y or N");
}
}
}
}
答案
要从扫描仪中读取单词,请使用next():
String userInput = Nc.next();
if(userInput.equals("Y")){
// some code
}
以上是关于无法从扫描仪的输入中识别.equals(String)的主要内容,如果未能解决你的问题,请参考以下文章
apk文件二维码微信无法识别 APP在微信中二维码扫描无法下载的解决方案
Phonegap 条码扫描仪无法识别 windows phone 上的任何条码