gpt4 book ai didi

java - java的循环问题

转载 作者:行者123 更新时间:2023-12-01 18:33:16 26 4
gpt4 key购买 nike

每当我运行这个循环时:

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;
}
}
}
}

我最终得到这个输出:

How many gerbils are in the lab?
2
What is the id number of gerbil 1
123
What is the id number of gerbil 2
456
Exception in thread "main" java.lang.NullPointerException
at assignment4.Assignment4.main(Assignment4.java:57)

第 57 行是这样的:if (idnumberx.equals(gerbil[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;




//








}

沙鼠类

package assignment4;

import java.util.Arrays;

public class Gerbil {

public static int[] foodeats;
public static String idnumberx;
public static String nicknamex;
public static String gerbilsearch;
public static boolean bite;
public static boolean escape;
public static String foodname;

public Gerbil(String idnumberx, String nicknamex, int[]foodeats,boolean bite, boolean escape) {
this.idnumberx = idnumberx;
this.nicknamex = nicknamex;
this.foodeats = foodeats;
this.escape = escape;
this.bite = bite;
}

public boolean getBite() {
return bite;
}
public boolean getEscape() {
return escape;
}
public String getId() {
return idnumberx;
}
public String getName() {
return nicknamex;
}
public void setId(String[] newId) {
idnumberx = this.idnumberx;
}
public void setName(String[] newName) {
nicknamex = this.nicknamex;
}

public String toString() {
return "Gerbil [idnumber=" + idnumberx + ", nickname=" + nicknamex
+ ", totalfood=" + Arrays.toString(foodeats) + ", foodname="
+ Arrays.deepToString(foodname) + ", escape=" + escape + ", bite="
+ bite + ", foodeats=" + Arrays.toString(foodeats)
+ ", gerbilsearch=" + gerbilsearch + "]";
}
}

最佳答案

这是你的问题:

Gerbil[] gerbil = new Gerbil[population];
for (int i=0; i<population; i++){
gerbil = new Gerbil[population]; // reinitilizing array

我认为你的意思是这样的:

Gerbil[] gerbil = new Gerbil[population];
for (int i=0; i<population; i++){
gerbil[i] = new Gerbil(); // initilizing gerbil

关于java - java的循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23178051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com