【问题标题】:Java saying everything is palindromeJava 说一切都是回文
【发布时间】:2017-10-19 05:33:06
【问题描述】:

我正在编写一个程序,它从一个文件中读取文本,然后确定它是否是回文。一个问题是,它认为一切都是一。这是我的代码:

   String input = "";
   String reversed = "";
   String answer = "";
   int count = 0;
   int low = 0;
   int high = input.length() - 1;

   outF.println("Palindrome   Input String            New String");

   while (inF.hasNext()){
       input=inF.nextLine();
       reversed = new StringBuffer(input).reverse().toString();
        boolean isPalindrome;
       while (low < high) {
         isPalindrome = true;
           if (input.charAt(low) != input.charAt(high)) {
            isPalindrome = false;
            break;
         }
         low++;
         high--;

       }
       if (isPalindrome = true){
          answer = "yes";}
       else{
           answer = "no";}

       outF.printf("  %-10s %-23s %s %n", answer, input.trim(), reversed.trim());

       count++;
   }

这是我的输出:

Palindrome   Input String            New String
  yes        STAR RATS               STAR RATS 
  yes        STAT                    TATS 
  yes        LEVEL                   LEVEL 
  yes        MALAYALAM               MALAYALAM 
  yes        Solos                   soloS 
  yes        kaya                    ayak 
  yes        Radar                   radaR 
  yes        Regal lageR             Regal lageR 
  yes        Straw wartS             Straw wartS 
  yes        RSTUVWXYZXWVUTSR        RSTUVWXZYXWVUTSR 
  yes        DeifieD                 DeifieD 
  yes        ABBA                    ABBA 
  yes        mama anna mama          amam anna amam 
  yes        noel leon               noel leon 
  yes        Racecar                 racecaR 
  yes        galleonnoelag           galeonnoellag 
end of program 16 strings were processed

有人知道这可能是什么原因吗?我正在按照我的书所说的来找出某件事是否是回文

我移动了一些东西,看起来像这样: 布尔 isPalindrome = true; 而(低

           if (input.charAt(low) != input.charAt(high)) {
            isPalindrome = false;
            break;
         }
         low++;
         high--;

       }
       if (isPalindrome ){
          answer = "yes";}
       else{
           answer = "no";}

       outF.printf("  %-10s %-23s %s %n", answer, input.trim(), reversed.trim());

       count++;

它仍然无法正常工作。如果我让它保持原样,它会试图说 isPalindrome 没有定义。

【问题讨论】:

  • 我对这个问题投了反对票,因为没有证据表明对此代码执行了任何调试。请edit您的问题向我们展示您的调试发现了什么,以及关于特定代码行的特定问题。请参阅:How to create a Minimal, Complete, and Verifiable exampleHow to Debug Small Programs
  • 查找变量范围。如果你在循环内定义一个变量,它在循环外是不可用的。
  • @JJJ 他在循环内定义了什么变量并试图在循环外使用?
  • 提示:在while (low &lt; high) 循环中放置一个System.out.println(),并显示一些重要的变量。你看到的——或者你没有看到的——可能会帮助你弄清楚为什么这不起作用。

标签: java palindrome


【解决方案1】:
while (inF.hasNext()){
   input=inF.nextLine();
   reversed = new StringBuffer(input).reverse().toString();
    boolean isPalindrome;
int low = 0;
int high = input.length() - 1;

   while (low < high) {
     isPalindrome = true;
       if (input.charAt(low) != input.charAt(high)) {
        isPalindrome = false;
        break;
     }
     low++;
     high--;

   }

在while循环中声明lowhigh变量。

【讨论】:

    【解决方案2】:

    您的代码有两个问题:

    1. 您在 while() 循环中声明了 isPalendrome。因此,您无法从循环外部访问它。只需将 boolean isPalindrome; 移出您的 while() 循环即可。
    2. 您没有将任何变量与此语句进行比较 if (isPalindrome = true){。使用单个 = 可以为变量赋值。因此,您的 if 语句始终为真。要比较两个变量,您必须使用 ==:if (isPalindrome == true){

    编辑:我还发现了一些其他问题:

    1. high = input.length() - 1; 在您的程序中始终为 -1。此时它必须在 while() 循环内为 high = 0;high = input.length() - 1;,以便在每次运行 while() 循环时将其设置为 length-1。
    2. 您必须在每次运行后重置为低电平:您必须在 while 循环内声明 low = 0;

    现在它非常适合我。

    【讨论】:

    • 如果 #1 有问题,他会得到语法错误,而不是错误的结果。他声明isPalindrome的地方没有问题。另外,您对#2的看法是对的,但他似乎解决了这个问题。还有一个更严重的错误你没有注意到。
    猜你喜欢
    • 1970-01-01
    • 2014-06-07
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多