Walter Savitch的Java书中有一些作业,它要求你编写一些代码来反转输入单词的顺序。我想出了以下内容,我想知道我是否能够优化它,因为它似乎有点沉重:
public static void main(String[] args) {
String statement;
System.out.print("Enter a statement to reverse: ");
statement = keyboard.nextLine();
int n;
String finalWord = "";
String letter;
for (n = statement.length(); n > 0; n--)
{
letter = statement.substring(0, 1);
finalWord = letter + finalWord;
statement = statement.substring(1);
System.out.println(finalWord);
}
System.out.println("Final work: " + finalWord);
任何见解都将受到赞赏。 }
答案 0 :(得分:-1)
#define DllExport __declspec( dllexport )
class DllExport C {
int i;
virtual int func( void ) { return 1; }
};
答案 1 :(得分:-1)
您可以使用下面给出的代码来反转字符串
public class ReversString
{
public static void main(String args[])
{
String name = "Vinayak Dwivedi";
String reverseStrinf = "";
for(int i = name.length() - 1;i >= 0 ;i--)
{
reverseStrinf = reverseStrinf + name.charAt(i);
}
System.out.println("reverseStrinf:-"+reverseStrinf);
}
}