gpt4 book ai didi

java - 我尝试编写一个程序,但控制台仍然显示 "Exception in thread "main"java.lang.NullPointerException",我不知道出了什么问题

转载 作者:行者123 更新时间:2023-12-01 13:26:09 25 4
gpt4 key购买 nike

该程序应该像这样工作:

首先,它从用户处获取 SENTENCE(字符串)和 MUTATION 因子( double )。然后它将字符串 SENTENCE 更改为字符数组 (ARRAY 1)。之后,它创建 5 个新的 char 数组,其长度等于 ARRAY 1 的长度。其中每一个都填充了随机字符(只有字母和空格)。然后程序选择这 5 个与数组 1 最相似的数组之一。获胜者“乘”到接下来的 5 个数组中。在每一个中,程序都会验证是否符合 ARRAY 1。如果字符适合 ARRAY 1 中的字符,则程序不会更改它。如果该字符不适合 ARRAY 1 中的字符,则它是否会发生突变(根据 MUTATION 因子和随机概率)。然后程序选择一个与 ARRAY 1 最相似的数组,获胜者“相乘”等。程序一直运行,直到程序创建的数组与 ARRAY 1 相同。“计数器”(静态 int)计算具有在程序创建正确的数组之前通过。

我怀疑最后一个方法可能有问题。但我真的不知道那是什么。这是我写的代码:

static int counter;
public static void main(String[] args) {

Scanner skaner = new Scanner(System.in);
String sentence = skaner.nextLine();
double mutation = Double.parseDouble(skaner.nextLine());

//
ArrayList<char[]> candidates = Draw(ChangeIntoChars(sentence), mutation);
char[] candidate = ChooseTheBestOne(candidates,
ChangeIntoChar(sentence));
String result = CheckAndDraw(candidate, ChangeIntoChars(sentence),
mutation);
System.out.print(result);
System.out.println();
System.out.print(counter);
}

public static char[] ChangeIntoChars (String sentence) {

char[] chars = sentence.toCharArray();
return chars;
}



public static ArrayList<char[]> Draw (char [] chars, double mutation) {

char [] random1 = new char [chars.length];
char [] random2 = new char [chars.length];
char [] random3 = new char [chars.length];
char [] random4 = new char [chars.length];
char [] random5 = new char [chars.length];

Random r = new Random();


for (int i=0; i<random1.length; i++) {
if (random1[i]==chars[i]) {}
else {double p = Math.random();
if (mutation>p){
String alphabet = "aąbcćdeęfghijklłmnńoópqrsśtuwxyzżź ";
random1[i]=(alphabet.charAt(r.nextInt(alphabet.length())));
}
else {}
}}

for (int i=0; i<random2.length; i++) {
if (random2[i]==chars[i]) {}
else {double p = Math.random();
if (mutation>p){
String alphabet = "aąbcćdeęfghijklłmnńoópqrsśtuwxyzżź ";
random2[i]=(alphabet.charAt(r.nextInt(alphabet.length())));
}
else {}
}}

for (int i=0; i<random3.length; i++) {
if (random3[i]==chars[i]) {}
else {double p = Math.random();
if (mutation>p){
String alphabet = "aąbcćdeęfghijklłmnńoópqrsśtuwxyzżź ";
random3[i]=(alphabet.charAt(r.nextInt(alphabet.length())));
}
else {}
}}

for (int i=0; i<random4.length; i++) {
if (random4[i]==chars[i]) {}
else {double p = Math.random();
if (mutation>p){
String alphabet = "aąbcćdeęfghijklłmnńoópqrsśtuwxyzżź ";
random4[i]=(alphabet.charAt(r.nextInt(alphabet.length())));
}
else {}
}}

for (int i=0; i<random5.length; i++) {
if (random5[i]==chars[i]) {}
else {double p = Math.random();
if (mutation>p){
String alphabet = "aąbcćdeęfghijklłmnńoópqrsśtuwxyzżź ";
random5[i]=(alphabet.charAt(r.nextInt(alphabet.length())));
}
else {}
}}


ArrayList<char[]> candidates = new ArrayList<char[]>();
candidates.add(random1);
candidates.add(random2);
candidates.add(random3);
candidates.add(random4);
candidates.add(random5);

return candidates;
}
public static char [] ChooseTheBestOne(ArrayList<char[]>candidates,char[]chars) {

char [] number1 = candidates.get(0);
char [] number2 = candidates.get(1);
char [] number3 = candidates.get(2);
char [] number4 = candidates.get(3);
char [] number5 = candidates.get(4);

int j = 0;
for (int i=0; i<number1.length; i++){
if (number1[i]==chars[i]){
j++;}
}

int k = 0;
for (int i=0; i<number2.length; i++){
if (number2[i]==chars[i]){
k++;}
}

int l = 0;
for (int i=0; i<number3.length; i++){
if (number3[i]==chars[i]){
l++;}
}

int m = 0;
for (int i=0; i<number4.length; i++){
if (number4[i]==chars[i]){
m++;}
}

int n = 0;
for (int i=0; i<number5.length; i++){
if (number5[i]==chars[i]){
n++;}
}

int [] whosTheWinner = {j,k,l,m,n};
int max = 0;
char[] winner = null;
for (int x=0; x<whosTheWinner.length; x++) {
if (whosTheWinner[x]> max) {max = whosTheWinner[x];
if (x==0) {winner = number1;}
else if (x==1) {winner = number2;}
else if (x==2) {winner = number3;}
else if (x==3) {winner = number4;}
else if (x==4) {winner = number5;}

}}
counter++;
return winner ;
}
public static String CheckAndDraw (char[]winner,char[]chars,double mutation){
int i = 0;
String end = "This is not what I meant.";

boolean success;


if (winner[i]==chars[i]){
success = true;
end = "END";
}
else {success = false;}


while (success!=true){

ArrayList<char[]> resultsOfTheDraw = Draw(winner, mutation);
winner = ChooseTheBestOne(resultsOfTheDraw, chars);

}



return end;

}
}

最佳答案

您的问题是,在方法 ChooseTheBestOne() 中,在方法末尾的 if-else 梯子中,有时没有一个条件为 true,因此 winner 保持为空。您需要在该阶梯中放置一个 else 子句,为 winner 分配一些内容。

关于java - 我尝试编写一个程序,但控制台仍然显示 "Exception in thread "main"java.lang.NullPointerException",我不知道出了什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21799226/

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