作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建的程序没有从 ClassRosterController 类中的 run 方法打印菜单功能。我在 Netbeans 中运行了调试器,但无法确定问题所在。任何帮助将非常感激。
package classroster;
import ClassRosterController.ClassRosterController;
public class App {
public static void main(String[] args) {
ClassRosterController controller = new ClassRosterController();
controller.run();
}
}
package ClassRosterController;
import ClassRoster.dao.ClassRosterDao;
import ClassRoster.dao.ClassRosterDaoImpl;
import ClassRoster.dto.Student;
import ClassRoster.ui.ClassRosterView;
import ClassRoster.ui.UserIO;
import ClassRoster.ui.UserIOConsoleImpl;
import java.util.List;
public class ClassRosterController {
ClassRosterView view = new ClassRosterView();
ClassRosterDao dao = new ClassRosterDaoImpl();
UserIO io = new UserIOConsoleImpl();
public void run() {
boolean keepGoing = true;
int menuSelection = 0;
while (keepGoing) {
menuSelection = getMenuSelection();
io.print("Main Menu");
io.print("1. List Student IDs");
io.print("2. Create New Student");
io.print("3. View a Student");
io.print("4. Remove a Student");
io.print("5. Exit");
menuSelection = io.readInt("Please select from the above choices.", 1, 5);
switch (menuSelection) {
case 1:
listStudents();
break;
case 2:
createStudent();
break;
case 3:
io.print("VIEW STUDENT");
break;
case 4:
io.print("REMOVE STUDENT");
break;
case 5:
keepGoing = false;
break;
default:
io.print("UNKNOWN COMMAND");
}
}
io.print("GOOD BYE");
}
private void listStudents(){
List<Student> studentList = dao.getAllStudents();
view.displayStudentList(studentList);
}
private int getMenuSelection() {
return view.printMenuAndGetSelection();
}
private void createStudent(){
view.displayCreatesStudentBanner();
Student newStudent = view.getNewStudentInfo();
dao.addStudent(newStudent.getStudentId(), newStudent);
view.displayCreateSuccessBanner();
}
}
package ClassRoster.ui;
import ClassRoster.dto.Student;
import java.util.List;
public class ClassRosterView {
private UserIO io = new UserIOConsoleImpl();
public int printMenuAndGetSelection() {
io.print("");
io.print("");
io.print("");
io.print("");
io.print("");
io.print("");
return io.readInt("Please select from the above choices.", 1, 5);
}
public Student getNewStudentInfo(){
String studentId = io.readString("Please enter Student ID");
String firstName = io.readString("Please enter First Name");
String lastName = io.readString("Please enter Last Name");
String cohort = io.readString("Please enter Cohort");
Student currentStudent = new Student(studentId);
currentStudent.setFirstName(firstName);
currentStudent.setLastName(lastName);
currentStudent.setCohort(cohort);
return currentStudent;
}
public void displayCreatesStudentBanner(){
io.print("=== Create Student ===");
}
public void displayCreateSuccessBanner(){
io.readString("Student successfully created. Please hit enter to continue.");
}
public void displayStudentList(List<Student> studentList){
for (Student currentStudent: studentList){
io.print(currentStudent.getStudentId() + ": "
+currentStudent.getFirstName() + " "
+currentStudent.getLastName());
}
io.readString("Please hit enter to continue.");
}
public void displayAllBanner(){
io.print("=== Display All Students ===");
}
}
package ClassRoster.ui;
public interface UserIO {
void print(String msg);
double readDouble(String prompt);
double readDouble(String prompt, double min, double max);
float readFloat(String prompt);
float readFloat(String prompt, float min, float max);
int readInt(String prompt);
int readInt(String prompt, int min, int max);
long readLong(String prompt);
long readLong(String prompt, long min, long max);
String readString(String prompt);
}
package ClassRoster.ui;
import java.util.Scanner;
public class UserIOConsoleImpl implements UserIO {
Scanner userInput = new Scanner(System.in);
@Override
public void print(String msg) {
System.out.println(msg);
}
@Override
public double readDouble(String prompt) {
System.out.println(prompt);
double inputNumber = userInput.nextDouble();
return inputNumber;
}
@Override
public double readDouble(String prompt, double min, double max) {
System.out.println(prompt);
double inputNumber = userInput.nextDouble();
boolean loop = true;
do{
if(inputNumber < min || inputNumber > max){
System.out.println("Please enter a number between " + min +" and " + max);
}
else {
loop = false;
}
}while(loop);
return inputNumber;
}
@Override
public float readFloat(String prompt) {
System.out.println(prompt);
float inputNumber = userInput.nextFloat();
return inputNumber;
}
@Override
public float readFloat(String prompt, float min, float max) {
System.out.println(prompt);
float inputNumber = userInput.nextFloat();
boolean loop = true;
do{
if(inputNumber < min || inputNumber > max){
System.out.println("Please enter a number between " + min +" and " + max);
}
else {
loop = false;
}
}while(loop);
return inputNumber;
}
@Override
public int readInt(String prompt) {
System.out.println(prompt);
int inputNumber = userInput.nextInt();
return inputNumber;
}
@Override
public int readInt(String prompt, int min, int max) {
System.out.println(prompt);
int inputNumber = userInput.nextInt();
boolean loop = true;
do{
if(inputNumber < min || inputNumber > max){
System.out.println("Please enter a number between " + min +" and " + max);
}
else {
loop = false;
}
}while(loop);
return inputNumber;
}
@Override
public long readLong(String prompt) {
System.out.println(prompt);
long inputNumber = userInput.nextLong();
return inputNumber;
}
@Override
public long readLong(String prompt, long min, long max) {
System.out.println(prompt);
long inputNumber = userInput.nextLong();
boolean loop = true;
do{
if(inputNumber < min || inputNumber > max){
System.out.println("Please enter a number between " + min +" and " + max);
}
else {
loop = false;
}
}while(loop);
return inputNumber;
}
@Override
public String readString(String prompt) {
System.out.println(prompt);
String inputString = userInput.nextLine();
return inputString;
}
}
package ClassRoster.dto;
public class Student {
private String firstName;
private String lastName;
private String studentId;
private String cohort;
public Student(String studentId){
this.studentId = studentId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getStudentId() {
return studentId;
}
public String getCohort() {
return cohort;
}
public void setCohort(String cohort) {
this.cohort = cohort;
}
}
package ClassRoster.dao;
import ClassRoster.dto.Student;
import java.util.List;
public interface ClassRosterDao {
Student addStudent(String studentId, Student student);
List<Student> getAllStudents();
Student getStudent(String studentId);
Student removeStudent(String studentId);
}
package ClassRoster.dao;
import ClassRoster.dto.Student;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ClassRosterDaoImpl implements ClassRosterDao {
private Map<String, Student> students = new HashMap<>();
@Override
public Student addStudent(String studentId, Student student) {
Student newStudent = students.put(studentId, student);
return newStudent;
}
@Override
public List<Student> getAllStudents() {
return new ArrayList<Student>(students.values());
}
@Override
public Student getStudent(String studentId) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public Student removeStudent(String studentId) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
最佳答案
首先,readInt()
中的 do-while 循环已损坏。 while(loop = true)
会将循环无限期地设置为 true
。将其替换为 while(loop)
。其次,如果 inputNumber
不在范围内会发生什么?您永远不会在该循环中再次提示用户输入!因此,如果他们第一次没有正确的输入,该循环将无限期地运行。
第三,为什么在循环之后打印提示符。。您不想提前提示用户吗?
关于Java 应用程序未将菜单打印到屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41294658/
我是一名优秀的程序员,十分优秀!