簡體   English   中英

Java回文,方法和抽象類?

[英]Java Palindrome, methods, and abstract classes?

我受困於作業,很大程度上是因為我的教科書和課堂資料中缺少示例甚至相關的圖表。

之所以以這種方式構造程序,是因為我必須使用4種方法:執行所有其他方法的main方法,retrient input方法,check方法和display方法。 我喜歡聽到有關最佳做法的信息,但是我不得不這樣做。

我的主要問題是我擁有的抽象類。 我用一種方法編寫的任何變量都不能用另一種方法解析,我也不知道如何使變量全局化。

其次,代碼無法編譯,我發現的示例沒有經典的main,我真的不知道如何制作main實現方法或使編譯器對抽象滿意。

也沒有關於如何獲取我的布爾結果並將其用於以display方法顯示結果的線索。 是的,它的優點是,我寧願只在check方法中進行操作。

我所知道的是,到目前為止,我的“邏輯”仍然有效。 我認為。 任何幫助我指出正確方向的幫助將不勝感激。 如果您希望在沒有抽象類的情況下完成此操作,我想聽聽它,我認為抽象是不必要的。

好吧,到目前為止,這是我的怪物:

import javax.swing.JOptionPane;

interface Palindrome {

void retrieveInput(String[] args);
boolean Check(String s);
void display();

}

abstract class Sub_Palindrome  implements Palindrome {

public void retrieveInput(String[] args)
{
    String Uinput;
    int number1;
    int digit1; // first digit
    int digit2; // second digit
    int digit3;
    int digit4; // fourth digit
    int digit5; // fifth digit
    Uinput = JOptionPane.showInputDialog("Enter A 5 Digit Integer");

    try { //Sanitize user input, make sure input entered is a number
        number1 = Integer.parseInt(Uinput);
    } catch (NumberFormatException String) {
        JOptionPane.showMessageDialog(null, "Input invalid, please enter an integer",
                "///-D-A-T-A---E-R-R-O-R-\\\\\\", JOptionPane.ERROR_MESSAGE);
        return;
    }
    if (number1 < 10000 || number1 > 99999) { //Sanitize user input, make sure the given number is between 10000 and 99999
        JOptionPane.showMessageDialog(null, 
                "The number entered must be between 10000 and 99999",
                "///-D-A-T-A---E-R-R-O-R-\\\\\\", JOptionPane.ERROR_MESSAGE);
        return;
    }


}

public boolean Check(String s)
{ 
     digit1 = number / 10000;
     digit2 = number / 1000 % 10;
     digit3 = number % 1000 / 100 % 10; // is the third digit even necessary?
     digit4 = number % 10000 % 1000 % 100 / 10;
     digit5 = number % 10000 % 1000 % 100 % 10;

     if (digit1 == digit5 && digit2 == digit4)
         return true;
     else
         return false;

}   

public void display()
{

    //display output text based upon result from IsPalinDrome
    //after displaying result, start from the beginning and ask user to input data again

}

}
  1. 將變量移到方法外,然后直接放在類范圍內
  2. 編寫main方法是您在Java中學習的第一件事。 再次查看您的教程
  3. 您可以使用布爾變量boolean displayCheck = false; 並設置相同

我這邊的一個問題是:如果您的代碼沒有編譯,是什么讓您感到邏輯正確?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM