Java 编译器错误我可以';我好像没办法
我的代码有三个问题。该代码设计用于从键盘读入字符串,将其转换为堆栈(将小写字母转换为大写字母,同时删除空格和标点符号),比较两者,并确定其是否为回文 到目前为止,我已经纠正了代码中的一个又一个错误,但现在我只纠正了三个(目前)。两个是在Stk的第5行“无法访问。\Stk.java”和“无法访问。\IStk.java”(是的,我注意到它可以访问它,然后突然无法访问) 第三个错误是在Java 编译器错误我可以';我好像没办法,java,Java,我的代码有三个问题。该代码设计用于从键盘读入字符串,将其转换为堆栈(将小写字母转换为大写字母,同时删除空格和标点符号),比较两者,并确定其是否为回文 到目前为止,我已经纠正了代码中的一个又一个错误,但现在我只纠正了三个(目前)。两个是在Stk的第5行“无法访问。\Stk.java”和“无法访问。\IStk.java”(是的,我注意到它可以访问它,然后突然无法访问) 第三个错误是在s=infle.nextline()行中找不到句点符号此特定实例位于第29行 这是主文件的代码 import java
s=infle.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调用push方法时也会崩溃!当我不那么忙的时候,我会试着检查问题,但你自己也要做
你真的应该花些时间来学习Java编程、面向对象编程(OOP)等的基础知识。这里有很多非常基本的错误
以下是文件:
提示:标识符大小写很重要。如果可以,请查找Scanner.nextline
。Stk
希望存储在palindromes
目录中,但您没有为其提供导入palindromes
类…s!=“Q”
也不是你想要的。将字符串与.equals()
进行比较,将while(s2.isEmpty()!=TRUE)更改为while(!s2.isEmpty())删除代码>来自而(s!=“Q”)
并在(!s.equals(“Q”)
时使用更新该特定行。如果您使用do…while(condition)
,它会更好,因为您需要至少执行一次程序。+1表示“您真的应该花一些时间学习更多Java编程的基础知识…”-1表示问题,这没有用,基本上是“教我如何编写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 ();
}