簡體   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