输入一定范围内的数字,然后让系统提示我为输入的那个数字输入姓名?
Enter a number within a certain range, and then get the system to prompt me to input names for that number that has been entered?
我正在尝试输入 1-10 之间的数字,如果连续 3 次输入超出此范围的数字,系统将终止。如果号码在范围内,则应提示用户输入姓名,该姓名应与最初添加的号码相匹配。 (因此,如果最初输入了 3 个,系统将允许添加 3 个名称)。你能告诉我哪里出错了吗?
import java.util.Scanner;
public class Lab7{
public static void main(String []args){
Scanner in = new Scanner(System.in);
int result;
int count = 0;
do {
System.out.println("Enter number of students (Must be between 1-10)");
result = in.nextInt();
} while(result < 0 || result > 10);
{
System.out.println("Thank you, Please enter the names for the "+ result + " Students");
}
try (Scanner input = new Scanner(System.in)) {
System.out.print("Enter Full Name: ");
String s1 = input.next();
System.out.print("The students name is" + " " + s1);
}
while(!(result < 0 || result > 10));
{
System.out.println("wrong input");
count = count + 1;
if (count == 3)
{
System.out.println("The maximum number of attempts exceeded");
System.exit(count);
}
}
}
}
您需要将整个代码放在 while 循环中,其条件应取决于不正确尝试的次数 (count)。由于您没有提到如果用户输入正确的输入(比如 3)需要做什么,我假设它将输入 3 个名称并结束(如果您想继续使用名称,请删除提到的中断条件在我的代码中。
public class Lab7 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int result;
int count = 0;
while (count < 3) {
System.out.println("Enter number of students (Must be between 1-10)");
result = in.nextInt();
// in.next();
if (result < 0 || result > 10) {
count++;
} else {
System.out.println("Thank you, Please enter the names for the " + result + " Students");
int nameCount = 0;
while (nameCount < result) {
System.out.println("Enter Full Name: ");
String s1 = in.next();
System.out.println("The students name is" + " " + s1);
nameCount++;
}
break;
}
}
}
}
输出为:
Enter number of students (Must be between 1-10)
2
Thank you, Please enter the names for the 2 Students
Enter Full Name:
john
The students name is john
Enter Full Name:
jacob
The students name is jacob
我正在尝试输入 1-10 之间的数字,如果连续 3 次输入超出此范围的数字,系统将终止。如果号码在范围内,则应提示用户输入姓名,该姓名应与最初添加的号码相匹配。 (因此,如果最初输入了 3 个,系统将允许添加 3 个名称)。你能告诉我哪里出错了吗?
import java.util.Scanner;
public class Lab7{
public static void main(String []args){
Scanner in = new Scanner(System.in);
int result;
int count = 0;
do {
System.out.println("Enter number of students (Must be between 1-10)");
result = in.nextInt();
} while(result < 0 || result > 10);
{
System.out.println("Thank you, Please enter the names for the "+ result + " Students");
}
try (Scanner input = new Scanner(System.in)) {
System.out.print("Enter Full Name: ");
String s1 = input.next();
System.out.print("The students name is" + " " + s1);
}
while(!(result < 0 || result > 10));
{
System.out.println("wrong input");
count = count + 1;
if (count == 3)
{
System.out.println("The maximum number of attempts exceeded");
System.exit(count);
}
}
}
}
您需要将整个代码放在 while 循环中,其条件应取决于不正确尝试的次数 (count)。由于您没有提到如果用户输入正确的输入(比如 3)需要做什么,我假设它将输入 3 个名称并结束(如果您想继续使用名称,请删除提到的中断条件在我的代码中。
public class Lab7 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int result;
int count = 0;
while (count < 3) {
System.out.println("Enter number of students (Must be between 1-10)");
result = in.nextInt();
// in.next();
if (result < 0 || result > 10) {
count++;
} else {
System.out.println("Thank you, Please enter the names for the " + result + " Students");
int nameCount = 0;
while (nameCount < result) {
System.out.println("Enter Full Name: ");
String s1 = in.next();
System.out.println("The students name is" + " " + s1);
nameCount++;
}
break;
}
}
}
}
输出为:
Enter number of students (Must be between 1-10)
2
Thank you, Please enter the names for the 2 Students
Enter Full Name:
john
The students name is john
Enter Full Name:
jacob
The students name is jacob