使用累积算法连接用户输入的字符串

时间:2018-03-30 23:19:22

标签: java string object output concatenation

我正在尝试编写一个程序,该程序将询问用户特定类具有多少先决条件,请求每个先决条件的名称,然后在一行中输出先决条件的列表

我的程序正在运行而没有返回任何错误;但是,当它输出先决条件列表时,它只重复用户输入的最后一个,而不是列出输入的所有先决条件。我相信这是因为每次用户输入新的先决条件时,对象的价值都会发生变化。但是,我无法弄清楚如何保存每个值并将它们连接在一起以便输出。

以下代码是我用来收集用户的先决条件

 public static void getPrereqs() { 
 System.out.print("How many pre-requisites does the course have? ");
 numPrereqs = console.nextInt();

 console.nextLine();

 for (int i = 1; i <= numPrereqs; i++) {
     System.out.print("List Pre-requisite #" + i + "? ");
     listPrereq = console.nextLine();
  }//Close for loop
}//Close getPrereqs method

这是我用来输出列表的代码(还包括调用其他一些信息,但这些信息输出正确)

  public static void printToScreen () {
  System.out.println(courseCode + ": " + courseName);
  System.out.print("Pre-requisites: ");
  for (int i = 1; i <= numPrereqs; i++) {
     System.out.print(listPrereq + ", ");
  }//Close for loop
  System.out.println();
  System.out.println("Total number of seats = " + TOTAL_SEATS);
  System.out.println("Number of students currently registered = " + studentsReg);

  openSeats = calcAvail(studentsReg);

  System.out.println("Number of seats available = " + openSeats);
  if (openSeats >= 5) {
     System.out.println ("There are a number of seats available.");
  }//Close if loop
  else {
     if (openSeats <= 0) {
        System.out.println ("No seats remaining.");
     }//Close if loop
     else {
           System.out.println ("Seats are almost gone!");
     }//Close else
  }//Close else statement
}//Close printToScreen method

我见过一些讨论数组列表的线程;但是,我对这是什么并不熟悉,并且此时不习惯使用。有没有办法使用累积算法获得我正在寻找的结果?

2 个答案:

答案 0 :(得分:1)

您目前每次迭代都会覆盖String变量listPrereq。您需要存储到数组(首选),或连接为一个大字符串。

如果您不喜欢Lists,那么数组应该没问题。

而不是将listPrereq声明为String,而不是声明为数组:

String[] listPrereq;

然后你需要创建足够大的空间来存储每个先决条件:

numPrereqs = console.nextInt();     
listPrereq = new String[numPrereqs];

然后存储值:

for (int i = 0; i < numPrereqs; i++) {
    System.out.print("List Pre-requisite #" + (i+1) + "? ");
    listPrereq[i] = console.nextLine();
} // Close for loop

然后打印:

System.out.print("Pre-requisites: ");
for (int i = 0; i < numPrereqs; i++) {
    System.out.print(listPrereq[i]);
    if (i != numPrereqs - 1)
        System.out.println(",");
}//Close for loop

你可以将一个大字符串连接在一起而不是使用数组(这是你的偏好吗?),但是你失去了之后能够轻松地单独引用每个先决条件的能力。

编辑:如何创建大字符串

String listPrereq = "";

for (int i = 0; i < numPrereqs; i++) {
    System.out.print("List Pre-requisite #" + (i+1) + "? ");
    listPrereq += console.nextLine();
    if (i != numPrereqs - 1)
        listPrereq += ",";
} // Close for loop

// To print:
System.out.println(listPrereq);

答案 1 :(得分:0)

import java.util.*;

public class CourseDetails {

    private static Scanner console;

    public static void getPrereqs() {
        console = new Scanner(System.in);
        int numPrereqs;
        String listPrereq = "";
        System.out.print("How many pre-requisites does the course have? ");
        numPrereqs = console.nextInt();

        console.nextLine();

        for (int i = 1; i <= numPrereqs; i++) {
            System.out.print("List Pre-requisite #" + i + "? ");
            listPrereq = listPrereq + i + " " + console.nextLine();
            listPrereq = listPrereq + "\n";
        } // Close for loop
        System.out.println("Pre-requisites: ");
        System.out.println(listPrereq);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        getPrereqs();
    }

}

第二种方式:

import java.util.Scanner;

public class CourseDetailsA {

    private static Scanner console;

    public static void getPrereqs() {

        console = new Scanner(System.in);
        int numPrereqs;
        int j = 1;
        String[] listPrereq;
        System.out.print("How many pre-requisites does the course have? ");
        numPrereqs = console.nextInt();
        listPrereq = new String[numPrereqs];
        console.nextLine();

        for (int i = 0; i <= numPrereqs - 1; i++) {
            System.out.print("List Pre-requisite #" + j + "? ");
            listPrereq[i] = j + " " + console.nextLine() + "\n";
            j++;
        } // Close for loop

        System.out.println("Pre-requisites: ");
        for (int i = 0; i <= numPrereqs - 1; i++) {
            System.out.print(listPrereq[i]);
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        getPrereqs();
    }
}