我是一名初学者程序员,正在尝试找出我的代码出了什么问题。我该如何纠正错误?到目前为止,这是我可以输入的代码。这与游戏“字梯”非常相似。我不断收到此错误,无法理解如何解决此问题:
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
答案 0 :(得分:1)
您需要使用-> charArray [0] =(char)index;
尽管如果在某个IDE中执行此操作,则编译错误将显示如下内容->类型不匹配:无法从int转换为char
答案 1 :(得分:0)
这类似于this answer
您正在尝试将some vbscript code
ERR: 'JSON' is undefined
{"aaa":"bbb"}
ddd
类型分配给char
值。尝试强制改成像这样键入char:
int