我已经完成了相反的部分,但是我在连字符方面遇到了麻烦。任何帮助表示赞赏!另外,到目前为止的代码。
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.print( "Enter a string of words that contains a hyphen: ");
String word = kbd.next();
for (int i = word.length()-1; i >= 0; i--) {
System.out.print(word.charAt(i));
}
}
输入示例:
low-budget
要求的输出:
tegdub (the reverse of the part after the hyphen)
java大神给出的解决方案
这是我能想到的最简单的解决方案(当然还有其他更好的解决方案,但这是我的实现:
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.print( "Enter a string of words that contains a hyphen: ");
String word = kbd.next();
int loc = word.indexOf('-'); //Here I am trying to find the location of that hyphen
for (int i = word.length()-1; i > loc; i--) { //Now print the rest of the String in reverse TILL that location where we found hyphen. Notic i > loc
System.out.print(word.charAt(i));
}
System.out.print(" ");
for (int i = loc + 1; i < word.length(); i++) { //Now print the original String starting after the hyphen. Notice int i = loc + 1
System.out.print(word.charAt(i));
}
}
java:继承 - java有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…
Java-如何将此字符串转换为日期? - java我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…
从方法返回数组-Java - javaprivate static Coordinate[] getCircleCoordintaes() { Coordinate coordinates[] = {new Coordinate(0, 0)}; return coordinates; } 以上程序工作正常。在上面的程序中,返回的坐标数组首先初始化了数组使用这条线Coordinate coordi…
JAVA:如何检查对象数组中的所有对象是否都是子类的对象? - java我有一个对象数组。现在,我要检查所有这些对象是否都是MyObject的实例。有没有比这更好的选择:boolean check = true; for (Object o : justAList){ if (!(o instanceof MyObject)){ check = false; break; } } java大神给出的解决方案 如果您不喜欢循环,则…
如何使用BorderLayout(Java)扩展JTextField - java我有一个Java程序,其中使用的是JTextField,但如果我未指定默认大小,则其宽度为0。我将其插入BorderLayout中,因此如何制作它展开以填充整个容器? java大神给出的解决方案 在上面的示例中,文本字段将正常工作。但是,如果您插入EAST或WEST,则将不起作用。import java.awt.BorderLayout; import ja…