函数解密器

huangapple go评论92阅读模式
英文:

Function Decipherer

问题

public class Decipherer {

    String arg = "aopi?sedohtém@#?sedhtmg+p9l!";

    int nbrChar = arg.length() / 2;

    String arg2 = arg.substring(5, nbrChar - 1);

    String arg3 = arg2.replace("@#?", " ");

    String arg4 = new StringBuilder(arg3).reverse().toString();

    public static void main(String[] args) {

        Decipherer mesage1 = new Decipherer();
        Decipherer message3 = new Decipherer();
        Decipherer message4 = new Decipherer();

    }
}

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor Decipherer() is undefined
The constructor Decipherer() is undefined
The constructor Decipherer() is undefined
Syntax error, insert "}" to complete ClassBody

at Decipherer.main(Decipherer.java:24)

I don't understand why it asks for another "}". Could someone explain it to me, please?
英文:

I'm a nooby on Java and in programmation too that's why I'm learning it 函数解密器 I need your help for an exercise. I need to create the function decipherer with some changes on my arg.

import java.util.StringBuffer;

public class Decipherer{

    String arg ="aopi?sedohtém@#?sedhtmg+p9l!";
    
    int nbrChar = arg.length()/2;

    String arg2 = arg.substring(5, nbrChar-1);

    String arg3 = arg2.replace("@#?", " ");

    String arg4 = new StringBuilder(arg3).reverse().toString();

    return arg4;

}

public static void main(String[] args) {

    Decipherer mesage1 = new Decipherer("0@sn9sirppa@#?ia'jgtvryko1");
    Decipherer message3 = new Decipherer("q8e?wsellecif@#?sel@#?setuotpazdsy0*b9+mw@x1vj");
    Decipherer message4 = new Decipherer("aopi?sedohtém@#?sedhtmg+p9l!");

}

--

> Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor Decipherer(String) is undefined
The constructor Decipherer(String) is undefined
The constructor Decipherer(String) is undefined
Syntax error, insert "}" to complete ClassBody

    at Decipherer.main(Decipherer.java:24)

I don't understand why it ask another "}". Could someone explain to me please?

答案1

得分: 0

以下是翻译好的部分:

主方法必须位于一个类内部。您可以将它放在Decipherer中,另外,您进行的操作必须放在一个方法内部,以便能够返回某些内容。您需要一个接受String的构造函数,例如:

public class Decipherer {

  private final String input;

  // 接受String的构造函数
  public Decipherer(String input) {
    this.input = input;
  }

  public String decipher() {

    int nbrChar = input.length() / 2;

    String arg2 = input.substring(5, nbrChar - 1);

    String arg3 = arg2.replace("@#?", " ");

    return new StringBuilder(arg3).reverse().toString();
  }

  // 类内的主方法
  public static void main(String[] args) {
    Decipherer decipherer1 = new Decipherer("0@sn9sirppa@#?ia'jgtvryko1");
    String message1 = decipherer1.decipher();
    Decipherer decipherer2 = new Decipherer("q8e?wsellecif@#?sel@#?setuotpazdsy0*b9+mw@x1vj");
    Decipherer decipherer3 = new Decipherer("aopi?sedohtém@#?sedhtmg+p9l!");
  }
}

通过执行new Decipherer("0@sn9sirppa@#?ia'jgtvryko1");,将创建一个类型为Decipherer的对象,带有字段input。然后,您可以在此对象上调用方法decipher()

或者,您还可以不使用构造函数,而是暴露一个静态方法来解密,例如:

public static String decipher(String input) {
    int nbrChar = input.length() / 2;
    String arg2 = input.substring(5, nbrChar - 1);
    String arg3 = arg2.replace("@#?", " ");
    return new StringBuilder(arg3).reverse().toString();
}

您可以在主方法中调用它,如final String message = Decipherer.decipher("0@sn9sirppa@#?ia'jgtvryko1");

英文:

The main method must be within a class. You could put it e.g. in Decipherer. In addition the operations that you are doing must be put inside a method to be able to return something. And you need a constructor accepting a String, e.g.

public class Decipherer {

  private final String input;

  // constructor accepting String
  public Decipherer(String input) {
    this.input = input;
  }

  public String decipher() {

    int nbrChar = input.length() / 2;

    String arg2 = input.substring(5, nbrChar - 1);

    String arg3 = arg2.replace("@#?", " ");

    return new StringBuilder(arg3).reverse().toString();
  }

  // main method inside class
  public static void main(String[] args) {
    Decipherer decipherer1 = new Decipherer("0@sn9sirppa@#?ia'jgtvryko1");
    String message1 = decipherer1.decipher();
    Decipherer decipherer2 = new Decipherer("q8e?wsellecif@#?sel@#?setuotpazdsy0*b9+mw@x1vj");
    Decipherer decipherer3 = new Decipherer("aopi?sedohtém@#?sedhtmg+p9l!");
  }
}

By executing new Decipherer("0@sn9sirppa@#?ia'jgtvryko1"); an object of type Decipherer is created with field input. You can then invoke method decipher() on this object.

Alternatively you can also go without constructor and expose a static method to decipher, e.g.

public static String decipher(String input) {
    int nbrChar = input.length() / 2;
    String arg2 = input.substring(5, nbrChar - 1);
    String arg3 = arg2.replace("@#?", " ");
    return new StringBuilder(arg3).reverse().toString();
  }

which you can call in your main method as final String message = Decipherer.decipher("0@sn9sirppa@#?ia'jgtvryko1");.

huangapple
  • 本文由 发表于 2020年5月2日 16:19:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61556366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定