繁体   English   中英

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

[英]Java: How to output user input (multiple Strings/Ints) from another method

我是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

您的问题是因为courseCode,courseName是局部变量,这意味着它们仅在该hintCodeName(例如ofc)方法中可用。

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

因此,在类的开头创建字段(例如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;
}

阅读有关“这个”一词的更多信息,我认为它将使您理解这一点。 :)

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

暂无
暂无

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

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