以下是代码。我无法弄清楚为什么在类数据库中我的其他状态如果状态与我的if语句同时运行如果我已经将第二只鸟输入到ArrayList中。请,任何帮助将不胜感激!
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Database d1 = new Database();
while (true) {
System.out.println("What do you want to do?");
String answer = input.nextLine();
if(answer.equalsIgnoreCase("Add")){
System.out.println("Name: ");
String name = input.nextLine();
System.out.println("Latin name: ");
String lName = input.nextLine();
d1.addBird(name, lName);
} else if (answer.equalsIgnoreCase("O")) {
System.out.println("What was observed?");
String observed = input.nextLine();
d1.Observation(observed);
} else if (answer.equalsIgnoreCase("stats")) {
d1.showBirds(); //Displays all with observations.
} else if (answer.equalsIgnoreCase("show")) {
System.out.println("What?");
String search = input.nextLine();
d1.searchBird(search);
} else if (answer.equalsIgnoreCase("quit")){
break;
}
}
}
}
public class Bird {
private final String name;
private final String latinName;
private int count;
public Bird (String name, String latinName) {
this.name = name;
this.latinName = latinName;
this.count = count;
}
public String getName () {
return this.name;
}
public String getLatin() {
return this.latinName;
}
public String add () {
return this.name + " " + "(" +this.latinName + ")"+ " " + this.count + " observation(s)";
}
public void increaseCount () {
this.count++;
}
}
import java.util.ArrayList;
public class Database {
private final ArrayList<Bird> birdList;
public Database() {
this.birdList = new ArrayList<Bird>();
}
public void addBird (String name, String lname) {
this.birdList.add(new Bird(name, lname));
}
public void Observation (String observed) {
for (Bird x : getBirds()) { // this has to be a method
if (x.getName() != null && x.getLatin() != null && x.getName().contains(observed) || x.getLatin().contains(observed)) {
System.out.println("Done");
System.out.println("");
x.increaseCount();
} else if (x.getName() != observed || x.getLatin() != observed) {
System.out.println("Not a bird");
}
}
}
public void showBirds () {
for (Bird x : this.birdList) {
System.out.println(x.add());
}
}
public ArrayList<Bird> getBirds() {
return this.birdList;
}
public void searchBird(String search) {
for (Bird x : getBirds()) {
if (x.getName().contains(search)) {
System.out.println(x.add());
}
}
}
}
答案 0 :(得分:0)
我认为问题出在这个方法上:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NgaModule } from '../../../theme/nga.module';
import { ValidationService } from '../../../services/validators/validation.service';
import { Calender } from './calender.component';
import { routing } from './calender.routing';
import { CalendarComponent } from "angular2-fullcalendar/src/calendar/calendar";
@NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
NgaModule,
routing,
],
declarations: [
Calender,
CalendarComponent
]
})
export default class CalenderModule { }
做这样的事情:
public void Observation (String observed) {
for (Bird x : getBirds()) { // this has to be a method
if (x.getName() != null && x.getLatin() != null && x.getName().contains(observed) || x.getLatin().contains(observed)) {
System.out.println("Done");
System.out.println("");
x.increaseCount();
}
/* No need to print "Not a bird" for every mismatch. Use a flag instead */
else if (x.getName() != observed || x.getLatin() != observed) {
System.out.println("Not a bird");
}
}
}