java - 我的程序运行但没有输出
问题描述
我必须使用继承制作一个程序,我想打印出我输入的内容。但是当我运行程序时,没有发现错误,它只是给了我一个空白空间。我的代码有什么问题?我在调用变量时犯了错误吗?
这是我的主要代码:
package labweek7;
import java.awt.Menu;
import java.util.Scanner;
import java.util.Vector;
public class Main {
Vector<String> menu =new Vector<>();
Scanner scan = new Scanner(System.in);
//Employee emp;
Employee emp = new EmployeeFullTime(null, 0, null, null);
Employee emps = new EmployeePartTime(null, 0, null, null);
EmployeeFullTime ft = new EmployeeFullTime(null, 0, null, null);
EmployeePartTime pt = new EmployeePartTime(null, 0, null, null);
public Main() {
int choice = 0;
int pay;
int time;
int salary;
do{
System.out.println("ABC EMPLOYEE DATA");
System.out.println("=================");
System.out.println("1. Add Employee");
System.out.println("2. View Employee");
System.out.println("3. Resign");
System.out.println("4. Exit");
System.out.print("Choice: ");
choice = scan.nextInt();
scan.nextLine();
switch(choice){
case 1:
String name = "";
do{
System.out.print("Input employee name[must be more than 3 characters]: ");
name = scan.next();
}while(! (name.length()>=3));
emp.empName.add(name);
int age;
do{
System.out.print("Input employee age[>=17]: ");
age = scan.nextInt();
}while(!(age>=17));
emp.empAge.add(age);
String role = "";
do{
System.out.print("Input employee role[Assistant | Programmer](Case Sensitive): ");
role = scan.next();
}while(!(role.equals("Assistant") || (role.equals("Programmer"))));
emp.empRole.add(role);
String type = "";
do{
System.out.print("Input employee type[PartTime | FullTime](Case Sensitive): ");
type = scan.next();
}while(!(type.equals("PartTime")|| type.equals("FullTime")));
emp.empType.add(type);
if(type.equals("PartTime")){
emp = new EmployeePartTime(name, age, role, type);
do{
System.out.print("Input pay per hour[>=10000]: ");
pay = scan.nextInt();
}while(!(pay>=10000));
pt.pays.add(pay);
do{
System.out.print("Input working hour per week[>0]: ");
time = scan.nextInt();
}while(!(time>0));
pt.hours.add(time);
}
else{
emps = new EmployeeFullTime(name, age, role, type);
do{
System.out.print("Input base salary[>=10000]: ");
salary = scan.nextInt();
}while(!(salary>=10000));
ft.salary.add(salary);
}
String status = "active";
System.out.println("Success insert employee data");
System.out.println("");
System.out.println("Please any key to continue...");
scan.nextLine();
break;
case 2:
boolean ans = emp.empName.isEmpty();
if(ans == true){
System.out.println("There is no employee data in the list");
}
else
{
view();
}
}
}while(choice!=4);
}
void view(){
//for(int i=0; i<menu.size(); i++){
System.out.println("Employee no.");
if(emp.empType.equals("FullTime")){
System.out.println("Full Time Employee");
System.out.println("===================");
System.out.println("Status: ");
for(int j=0; j<emps.empName.size(); j++){
// if(emps == null){
// }
// else{
System.out.println("Name: " + emps.empName.get(j));
// }
//System.out.println("Name: " + emps.empName.get(j));
}
for(int m=0; m<emp.empAge.size(); m++){
System.out.println("Age: " + emps.empAge.get(m));
}
for(int n=0; n<emps.empRole.size(); n++){
System.out.println("Role: " + emps.empRole.get(n));
}
for(int o=0; o<ft.salary.size(); o++){
System.out.println("Base salary per month: " + ft.salary.get(o));
}
System.out.println("");
}
else
{
System.out.println("Part Time Employee");
System.out.println("===================");
// System.out.println("Status: " );
// System.out.println("Name: " + emp.empName.get(i));
// System.out.println("Age: " + emp.empAge.get(i));
// System.out.println("Role: " + emp.empRole.get(i));
// System.out.println("Pay per hour: "+pt.pays.get(i));
// System.out.println("Working hour per week: "+pt.hours.get(i));
// System.out.println("Salary per month: "+ pt.pays.get(i) * pt.hours.get(i));
//
}
System.out.println("\n\n");
System.out.println("Press any key to continue...");
}
void resign(){
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Main();
}
}
这是我的父类:
package labweek7;
import java.util.Vector;
public abstract class Employee {
public String name;
public int age;
public String role;
public String type;
Vector<String> empName = new Vector<>();
Vector<Integer> empAge = new Vector<>();
Vector<String> empRole = new Vector<>();
Vector<String> empType = new Vector<>();
public Employee(String name, int age, String role, String type) {
// TODO Auto-generated constructor stub
this.name = name;
this.age = age;
this.role = role;
this.type = type;
}
}
这是我的孩子 1 班:
package labweek7;
import java.util.Vector;
public class EmployeePartTime extends Employee{
int pay;
int hour;
Vector<Integer> pays = new Vector<>();
Vector<Integer> hours = new Vector<>();
public EmployeePartTime(String name, int age, String role, String type) {
super(name, age, role, type);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
这是我的孩子 2 班:
p
ackage labweek7;
import java.util.Vector;
public class EmployeeFullTime extends Employee{
int gaji;
Vector<Integer> salary = new Vector<>();
public EmployeeFullTime(String name, int age, String role, String type) {
super(name, age, role, type);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
我在 for 循环中犯错了吗?为什么我的代码不打印输入的项目?任何帮助表示赞赏。谢谢你。
解决方案
我已经在我的 Eclipse 工作空间中调试了你的代码。
我能够成功插入员工数据,如下所示:
ABC EMPLOYEE DATA
=================
1. Add Employee
2. View Employee
3. Resign
4. Exit
Choice: 1
Input employee name[must be more than 3 characters]: asfds
Input employee age[>=17]: 14
Input employee age[>=17]: 18
Input employee role[Assistant | Programmer](Case Sensitive): Assistant
Input employee type[PartTime | FullTime](Case Sensitive): PartTime
Input pay per hour[>=10000]: 20000
Input working hour per week[>0]: 12
Success insert employee data
但是在为 View Employee 选择第二个选项后,我得到了空输出。
ABC EMPLOYEE DATA
=================
1. Add Employee
2. View Employee
3. Resign
4. Exit
Choice: 2
There is no employee data in the list
在对其进行了更多调试后,我在 Main() 中遇到了一个条件
boolean ans = emp.empName.isEmpty();
if(ans == true){
System.out.println("There is no employee data in the list");
} else {
view();
}
emp.empName 在这里是空的。所有属于 Employee 类的向量都是空的。
Vector<String> empName = new Vector<>();
Vector<Integer> empAge = new Vector<>();
Vector<String> empRole = new Vector<>();
Vector<String> empType = new Vector<>();
作为一种解决方法,我只是将 emp.empName.isEmpty() 更改为 emp.name.isEmpty() 来解决问题。我希望这可以帮助您识别问题。
推荐阅读
- python - Setup.py 安装需要本地包
- windows - 适用于 Windows 的 Docker 桌面 - 由于 Bitlocker 而无法启动
- if-statement - 最大/最小方法是否比 If 语句更传统地设置值的下限/上限?
- .net - 从任务中获取 WaitHandle
- ios - 为什么一个 bname 值在快速解析时在 tableview 中显示其 paramName 计数时间?
- python - 在 For 循环中:如何将值与 groupby 函数中的值进行比较
- git - VSCode Git 跟踪颜色?
- django - Django中的分页概念和实现大型对象的有效方法
- android - 滚动时recyclerview视图行为不端?
- listview - 由 SearchBar 过滤的 Xamarin 组 ListView