简体   繁体   English

Java回文,方法和抽象类?

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

I'm stuck on an assignment, largely due to an extreme lack of examples or even relevant diagrams from my textbook and class material. 我受困于作业,很大程度上是因为我的教科书和课堂资料中缺少示例甚至相关的图表。

The reason I structured the program the way I did is because I'm required to use 4 methods: a main method that executes all the other methods, a retrieve input method, a check method, and a display method. 之所以以这种方式构造程序,是因为我必须使用4种方法:执行所有其他方法的main方法,retrient input方法,check方法和display方法。 I love to hear about best practices, but I'm forced to code like this. 我喜欢听到有关最佳做法的信息,但是我不得不这样做。

My main problem is the abstract classes I have. 我的主要问题是我拥有的抽象类。 Any variables I write in one method won't be resolvable in another, I don't know how to make the variables global. 我用一种方法编写的任何变量都不能用另一种方法解析,我也不知道如何使变量全局化。

secondly, the code does not compile, the example I've found doesn't have a classic main, i don't really know how to make main implement methods, or make the compiler happy with abstraction. 其次,代码无法编译,我发现的示例没有经典的main,我真的不知道如何制作main实现方法或使编译器对抽象满意。

also no clue on how to take my boolean result and use that to display the results in the display method. 也没有关于如何获取我的布尔结果并将其用于以display方法显示结果的线索。 yes its asinine, I'd rather just do it in the check method. 是的,它的优点是,我宁愿只在check方法中进行操作。

all i know for sue is that my "logic" so far works. 我所知道的是,到目前为止,我的“逻辑”仍然有效。 i think. 我认为。 any help to point me in the right direction would be very much appreciated. 任何帮助我指出正确方向的帮助将不胜感激。 if thee is a way to do this without abstract classes i'd love to hear it, i think the abstraction is unnecessary. 如果您希望在没有抽象类的情况下完成此操作,我想听听它,我认为抽象是不必要的。

well here's my monster so far: 好吧,到目前为止,这是我的怪物:

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. Move the variables outside methods and put directly in class scope 将变量移到方法外,然后直接放在类范围内
  2. Writing main method is the first thing you learn in java. 编写main方法是您在Java中学习的第一件事。 Look into your tutorial again 再次查看您的教程
  3. You can use a boolean variable boolean displayCheck = false; 您可以使用布尔变量boolean displayCheck = false; and set the same 并设置相同

And one question from my side: If your code doesn't compile what makes you feel that the logic is correct? 我这边的一个问题是:如果您的代码没有编译,是什么让您感到逻辑正确?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM