所以我的问题是如何在运行程序时删除/不显示空值,多余未找到和0,以及当'runned'时Student201的最后两个属性,它以相反的顺序显示意味着首先显示空值或多余的未找到或零。
我如何添加学生的预定义'
This is my preferred output or display as much as possible w/o the not found
This is not what I want, the not founds are printed first before the desired output
Main.java
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Student a1=new Student();
//a1.choosy();
Student201 a2 =new Student201();
a2.studinfo();
a2.findstud("2000000001);
}
}
Student201.java
public class Student201 {
Student [] studarray = new Student[13];
int x;
public void studinfo()
{
for (x=0;x<studarray.length;x++) {
studarray[x]= new Student();
}
Student estudyante1 = new Student();
Student estudyante2 = new Student ();
Student estudyante3 = new Student ();
Student estudyante4 = new Student ();
Student estudyante5 = new Student ();
estudyante1.getStudName("Yves Francisco");
estudyante1.getStudNum(2000000001);
estudyante1.getYrLvl(5);
estudyante1.getKors("CpE");
estudyante1.getGender("Male");
estudyante2.getStudName("Lance Eco");
estudyante2.getStudNum(2000000002);
estudyante2.getYrLvl(5);
estudyante2.getKors("CpE");
estudyante2.getGender("Male");
estudyante3.getStudName("Karlos Castillo");
estudyante3.getStudNum(2000000003);
estudyante3.getYrLvl(5);
estudyante3.getKors("CpE");
estudyante3.getGender("Male");
estudyante4.getStudName("Glenn Bordonada");
estudyante4.getStudNum(2000000004);
estudyante4.getYrLvl(4);
estudyante4.getKors("ECE");
estudyante4.getGender("Male");
estudyante5.getStudName("Tim Tolentino");
estudyante5.getStudNum(2000000005);
estudyante5.getYrLvl(4);
estudyante5.getKors("ECE");
estudyante5.getGender("Male");
studarray[0]=estudyante1;
studarray[1]=estudyante2;
studarray[2]=estudyante3;
studarray[3]=estudyante4;
studarray[4]=estudyante5;
}
public void findstud (String query) //String query for searching
{
int ercatch=0;
try{
ercatch=Integer.parseInt(query);
}
catch (NumberFormatException m)
{
}
for (x=0;x<studarray.length;x++)
{
if (query.equalsIgnoreCase(studarray[x].setStudName())) //query.equalsIgnorecase for case sensitive inputs
{
System.out.println(studarray[x].setStudName()+"\n"+studarray[x].setStudNum()+"\n"+studarray[x].setYrLvl()+"\n"+studarray[x].setKors()+"\n"+studarray[x].setGender()+"\n");
System.out.println("\n");
}
}
for (x=0;x<studarray.length;x++)
{
if (ercatch == studarray[x].setStudNum()) //Integer.parseInt for int data types
{
System.out.println(studarray[x].setStudName()+"\n"+studarray[x].setStudNum()+"\n"+studarray[x].setYrLvl()+"\n"+studarray[x].setKors()+"\n"+studarray[x].setGender());
System.out.println("\n");
}
else if (ercatch != studarray[x].setStudNum())
{
System.out.println("Not Found!");
}
}
for (x=0;x<studarray.length;x++)
{
if (ercatch == studarray[x].setYrLvl())
{
System.out.println(studarray[x].setStudName()+"\n"+studarray[x].setStudNum()+"\n"+studarray[x].setYrLvl()+"\n"+studarray[x].setKors()+"\n"+studarray[x].setGender());
System.out.println("\n");
}
else if (ercatch != studarray[x].setYrLvl())
{
System.out.println("Not Found!");
}
}
for (x=0;x<studarray.length;x++)
{
if (query.equalsIgnoreCase(studarray[x].setKors()))
{
System.out.println(studarray[x].setStudName()+"\n"+studarray[x].setStudNum()+"\n"+studarray[x].setYrLvl()+"\n"+studarray[x].setKors()+"\n"+studarray[x].setGender());
System.out.println("\n");
}
}
for (x=0;x<studarray.length;x++)
{
if (query.equalsIgnoreCase(studarray[x].setGender()))
{
System.out.println(studarray[x].setStudName()+"\n"+studarray[x].setStudNum()+"\n"+studarray[x].setYrLvl()+"\n"+studarray[x].setKors()+"\n"+studarray[x].setGender());
System.out.println("\n");
}
}
}
public void addstud (String query)
{
}
}
Student.Java
public class Student {
private String StudName;
private int StudNum;
private int YrLvl;
private String Kors;
private String Gender;
//this just for naming convention for the get and set
public void getStudName (String name) {
this.StudName=name;
}
public String setStudName() {
return StudName;
}
public void getStudNum (int numero) {
this.StudNum=numero;
}
public int setStudNum() {
return StudNum;
}
public void getYrLvl (int yrlvl) {
this.YrLvl=yrlvl;
}
public int setYrLvl()
{
return YrLvl;
}
public void getKors (String korse) {
this.Kors=korse;
}
public String setKors() {
return Kors;
}
public void getGender (String sex)
{
this.Gender=sex;
}
public String setGender() {
return Gender;
}
public void choosy() {
System.out.println("Here is the list and the information of the Students \n");
}
/*public static void main(String[] args) {
// TODO Auto-generated method stub
}*/
}
答案 0 :(得分:1)
首先,你完全混淆 getters 和 setters , getters 应该返回没有任何参数的值, setters 将字段设置为带参数的vala ue,如此
public void setStudName (String name) {
this.StudName=name;
}
public String getStudName() {
return StudName;
}
public void setStudNum (int numero) {
this.StudNum=numero;
}
public int getStudNum() {
return StudNum;
}
由于您不想显示未找到的结果,因此我将其删除,这是您的Student201
课程。
public class Student201 {
Student[] studarray = new Student[13];
int x;
public void studinfo() {
for (x = 0; x < studarray.length; x++) {
studarray[x] = new Student();
}
Student estudyante1 = new Student();
Student estudyante2 = new Student();
Student estudyante3 = new Student();
Student estudyante4 = new Student();
Student estudyante5 = new Student();
estudyante1.setStudName("Yves Francisco");
estudyante1.setStudNum(2000000001);
estudyante1.setYrLvl(5);
estudyante1.setKors("CpE");
estudyante1.setGender("Male");
estudyante2.setStudName("Lance Eco");
estudyante2.setStudNum(2000000002);
estudyante2.setYrLvl(5);
estudyante2.setKors("CpE");
estudyante2.setGender("Male");
estudyante3.setStudName("Karlos Castillo");
estudyante3.setStudNum(2000000003);
estudyante3.setYrLvl(5);
estudyante3.setKors("CpE");
estudyante3.setGender("Male");
estudyante4.setStudName("Glenn Bordonada");
estudyante4.setStudNum(2000000004);
estudyante4.setYrLvl(4);
estudyante4.setKors("ECE");
estudyante4.setGender("Male");
estudyante5.setStudName("Tim Tolentino");
estudyante5.setStudNum(2000000005);
estudyante5.setYrLvl(4);
estudyante5.setKors("ECE");
estudyante5.setGender("Male");
studarray[0] = estudyante1;
studarray[1] = estudyante2;
studarray[2] = estudyante3;
studarray[3] = estudyante4;
studarray[4] = estudyante5;
}
public void findstud(String query) //String query for searching
{
int ercatch = 0;
try {
ercatch = Integer.parseInt(query);
} catch (NumberFormatException m) {
}
for (x = 0; x < studarray.length; x++) {
if (query.equalsIgnoreCase(studarray[x].getStudName())) //query.equalsIgnorecase for case sensitive inputs
{
System.out.println(studarray[x].getStudName() + "\n" + studarray[x].getStudNum() + "\n" + studarray[x].getYrLvl() + "\n" + studarray[x].getKors() + "\n" + studarray[x].getGender() + "\n");
System.out.println("\n");
}
}
for (x = 0; x < studarray.length; x++) {
if (ercatch == studarray[x].getStudNum()) //Integer.parseInt for int data types
{
System.out.println(studarray[x].getStudName() + "\n" + studarray[x].getStudNum() + "\n" + studarray[x].getYrLvl() + "\n" + studarray[x].getKors() + "\n" + studarray[x].getGender());
System.out.println("\n");
}
}
for (x = 0; x < studarray.length; x++) {
if (ercatch == studarray[x].getYrLvl()) {
System.out.println(studarray[x].getStudName() + "\n" + studarray[x].getStudNum() + "\n" + studarray[x].getYrLvl() + "\n" + studarray[x].getKors() + "\n" + studarray[x].getGender());
System.out.println("\n");
}
}
for (x = 0; x < studarray.length; x++) {
if (query.equalsIgnoreCase(studarray[x].getKors())) {
System.out.println(studarray[x].getStudName() + "\n" + studarray[x].getStudNum() + "\n" + studarray[x].getYrLvl() + "\n" + studarray[x].getKors() + "\n" + studarray[x].getGender());
System.out.println("\n");
}
}
for (x = 0; x < studarray.length; x++) {
if (query.equalsIgnoreCase(studarray[x].getGender())) {
System.out.println(studarray[x].getStudName() + "\n" + studarray[x].getStudNum() + "\n" + studarray[x].getYrLvl() + "\n" + studarray[x].getKors() + "\n" + studarray[x].getGender());
System.out.println("\n");
}
}
}
public void addstud(String query) {
}
}
答案 1 :(得分:0)
如何删除/不显示空值,多余的未找到,以及0
首先,删除此
for (x=0;x<studarray.length;x++) {
studarray[x]= new Student();
}
然后,除了您定义的那些学生之外,所有学生都是空的,您可以明确地忽略它们。
for (int x = 0; x < studarray.length; x++) {
Student s = studarray[x];
if (s == null) {
continue; // here
}
// Check all your fields in a single loop
if (query.equalsIgnoreCase(s.getStudName())) {
} else if (Integer.parseInt(query) == s.getStudNum()) {
} else {
System.out.println("Student not found using query " + query);
}
}