0

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

到目前为止,我已经纠正了我的代码中的一个又一个错误,但现在我只剩下三个(目前)。两个是它在 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 ();
}
4

1 回答 1

1

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

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

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

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

以下是文件:

palidromes.java

IStk.java

Stk.java

于 2013-09-12T01:33:26.787 回答