java的循环问题

java的循环问题,java,eclipse,for-loop,nested-loops,Java,Eclipse,For Loop,Nested Loops,每当我运行此循环时: System.out.println("How many gerbils are in the lab?"); int population = keyboard.nextInt(); Gerbil[] gerbil = new Gerbil[population]; for (int i=0; i<population; i++){ gerbil = new Gerbil[population]; idnumber:

每当我运行此循环时:

System.out.println("How many gerbils are in the lab?");
    int population = keyboard.nextInt();
    Gerbil[] gerbil = new Gerbil[population];
    for (int i=0; i<population; i++){
    gerbil = new Gerbil[population];
    idnumber:
        for 
            (int b = 0; b<population; b++){
            System.out.println("What is the id number of gerbil " + (b+1));
            String idnumberx = keyboard.next();
                if (b>0){
                    for (int c = 0; c<gerbil.length; c++){
                        if (idnumberx.equals(gerbil[c].getId())){
                            System.out.println("Repeat, try again");
                            c--;

                            }
                        else {
                            return;
                        }
                        }
                    }
                }
第57行是这样的:
if(idnumberx.equals(沙鼠[c].getId()){

基本上,我的程序要求用户输入x个沙鼠的id。如果其中一个沙鼠id与以前的id匹配,则输出应为

repeat, try again
以下是我的代码的其余部分供参考:

package assignment4;

import java.util.Scanner;
import java.util.Arrays;

import assignment4.Gerbil;

public class Assignment4 {
public static int population;
public static int[] foodeats;
public static int types;
public static String idnumberx;
public static String nicknamex;
public static String g;
public static String gerbilId;
public static Gerbil[] gerbil;
public static String amountoffoodeaten;
public static String gerbilsearch;
public static String thisgerbil;
public static String foodname;
public static int[] totalfood;
public static food[] food;
public static int maximum;
public static boolean bite;
public static boolean escape;


public static void main(String args[]){
    Scanner keyboard = new Scanner(System.in);

    System.out.println("How many types of food do the gerbils eat?");       
    int F = keyboard.nextInt();
    food = new food[F];

    for
        (int a = 0; a<food.length; a++){
        System.out.println("Name of food number " + (a+1));
        foodname = keyboard.next();
        System.out.println("Max amount of food " + (a+1));
        maximum = keyboard.nextInt();

        food[a] = new food(foodname, maximum);
        }

    System.out.println("How many gerbils are in the lab?");
    int population = keyboard.nextInt();
    Gerbil[] gerbil = new Gerbil[population];
    for (int i=0; i<population; i++){
    gerbil = new Gerbil[population];
    idnumber:
        for 
            (int b = 0; b<population; b++){
            System.out.println("What is the id number of gerbil " + (b+1));
            String idnumberx = keyboard.next();
                if (b>0){
                    for (int c = 0; c<gerbil.length; c++){
                        if (idnumberx.equals(gerbil[c].getId())){
                            System.out.println("Repeat, try again");
                            c--;

                            }
                        else {
                            return;
                        }
                        }
                    }
                }

            System.out.println("What is the name for gerbil " + (i+1));
            String nicknamex = keyboard.next();

            foodeats = new int[F];
                for
                    (int c = 0; c<foodeats.length; c++){
                     System.out.println("how much " + food[c].foodname + " does " + nicknamex + " eat");
                        int gerbileats = keyboard.nextInt();
                        foodeats[c] = gerbileats;
                        if (gerbileats > maximum){
                            do{
                                System.out.println("You stoopid, try again");
                                c--;
                            }
                            while (gerbileats <= maximum);
                            }
                        }




            for
                (int d = 0; d<population; d++){
            System.out.println("Does " + nicknamex + " bite? Please enter True or False");
            String doesitbite = keyboard.next();
            if (doesitbite.equalsIgnoreCase("true")){
                bite = Boolean.parseBoolean(doesitbite);
            break;
            }
            else if (doesitbite.equalsIgnoreCase("false")){
                bite = Boolean.parseBoolean(doesitbite);
                break;
                }
                else 
                System.out.println("Repeat, try again");
                d--;


            }



            for
                (int d = 0; d<population; d++){
            System.out.println("Does " + nicknamex + " escape? Please enter True or False");
            String doesitescape = keyboard.next();
            if (doesitescape.equalsIgnoreCase("true")){
            escape = Boolean.parseBoolean(doesitescape);
            break;
            }
            else if (doesitescape.equalsIgnoreCase("false")){
            escape = Boolean.parseBoolean(doesitescape);
            break;
            }
                else 
                System.out.println("Repeat, try again");
                d--;
              }


            gerbil[i] = new Gerbil(idnumberx, nicknamex, foodeats, bite, escape);
    }


        while (true){
            System.out.println("What would you like to know?");
            String question = keyboard.nextLine();
            String search = "search";
            String average = "average";
            String end = "end";
            String restart = "restart";


            if (question.equalsIgnoreCase(search)){
                new Assignment4().searchForGerbil();
                }

            else 
                if (question.equalsIgnoreCase(average)){
                    //new Assignment4().averageFood();
                }
            else
                if (question.equalsIgnoreCase(end)){
                    System.exit(0);
                }
            else
                if (question.equalsIgnoreCase(restart)){
                    new Assignment4().main(args);
                    }
            else
                System.out.println("Try again");
                }
}


public static void searchForGerbil()
{
System.out.println("Please type in a gerbil lab ID");
Scanner keyboard = new Scanner(System.in);
String searchgerbil = keyboard.next();
boolean found = false;
int i = 0;
int location = 0;
for (i = 0; i <gerbil.length; i++){
        if ( searchgerbil.equals(gerbil[i].getId()))
        {
        found = true;
        location = i;
        break;
        }
        else
        {
            found = false;
        }
}
if (found = true){
     System.out.println(gerbil[location]);}
else {
System.out.println("Gerbil " + searchgerbil + " does not exist");
}
}
















    //return everything;




//  








}
这是你的问题:

Gerbil[] gerbil = new Gerbil[population];
for (int i=0; i<population; i++){
    gerbil = new Gerbil[population]; // reinitilizing array
沙鼠[]沙鼠=新沙鼠[种群];
对于(int i=0;i这是您的问题:

Gerbil[] gerbil = new Gerbil[population];
for (int i=0; i<population; i++){
    gerbil = new Gerbil[population]; // reinitilizing array
沙鼠[]沙鼠=新沙鼠[种群];
对于(int i=0;i
您的问题是
gerbil[c]
为null。这是因为Java中的数组默认将其所有值初始化为
null
。您可以通过检查null来修复此问题

for (int c=0;c<gerbil.length;c++) {
    if (gerbil[c] == null) // Tests if null.
        break;             // If null exit loop.
    if (idnumberx.equals(gerbil[c].getId())) {
        System.out.println("Repeat, try again");
        c--;
    } else {
        return;
    }
}
for(int c=0;c
您的问题是
gerbil[c]
为null。这是因为Java中的数组默认将其所有值初始化为
null
。您可以通过检查null来修复此问题

for (int c=0;c<gerbil.length;c++) {
    if (gerbil[c] == null) // Tests if null.
        break;             // If null exit loop.
    if (idnumberx.equals(gerbil[c].getId())) {
        System.out.println("Repeat, try again");
        c--;
    } else {
        return;
    }
}

for(int c=0;c您正在引用一个空数组。沙鼠数组中没有沙鼠类的对象。因此,下面的条件将引发NullPointerException:

if (idnumberx.equals(gerbil[c].getId())){
我认为您应该更改下面的第(49)行,创建一个沙鼠类的对象并将其放入数组中

gerbil = new Gerbil[population];

您正在引用一个空数组。沙鼠数组中没有沙鼠类的对象。因此,下面的条件将引发NullPointerException:

if (idnumberx.equals(gerbil[c].getId())){
我认为您应该更改下面的第(49)行,创建一个沙鼠类的对象并将其放入数组中

gerbil = new Gerbil[population];

Java中的数组索引基于
0
,而不是
1
。这应该做什么?Java中的数组索引基于
0
,而不是
1
。这应该做什么?