gpt4 book ai didi

java - 我的程序更改了 4 个字母单词中的一个字母,并将其与输入的其他内容进行切换,然后更新

转载 作者:行者123 更新时间:2023-12-02 10:37:02 25 4
gpt4 key购买 nike

我是一名初学者程序员,正在尝试找出我的代码出了什么问题。我该如何改正我的错误?这是到目前为止我可以输入的代码。这与“Word Lader”游戏非常相似。我不断收到此错误,并且无法理解如何解决此问题:

OneLetterGame.java:67: error: incompatible types: possible lossy conversion from int to char:   charArray[0] = index ;

代码:

import javax.swing.*;
import java.util.Scanner;
import java.io.*;


public class OneLetterGame {
public static void main(String[] args) {
String startWord = "";
String currentWord = "";
String goalWord = "";
String error = "";
char newLetter = '\0';
int index = 0;
int steps = 0;

do {
String[] fileContents = getFileContents("dictionary.txt");

startWord = JOptionPane.showInputDialog(error + "Player One, please enter a FOUR letter word: ");

if (startWord == null) {
//exits program if user selects "cancel"
System.exit(0);
} //if

currentWord = startWord;

/* for(int i = 0; i < fileContents.length;i++){
if(currentWord.equals(fileContents[i]){
break;
}else{
error = "This is not a valid four letter word. Please try again. \n";
continue;
}

}//for */

System.out.println(startWord);

goalWord = JOptionPane.showInputDialog("Player Two, please enter a FOUR letter GOAL word: ");

if (goalWord == null) {
//exits program if user selects "cancel"
System.exit(0);
} //if

steps++;

//user enters step has form index space letter
currentWord = JOptionPane.showInputDialog("Please enter the letter you want to change and its location (ex: 1 a) \n The Goal Word is " + goalWord + " :");

if (currentWord == null) {
//exits program if user selects "cancel"
System.exit(0);
} //if

//pulls out character at index 0
index = currentWord.charAt((int) 0 - 48);
//pulls out character at index 3
newLetter = currentWord.charAt(3);

//string to char
char[] charArray;
charArray = currentWord.toCharArray();

charArray[0] = index;
charArray[3] = newLetter;
currentWord = String.valueOf(charArray);

//char to string
currentWord = new String(charArray);

System.out.println(currentWord);



if (currentWord.equals(goalWord)) {
if (steps % 2 == 0) {
System.out.println("Congratulations to Player2. You won in " + steps + " step(s)!");
} else {
System.out.println("Congratulations to Player1. You won in " + steps + " step(s)!");
}
break;
} else {
continue;
}
} while (true);

} //main

public static String[] getFileContents(String fileName) {

String[] contents = null;
int length = 0;
int num = 0;
try {
// input
String folderName = "/subFolder/"; // if the file is contained in the same folder as the .class file, make this equal to the empty string
String resource = fileName;

// this is the path within the jar file
InputStream input = OneLetterGame.class.getResourceAsStream(folderName + resource);
if (input == null) {
// this is how we load file within editor (eg eclipse)
input = OneLetterGame.class.getClassLoader().getResourceAsStream(resource);
}
BufferedReader in = new BufferedReader(new InputStreamReader(input));



in .mark(Short.MAX_VALUE); // see api

// count number of lines in file
while ( in .readLine() != null) {
length++;
}

in .reset(); // rewind the reader to the start of file
contents = new String[length]; // give size to contents array

// read in contents of file and print to screen
for (int i = 0; i < length; i++) {
contents[i] = in .readLine();
} in .close();
} catch (Exception e) {
System.out.println("File Input Error");
}

return contents;

} // getFileContents
}//onelettergame

最佳答案

您需要正确使用 --> charArray[0] = (char) index;

尽管如果您在某些 IDE 中执行此操作,则编译错误将显示类似以下内容 --> 类型不匹配:无法从 int 转换为 char

关于java - 我的程序更改了 4 个字母单词中的一个字母,并将其与输入的其他内容进行切换,然后更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53201367/

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