Java:如何从另一个方法输出用户输入(多个字符串/ Ints)

时间:2018-03-25 00:42:02

标签: java methods

我是Java的新手,也是这个网站的新手,所以请放轻松我。 :)

我正在尝试编写一个程序,它会询问用户几条信息。收集用户的信息后,我需要调用另一种方法将信息打印回控制台屏幕。

我遇到的问题是我将所有信息重新打印到屏幕上的最终方法是破坏,我不知道从哪里开始修复它。我在编写和调用最终方法(printToScreen)之前运行了我的代码,程序按预期工作,没有错误或异常。代码如下,我真的很感激任何帮助。

import java.util.*;

public class Program5 {

//Create constants
public static final int TOTAL_SEATS = 50;
public static Scanner console = new Scanner(System.in);

public static void main (String[] args) {

  //Create variables and objects
  String courseCode, courseName;
  int studentsReg;
  int openSeats;

  //Call method to print three lines of 55 asterisks to screen
  screenBreak();

  //Call method to prompt the user for input
  promptCodeName();

  //Call method to ask for pre-requisites
  getPrereqs();

  //Call method to ask how many students are currently registered
  numStudents();

  screenBreak();

  printToScreen();

}//Close the main method

public static void screenBreak() {
  for (int i = 1; i <= 3; i++) {
     for (int j = 1; j <= 55; j++) {
        System.out.print("*");
     } //Close inner for loop
     System.out.println();
  } //Close outer for loop
} //Close screenBreak method

public static void promptCodeName() {
  String courseCode, courseName;

  System.out.print("Please enter the course code: ");
  courseCode = console.nextLine();

  System.out.print("Please enter the course name: ");
  courseName = console.nextLine();
}//close promptCodeName method

public static void getPrereqs() { 
  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 = console.nextLine();

  }//Close for loop
}//Close getPrereqs method

public static void numStudents() {
  int studentsReg;
  System.out.print("How many students are currently registered for this course? ");
  studentsReg = console.nextInt();
}//Close numStudents method

public static int calcAvail (int seatsTaken) {
  return (TOTAL_SEATS - seatsTaken);
}//Close calcAvail method

public static void printToScreen () {
  String courseCode = console.nextLine;
  String courseName = console.nextLine;
  numPrereqs = console.nextLine;
  int studentsReg = console.nextInt;

  String listPrereq = console.nextLine;

  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("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 printToScreen method
}//Close Program5 class

2 个答案:

答案 0 :(得分:2)

你的问题是因为courseCode,courseName是局部变量,这意味着它们只在这个promptCodeName(例如ofc)方法中可用。

如果您想在变量中存储来自用户的信息,您应该在您的类中创建字段并在其中存储信息用户。

所以在类的开头创建字段(e.q。private String courseCode;) 然后,方法看起来应该是这样的:

public static void promptCodeName() {
  String courseCode, courseName;

  System.out.print("Please enter the course code: ");
  courseCode = console.nextLine();
  this.courseCode = courseCode;

  System.out.print("Please enter the course name: ");
  courseName = console.nextLine();
  this.courseName = courseCode;
}

详细了解“这个”这个词,我想它会让你理解这一点。 :)

答案 1 :(得分:2)

不要忘记变量的范围。例如,在方法promptCodeName()中,您声明局部变量courseCode和courseName并将它们分配给控制台的输入,但您从不使用此变量(它们的值)。因此,您必须声明类变量(与TOTAL_SEATS和扫描程序相同)并为它们分配相应的值或使用main方法中的局部变量,但在这种情况下,您必须将它们作为方法参数发送到各自的方法。