gpt4 book ai didi

java - 我似乎无法修复编译器错误

转载 作者:行者123 更新时间:2023-12-01 14:07:56 25 4
gpt4 key购买 nike

我的代码遇到三个问题。该代码旨在从键盘读取字符串,将其转换为堆栈(将小写字母转换为大写字母,并在此过程中删除空格和标点符号),比较两者,并确定它是否是回文。

到目前为止,我已纠正了代码中的一个又一个错误,但现在(目前)已减少到三个。两个是它在 Stk 的第 5 行“无法访问 .\Stk.java”和“无法访问 .\IStk.java”(是的,我注意到它可以访问它,然后突然不能)。

第三个错误是在 s = infile.nextline(); 行中找不到句号符号。 这个特定实例位于第 29 行。

这是主文件的代码。

import java.text.*;
import javax.swing.*;
import java.util.*;
import java.util.Scanner;
import java.util.Stack;

public class palindromes {
public static void main(String[] args) {
Scanner infile = new Scanner (System.in);
String s;
int StrLength;
while (s != "Q");
{
// Explains what a palindrome is and asks user to enter test phrase.
System.out.println("This program will test strings to see if they are palindromes.");
System.out.println("A palindrome is a word or phrase that is spelled the same way frontwards and backwards.");
System.out.println("Please enter a string to be tested (Enter 'Q' to quit): ");

// User enters test phrase. If phrase == 'Q', program ends.
s = infile.nextline();

// Converts all letters to caps, removes all white-space and punctuation,
// initializes and stores the value of the length of String 2, initializes the stack.
s.toUpperCase();
s.replaceAll("\\W", "");
s.replaceAll("\\s", "");
StrLength = s.length() - 1;
Stk s2;
s2(s.length());

int i;

// Pushes every letter in the string into the stack.
for (i = 0; i <= StrLength; i++) {
s2.push(s.charAt(i));
};
char c;
i = 0;

// While S3 is not empty, compare popped character with character in same position in string.
// If both are equal, continue until s3 is empty. If there is any difference, end the loop.
while (s2.isEmpty() != TRUE) {
c = s2.pop();
if (c == s.charAt(i)) {
i++;
if (i > StrLenth) {
System.out.println("This string is a palindrome. Please enter another string to test (Enter Q to quit).");
s = infile.nextline();
StrLength = 0;
};
}
else
{
System.out.println("This string is not a palindrome. Please enter another string to test (Enter Q to quit).");
s = infile.nextline();
StrLength = 0;
};

};
};

}

}

这里分别是 Stk.java 和 IStk.java 的代码。

package palindromes;

import java.io.*;

public class Stk implements IStk {
public Stk ()
{
front=0;
rear=0;
maxStk=0;
items = new char [MAXSIZE];
}

public Stk(int max)
{
maxStk = max+1;
front = maxStk - 1;
rear = maxStk - 1;
items = new char [max];
}

public boolean isFull ()
{
// WRAP AROUND
return ( (top + 1) % maxStk == bot );
}

public boolean isEmpty ()
{
return ( bot == top );
}

public void push (char item)
{
if (!isFull())
{ top = (top+1) % maxStk;
items[top] = item;
}
else
System.out.println ("Tried to insert into full stack.");
}

public char pop ()
{
char item;
if (!isEmpty())
{
item = items[top];
top = (top-1) % maxStk;
}
else
System.out.println ("Tried to remove from empty stack.");

return item;
}

public final int MAXSIZE =100;
private char top;
private char bot;
private int maxStk;
private char items [];
}

public interface IStk
{
/**
* Returns true if Stk is full
* @param none
* @return boolean
*/
public boolean isFull ();

/**
* Returns true if Stk is empty
* @param none
* @return boolean
*/
public boolean isEmpty ();

/**
* Inserts an element at the rear of the Stk
* @param item, the item to be inserted
*/
public void push (char item);

/**
* Returns (and removes) an element from the Stk
* @param item, the item that is removed/returned
*/
public char pop ();
}

最佳答案

我修复了所有三个类的所有编译错误以及一些最明显的语义错误(其中一些在问题评论中提到)。

将我的版本与你的版本进行比较,并尝试了解我的更改。如果您没有得到任何答案,请在此答案的评论中留下问题。

注意:即使您的程序正在编译并运行,但在从 Stk 调用 Push 方法时它会崩溃!当我不那么忙的时候,我会尝试检查问题,但也要自己做!

你确实应该花一些时间来学习更多 Java 编程、面向对象编程 (OOP) 等基础知识。这里有很多非常非常基本的错误。

以下是文件:

palidromes.java

IStk.java

Stk.java

关于java - 我似乎无法修复编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18753626/

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