繁体   English   中英

如何创建一个最后显示所有名称的程序?

[英]How can I create a program that displays all the names at the end?

我不知道为什么我不能理解这种类型的概念。 我必须让它尽可能简单。 我正在尝试获取 class 中的学生列表,然后在用户输入 2 时在最后显示它们。我已经切换了几次,但每次都在同一点结束。 我尝试为 1 和 2 的答案做 if 语句,但仍然没有得到任何结果。

谢谢大家。

package peopleinclass;
import java.util.Scanner;

public class PeopleinClass {
    static Scanner get=new Scanner(System.in);
    static String yourName;
    static String studentNames = "";
    static int answer = 1;
    
    public static void main(String[] args) 
    {
        getNames();

        while(true)
        {
            if(answer == 1  )
            {
                System.out.println("Would you like to add another student? 1 for yes 2 for no ");
                answer=get.nextInt();   
                System.out.println("Please enter another students name: ");
                yourName = get.nextLine();  
                get.nextLine();
                yourName+;
                break;  
            }
        }
        Display();      
    }

    public static String getNames()
    {
        System.out.println("Please enter your students name: ");
        yourName = get.nextLine();

        return yourName;
    }

    public static void Display()
    {
        System.out.println("Below are the students in the class: ");
        System.out.println(yourName);
    }
}
  1. 您发布的代码无法编译。 yourName+; 无法编译
  2. 您有一个 String studentNames但您从未为其分配任何值。
  3. 您正在读取getNames()方法中的输入,但您从不使用返回值。

我建议进行以下更改:

  1. getNames()方法的返回值分配给studentNames变量。
  2. 删除您的yourName+; 线。
  3. 在您的Display()方法中打印studentNames而不是yourName
  4. 你有一个get.nextLine()并且你没有使用它的价值。 还将返回值与您的studentNames变量连接起来。

对我来说,这看起来像是一项学校作业,所以我没有发布解决方案,只是想给你一些提示。

看起来你可能在这个问题和你对编程的理解上有点挣扎,所以我在下面提供了一个带有详细注释的解决方案。 我敦促您阅读整篇文章以及我在代码下方提供的链接中的内容。

如果这是一个学校/学院/大学项目,那么不要在不了解它的工作原理的情况下采用解决方案并使用它,因为您将在整个学习过程中继续挣扎。 我提供了这段代码来尝试帮助您理解问题以及如何解决它。 如果您需要任何进一步的信息,请在下面的评论部分回复。

package peopleinclass;

import java.util.Scanner;
import java.util.ArrayList;

class PeopleinClass {

    private static Scanner get = new Scanner(System.in);
    private static int answer = 1;
    private static String nextName = null;
    
    public static void main(String[] args) 
    {
      //Initialise an ArrayList of type String.
      ArrayList<String> listOfNames = new ArrayList<String>();

      //Request the first student name and store the value in 
      //the nextName variable which is initialised at the 
      //top of this class.
      nextName = requestFirstStudentName();

      //Add the value of nextName in to the ArrayList.
      listOfNames.add(nextName);

      //Create a do...while loop which executes all of the code 
      //inside the bracers at least once and then continues to 
      //execute the code based on the while (...) condition at 
      //the end of the bracers.
      do {

          System.out.println("Would you like to add another student? 1 for yes 2 for no ");

          //Prompt the user to enter a value which we assume is of type 
          //int. If the value the user enters here is not of type int 
          //and is a String instead, the program will crash. Type 
          //checking should be done here to make sure it is an 
          //int but for simplicity I have left type checking out.
          answer = get.nextInt();

          //Clear any tokens that are left in the scanner.
          get.nextLine();

          //If the answer given when prompted for an int (above) 
          //is equal to 1.
          if(answer == 1)
          {
            System.out.println("Please enter another students name: ");

            //Prompt the user to enter another name.
            nextName = get.nextLine();

            //Add another entry to our ArrayList of type String.
            listOfNames.add(nextName);
          }

      //While the user has given the answer of 1 (above when asked for int) 
      //go back to the top of the do...while loop and repeat the process 
      //again.
      } while (answer == 1);

      //Call the display(...) method, which accepts an ArrayList 
      //of type String and is defined in this class further 
      //down.
      display(listOfNames);
    }

    public static String requestFirstStudentName()
    {
        System.out.println("Please enter your students name: ");

        //Prompt the user to enter a name.
        nextName = get.nextLine();

        //Return the name back to where it was invoked.
        return nextName;
    }

    public static void display(ArrayList<String> listOfNames)
    {
        System.out.println("Below are the students in the class: ");

        //Create a for loop, initialising a variable of type 
        //int and set it to 0. The rest of the for loop can 
        //be read as - while i is less than the size of 
        //the ArrayList of type String, print the next 
        //String stored in the ArrayList and increment i.
        for(int i = 0; i < listOfNames.size(); i++)
        {
          System.out.println(listOfNames.get(i));
        }
    }
}

As you can see, I made use of the ArrayList data structure provided in the java.util.* package that is provided with the Java language. 在此处阅读有关ArrayList及其工作原理的更多信息。

您还将看到我使用了do...while循环。 do...while循环以多种不同的语言提供,但不是很常用,但它适合您所呈现的场景。 此处查找有关do...while循环的更多信息。

我还使用了一个for循环来遍历display()方法中的ArrayList 在此处了解有关 for 循环的更多信息。

在评论中,我谈到了从Scanner class 读取nextInt()以及应该如何进行类型检查。 我这样说的原因是因为您依赖于用户输入数字(用户通常是白痴并决定输入字符串)。

如果输入了String而不是int ,那么您将得到java.util.InputMismatchException所以这里最好的方法是请求nextLine()并尝试将该行转换为int

String转换为int可以在这里找到。 您将在该链接上找到答案显示使用try...catch块,如果尝试从String转换为int不起作用,它将 go 到您可以将int设置为的 catch 块默认值。

暂无
暂无

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

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