Java 如何使用DoWhile循环再次检查用户输入?
我想在我的程序中添加一个循环,这样当用户输入错误的名称时,它会返回到程序的开头,并要求他们再次输入自己的名称。我想我需要一个Java 如何使用DoWhile循环再次检查用户输入?,java,do-while,Java,Do While,我想在我的程序中添加一个循环,这样当用户输入错误的名称时,它会返回到程序的开头,并要求他们再次输入自己的名称。我想我需要一个do-while循环,但我不确定如何使用已经包含的if语句和布尔值来实现它。我希望用户只有三个条目,如果他们三次出错,程序就会关闭 import java.util.Scanner; public class Username { public static void main(String[] args) { { Scanner kb = n
do-while
循环,但我不确定如何使用已经包含的if语句和布尔值来实现它。我希望用户只有三个条目,如果他们三次出错,程序就会关闭
import java.util.Scanner;
public class Username
{
public static void main(String[] args)
{
{
Scanner kb = new Scanner(System.in);
// array containing usernames
String[] name = {"barry", "matty", "olly", "joey"}; // elements in array
System.out.println("Enter your name");
String name1 = kb.nextLine();
boolean b = true;
for (int i = 0; i < name.length; i++)
{
if (name[i].equals(name1))
{
System.out.println("you are verified you may use the lift");
b = false;
break;// to stop loop checking names
}
}
if (b)
{
System.out.println("Invalid entry 2 attempts remaining, try again");
}
}
import java.util.Scanner;
公共类用户名
{
公共静态void main(字符串[]args)
{
{
扫描仪kb=新扫描仪(System.in);
//包含用户名的数组
String[]name={“barry”、“matty”、“olly”、“joey”};//数组中的元素
System.out.println(“输入您的姓名”);
字符串名称1=kb.nextLine();
布尔b=真;
for(int i=0;i
您可以在while循环中使用一个条件
boolean b = false;
while(!b){
System.out.println("Enter your name");
String name1 = kb.nextLine();
for (int i = 0; i < name.length; i++) {
if (name[i].equals(name1)) {
b = true;
System.out.println("you are verified you may use the lift");
}else{
System.out.println("Invalid entry 2 attempts remaining, try again");
}
}
}
boolean b=false;
而(!b){
System.out.println(“输入您的姓名”);
字符串名称1=kb.nextLine();
for(int i=0;i
如果满足名称条件,循环将退出,如果不满足,循环将循环。您可以在while循环中使用条件。如下所示:
boolean b = false;
while(!b){
System.out.println("Enter your name");
String name1 = kb.nextLine();
for (int i = 0; i < name.length; i++) {
if (name[i].equals(name1)) {
b = true;
System.out.println("you are verified you may use the lift");
}else{
System.out.println("Invalid entry 2 attempts remaining, try again");
}
}
}
boolean b=false;
而(!b){
System.out.println(“输入您的姓名”);
字符串名称1=kb.nextLine();
for(int i=0;i
如果满足名称条件,循环将退出,如果不满足,循环将循环。您可以这样做:
int count = 0;
point:
do {
System.out.println("Enter your name");
String name1 = kb.nextLine();
boolean b = true;
for (int i = 0; i < name.length; i++) {
if (name[i].equals(name1)) {
System.out.println("you are verified you may use the lift");
b = false;
break point;// to stop loop checking names
}
}
if (b) {
count++;
System.out.println("Invalid entry 2 attempts remaining, try again");
}
while(!b || count <=3)
int count=0;
要点:
做{
System.out.println(“输入您的姓名”);
字符串名称1=kb.nextLine();
布尔b=真;
for(int i=0;i 而(!b | | count您可以这样做:
int count = 0;
point:
do {
System.out.println("Enter your name");
String name1 = kb.nextLine();
boolean b = true;
for (int i = 0; i < name.length; i++) {
if (name[i].equals(name1)) {
System.out.println("you are verified you may use the lift");
b = false;
break point;// to stop loop checking names
}
}
if (b) {
count++;
System.out.println("Invalid entry 2 attempts remaining, try again");
}
while(!b || count <=3)
int count=0;
要点:
做{
System.out.println(“输入您的姓名”);
字符串名称1=kb.nextLine();
布尔b=真;
for(int i=0;i
Enter your name
loknath
Not a verified user try again!
Enter your name
chiku
Not a verified user try again!
Enter your name
zerr
you are verified you may use the lift
Done
输出
Enter your name
loknath
Not a verified user try again!
Enter your name
chiku
Not a verified user try again!
Enter your name
zerr
you are verified you may use the lift
Done
使用以下方法。好的方面是它是一个干净而健壮的解决方案
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class AccessPoint
{
private Scanner scanner;
private List<String> usernames;
public AccessPoint()
{
scanner = new Scanner(System.in);
usernames = Arrays.asList("Barry", "Matty", "Olly", "Joey");
if (tryAccessForTimes(3))
{
allowAccess();
}
else
{
denyAccess();
}
scanner.close();
}
public static void main(String[] args)
{
new AccessPoint();
}
private boolean tryAccessForTimes(int times)
{
boolean accessAllowed = false;
for (int tryIndex = 1; tryIndex <= times && !accessAllowed; tryIndex++)
{
String userInput = getUserName();
for (String userName : usernames)
{
if (userName.equals(userInput))
{
accessAllowed = true;
break;
}
}
if (!accessAllowed)
{
printNumberOfTriesLeft(times, tryIndex);
}
}
return accessAllowed;
}
private void printNumberOfTriesLeft(int times, int tryIndex)
{
int triesLeft = times - tryIndex;
if (triesLeft != 0)
{
System.out.println("You have " + triesLeft
+ (triesLeft == 1 ? " try" : " tries") + " left.");
}
}
private String getUserName()
{
System.out.print("Enter Username: ");
return scanner.nextLine();
}
private void allowAccess()
{
System.out.println("Access Granted. Allowed to use lift.");
}
private void denyAccess()
{
System.out.println("Access Denied.");
}
}
导入java.util.array;
导入java.util.List;
导入java.util.Scanner;
公共类访问点
{
私人扫描仪;
私人名单用户名;
公共访问点()
{
扫描仪=新扫描仪(System.in);
usernames=Arrays.asList(“Barry”、“Matty”、“Olly”、“Joey”);
if(tryAccessForTimes(3))
{
allowAccess();
}
其他的
{
denyAccess();
}
scanner.close();
}
公共静态void main(字符串[]args)
{
新访问点();
}
专用布尔值tryAccessForTimes(整数次)
{
布尔accessAllowed=false;
对于(int-tryIndex=1;tryIndex使用以下方法。好的是它是一个干净而健壮的解决方案
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class AccessPoint
{
private Scanner scanner;
private List<String> usernames;
public AccessPoint()
{
scanner = new Scanner(System.in);
usernames = Arrays.asList("Barry", "Matty", "Olly", "Joey");
if (tryAccessForTimes(3))
{
allowAccess();
}
else
{
denyAccess();
}
scanner.close();
}
public static void main(String[] args)
{
new AccessPoint();
}
private boolean tryAccessForTimes(int times)
{
boolean accessAllowed = false;
for (int tryIndex = 1; tryIndex <= times && !accessAllowed; tryIndex++)
{
String userInput = getUserName();
for (String userName : usernames)
{
if (userName.equals(userInput))
{
accessAllowed = true;
break;
}
}
if (!accessAllowed)
{
printNumberOfTriesLeft(times, tryIndex);
}
}
return accessAllowed;
}
private void printNumberOfTriesLeft(int times, int tryIndex)
{
int triesLeft = times - tryIndex;
if (triesLeft != 0)
{
System.out.println("You have " + triesLeft
+ (triesLeft == 1 ? " try" : " tries") + " left.");
}
}
private String getUserName()
{
System.out.print("Enter Username: ");
return scanner.nextLine();
}
private void allowAccess()
{
System.out.println("Access Granted. Allowed to use lift.");
}
private void denyAccess()
{
System.out.println("Access Denied.");
}
}
导入java.util.array;
导入java.util.List;
导入java.util.Scanner;
公共类访问点
{
私人扫描仪;
私人名单用户名;
公共访问点()
{
扫描仪=新扫描仪(System.in);
usernames=Arrays.asList(“Barry”、“Matty”、“Olly”、“Joey”);
if(tryAccessForTimes(3))
{
allowAccess();
}
其他的
{
denyAccess();
}
scanner.close();
}
公共静态void main(字符串[]args)
{
新访问点();
}
专用布尔值tryAccessForTimes(整数次)
{
布尔accessAllowed=false;
对于(int-tryIndex=1;tryIndex-man,几分钟前,您提出了一个问题。我给了您一个答案,现在,您将我的代码粘贴到这里,然后再次提问……您需要开始接受答案。@Salah或者我们应该停止给出答案;)@女孩女孩我想你是对的,我们正在做他的作业:S.man,几分钟前,你问了一个问题。我给了你一个答案,现在,你把我的代码贴在这里,然后你又问了一次……你需要开始接受答案。@Salah或者我们应该停止回答;)@GirlyGirl我想你是对的,我们正在做他的任务:S.binarySearch
只有在name
数组中的元素被排序时才会工作。问题是没有这样的约束。如果数组中存在元素,则返回+ve值,否则返回-ve@loknath:将名称数组设置为:String[]name={“olly”、“matty”、“joey”、“barry”};
或任何其他未排序的名称列表,则基于binarySearch的程序将无法正常运行。@loknath:请再次阅读我的上述评论。如果将名称数组设为未排序,则binarySearch方法将失败