【发布时间】:2015-03-17 21:56:41
【问题描述】:
对于我的任务,我必须编写和测试一个 Java 程序以读取多个 输入行,直到读取空行。读完每一行后,我必须确定该行是否包含回文,如果确实包含回文,我必须打印它是哪种类型的回文(单词、短语或数字)。为了做回文部分,我必须使用伪代码。
伪代码为:
注意:以下符号代表赋值
左0
右字符串最后一个字符的位置
好的真的
虽然还好,但左
ch1 字符串中位置(左)的字符
如果 ch1 不是数字或字母
向左递增
其他
ch2 字符串中位置(右)的字符
如果 ch2 不是数字或字母
向右递减
其他
将ch1和ch2都转换为大写
如果 ch1 = ch2
向左递增
向右递减
其他
好的假
结束
结束
结束
结束
还行
到目前为止我所拥有的是:
import java.util.Scanner;
public class Project4
{
public static void main (String [] args)
{
String line = getInputLine();
while (!isEmptyLine (line))
{
if (isPalindrome (line))
System.out.println ("\"" + line + "\" is a palindrome and a " + getPalType (line));
else
System.out.println ("\"" + line + "\" is not a palindrome");
line = getInputLine();
}
System.out.println ("End of program");
}
public static String getInputLine()
{
System.out.println("Enter a line of input: ");
Scanner in = new Scanner(System.in);
String line = in.next();
System.out.println(line);
return line;
}
public static boolean isEmptyLine (String str)
{
boolean isEmptyLine;
if( str == null)
isEmptyLine = true;
else
isEmptyLine = false;
return true;
}
public static boolean isPalindrome (String str)
{
Scanner word = new Scanner(System.in);
String isPalindrome = word.next();
int strLength = isPalindrome.length();
while(true && 0 < isPalindrome.charAt(isPalindrome.length() -1))
{
if(Character.isDigit(strLength) || Character.isLetter(0))
{
我还没有完成,但我需要帮助来了解如何使用伪代码。我不太明白第一个 if 语句部分。如果有人有时间解释代码,我将不胜感激。
【问题讨论】:
标签: java pseudocode palindrome