简体   繁体   English

最后在for循环中打印一次语句?

[英]Printing statement in for loop once at the end?

I have a for loop with an if statement inside and I want the statement to print after all the iterations have occurred but it prints after each iteration for which the if statement applies. 我有一个带if语句的for循环,我希望在所有迭代均发生后打印该语句,但在每次if语句适用的迭代之后打印。 I want the program to go through the iterations and whatever names start with the letter inputted, I want them to print with the statement "The last names that start with " + letter + " are " + students[i]" only once at the end. Is there a way to do that? 我希望程序进行迭代,无论名称以输入的字母开头,我都希望它们以以下语句打印:“以“ + letter +”开头的姓是“ + students [i]”,结束,有办法吗?

    import java.util.Scanner;

public class ClassNames {

   public static void main(String[] args) {

   Scanner keyboard = new Scanner(System.in);

   int numberOfStudents;
   int i;

   System.out.print("Please enter the total number of students: ");
   numberOfStudents = Integer.valueOf(keyboard.next());

   String students[] = new String[numberOfStudents];

   i = 0;
   while(i < numberOfStudents) {
      System.out.print("Enter the last name: ");
      students[i] = keyboard.next();
      i++;
   }   

   String findName;
   System.out.print("Enter a name to search for: ");
   findName = keyboard.next();


   for(i = 0; i < numberOfStudents; i++) {

      if(students[i].contains(findName)) {
      System.out.println(findName + " is at index " + i);
      }
       else {
      System.out.println("That name does not exist ");
      break;
      }
   } 

   String letter;

   System.out.print("Enter a letter to search on: ");
   letter = keyboard.next();

   for(i = 0; i < numberOfStudents; i++) {

      if(students[i].startsWith(letter)) {
         System.out.println("The last names that start with " + letter + " are " + students[i]);

      }
   }

   }
}  

You can just print "The last names that start with " + letter + " are " before the for loop and use the loop to print the student names. 您可以在for循环之前打印"The last names that start with " + letter + " are "然后使用循环打印student姓名。

System.out.println("The last names that start with " + letter + " are ");
for(i = 0; i < numberOfStudents; i++) {
      if(students[i].startsWith(letter)) {
         System.out.print(students[i] + ","); // Comma to separate student names
      }
   }

Hope this helps and what you were looking for. 希望这对您有所帮助。

Perhaps something like the following is what you want: 您可能想要以下内容:

System.out.print("The last names that start with " + letter + " are ");
for (i = 0; i < numberOfStudents; i++) {
   if (students[i].startsWith(letter)) {
      System.out.print(students[i] + ", ");
   }
}
System.out.println();

This will leave with you a trailing comma, but might get you on the road to where you want to be. 这会给您留下逗号结尾,但可能会让您踏上想要成为的道路。

You are printing out the output before you know the result. 您将在知道结果之前打印输出。

Get the result, and then generate the output. 获取结果,然后生成输出。 We need to store which names were a match, and display them with one print out. 我们需要存储匹配的名称,并打印出它们以显示它们。 An ArrayList works nicely for this, since we don't know how many names are a match until we are finished looping, which works better than creating an array with a set size that could be too large or too small. ArrayList可以很好地实现此目的,因为在完成循环之前,我们不知道有多少个名字匹配,这比创建一个数组的大小可能会太大或太小要好。

List<String> matchesFound = new ArrayList<>();
for (i = 0; i < numberOfStudents; i++) {
    if (students[i].startsWith(letter)) {
        matchesFound.add(students[i]);
    }
}

if (!matchesFound.isEmpty()) {
    System.out.printf("The last name(s) that starts with %s %s %s.", letter,
         matchesFound.size() > 1 ? "are" : "is", String.join(", ", matchesFound));
}

Let me try to answer your question as much as I understand your question, You have two kinds of search: with name and with the first letter. 让我尝试尽我所能回答您的问题,您有两种搜索方式:名称搜索和首字母搜索。

First of all, if you want to perform a string equality check, better to use .equals or .equals vs .equalsIgnoreCase such that 首先,如果要执行字符串相等性检查,最好使用.equals或.equals与.equalsIgnoreCase,这样

if(students[i].equals(findName))

I will focus on your question and answer for just letter search part. 我将仅针对字母搜索部分关注您的问题和答案。 (as search name part is also problematic as it will print else section for each iteration as long as it can not find the name) (因为搜索名称部分也是有问题的,因为只要找不到名称,它将在每次迭代中打印else部分)

So, if you want to print all name starting with letter 'x' (let's assume letter is x) then first, you need to iterate all your names first and save whenever you find a match. 因此,如果要打印以字母“ x”开头的所有名称(假设字母为x),则首先打印所有名称,并在找到匹配项时进行保存。 You can save those name by using different collections. 您可以使用其他集合来保存这些名称。 You can use ArrayList for this specific purpose. 您可以将ArrayList用于此特定目的。

  String letter;
   ArrayList<String> matches = new ArraList<String>();
   System.out.print("Enter a letter to search on: ");
   letter = keyboard.next();

   for(i = 0; i < numberOfStudents; i++) {

      if(students[i].startsWith(letter)) {
         matches.add(students[i]);

      }
   }
String names = "";
if(!matches.isEmpty)
{
  for(String name : matches)
  { 
    names += name + " "; 
 }
 System.out.println("The last names that start with " + letter + " are " + 
    names);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM