简体   繁体   English

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

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

I'm brand new to Java, and also new to this site, so please go easy on me. 我是Java的新手,也是本网站的新手,所以请放轻松。 :) :)

I'm attempting to write a program which will ask the user for several pieces of information. 我正在尝试编写一个程序,该程序将询问用户一些信息。 After gathering the information from the user, I then need to call another method to print the information back to the console screen. 从用户那里收集信息之后,我需要调用另一种方法来将信息打印回控制台屏幕。

The problem that I'm having is that my final method to reprint all of the information to the screen is a wreck, and I don't know where to start to fix it. 我遇到的问题是,将所有信息重新打印到屏幕上的最终方法是残破的,而且我不知道从哪里开始修复它。 I ran my code prior to writing and calling the final method (printToScreen), and the program worked as expected with no errors or anomalies. 在编写和调用最终方法(printToScreen)之前,我先运行代码,程序正常运行,没有错误或异常。 Code is below, and I really REALLY appreciate any assistance. 代码在下面,我非常感谢您的协助。

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

Your problem is becouse courseCode, courseName are local variables which means they are only available in this promptCodeName (for example ofc) method. 您的问题是因为courseCode,courseName是局部变量,这意味着它们仅在该hintCodeName(例如ofc)方法中可用。

If You want to store informations from user in variables, u should create fields in Your class and store informations user in it. 如果您想将来自用户的信息存储在变量中,则您应在您的类中创建字段并在其中存储用户信息。

So create fields at the beginning of class (eq private String courseCode;) and then, method should looks like that: 因此,在类的开头创建字段(例如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;
}

Read more about "this" word, i think it will let You understand this. 阅读有关“这个”一词的更多信息,我认为它将使您理解这一点。 :) :)

Don't forget about scope of your variables. 不要忘记变量的范围。 For example in method promptCodeName() you declare local variables courseCode and courseName and assign them to input from console, but you never use this variables (their values). 例如,在方法hintCodeName()中,您声明了局部变量courseCode和courseName并将它们分配给控制台的输入,但是您从不使用此变量(它们的值)。 So you have to declare class variables (in the same way as TOTAL_SEATS and scanner) and assign respective values to them or use your local variables from main method, but in this case you have to send them to respective methods as method parameters. 因此,您必须声明类变量(以与TOTAL_SEATS和scanner相同的方式)并为其分配各自的值,或者使用main方法中的局部变量,但是在这种情况下,您必须将它们作为方法参数发送给各自的方法。

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

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