gpt4 book ai didi

java token 语法错误 "(", ;预期的

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

/*help me to correct this error*/

import javax.swing.JOptionPane;

public class Assignment1
{

public static void main(String args[])
{
String input = JOptionPane.showInputDialog("Enter a string");
if (istPalindrome(input))
{
JOptionPane.showMessageDialog(null,input+"is a palindrome");
}
else{
JOptionPane.showMessageDialog(null ,input +"is not a palindrome");
}

public static boolean istPalindrome(String a)/*error is here*/
{
char[] charArray = word.toCharArray();
int i1 = 0;
int i2 = word.length() - 1;

while (i2 > i1) {
if (charArray[i1] != charArray[i2]) {
return false;
}

++i1;
--i2;
}
return true;
}
}
}

最佳答案

正如棘轮怪胎所说,您需要将函数移出 main。该函数本身属于Assignment1类,因此需要:

import javax.swing.JOptionPane;

public class Assignment1
{
public static void main(String args[])
{
String input = JOptionPane.showInputDialog("Enter a string");
if (istPalindrome(input))
{
JOptionPane.showMessageDialog(null,input+"is a palindrome");
}
else{
JOptionPane.showMessageDialog(null ,input +"is not a palindrome");
}
}

public static boolean istPalindrome(String a)/*error is here*/
{
char[] charArray = a.toCharArray();
int i1 = 0;
int i2 = a.length() - 1;

while (i2 > i1) {
if (charArray[i1] != charArray[i2]) {
return false;
}

++i1;
--i2;
}
return true;
}
}

我还将变量“word”更改为“a”(对于语句 word.toCharArray() 和 word.length()),因为该范围内没有“word”变量功能。

关于java token 语法错误 "(", ;预期的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22068872/

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