有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java试图反转句子中的字符

    Scanner input = new Scanner(System.in);
    String sentence , invertedsentence = "";
    int total , x;
    
    System.out.print("Enter your sentence:");
    sentence = input.nextLine();
    
    total = sentence.length();

I am trying to invert the characters from my input

    for ( x = total-1 ; x < 1 ; x--){
        invertedsentence = invertedsentence + sentence.charAt(x);
    }
    

The output shows blank

    System.out.println(invertedsentence);

共 (4) 个答案

  1. # 1 楼答案

    你试过:

    new StringBuilder(input).reverse().toString();
    

    这样就不那么复杂了

    如果你试图解决一个问题,你的for循环是错误的。第一个“x<;1”应该是x>;=0,因为您需要转到最后一个索引:

    for ( x = total-1 ; x >= 0 ; x--)
    

    完整示例:

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String sentence = "";
        int total , x;
    
        System.out.print("Enter your sentence:");
        sentence = input.nextLine();
        total = sentence.length();
    
        String invertedsentence = "";
    
        for ( x = total-1 ; x >= 0 ; x--){
            invertedsentence += (Character.toString(sentence.charAt(x)) + "");
        }
    
        System.out.println(invertedsentence);
    }
    
  2. # 2 楼答案

    逻辑是错误的。应该是的

    for ( x = total-1 ; x >= 0 ; x--){ 
        invertedsentence = invertedsentence + sentence.charAt(x);
    }
    

    此外,建议使用StringBuilder/StringBuffer(这是一个线程安全的实现),而不是直接连接字符串,因为每次连接字符串时,您正在创建字符串的新副本,并引用可能非常糟糕的内容,尤其是如果要反转的字符串很大,即使用以下内容:

    Scanner input = new Scanner(System.in);
    String sentence;
    StringBuilder invertedsentence = new StringBuilder();
    int total , x;
    
    System.out.print("Enter your sentence:");
    sentence = input.nextLine();
    
    total = sentence.length();
    for ( x = total-1 ; x >= 0 ; x--){
        invertedsentence.append(sentence.charAt(x));
    }
    System.out.println(invertedsentence.toString());
    
  3. # 3 楼答案

    我假设这是一个家庭作业,你的教授有兴趣教你如何操作数组

    字符串对象由一个字符数组支持。因此,需要颠倒数组中元素的顺序。剧透提示:您可以通过调用StringBuilder#reverse()方法轻松实现这一点,但这不是分配要求您完成的操作

    有两种解决方案应该适用于你的任务。一种是创建一个新数组,其中原始数组中的最后一个字符被放置在新数组的第一个索引中。因此,需要一个从字符串长度-1到零的循环,以便可以在原始数组的字符上移动指针

    第二个解决方案是就地完成。为此,您将交换第一个和最后一个元素,并将两个指针从数组末端向内移向中间。您需要一个临时char变量来保存其中一个值,以便完成交换。在这种情况下,将递增左指针(头部)并递减右指针(尾部),直到它们重叠(当左指针的值大于或等于右指针时)

    我不会编写代码,因为这是一个家庭作业,你需要自己解决这个问题。但这些信息足以让你实现这一目标

    更新:我改变了主意。以下是我上面描述的解决方案

    public class ReversedString {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter a word or sentence: ");
            String str = scanner.nextLine(); // i.e. Hello World!
            scanner.close();
            
            // in-place solution
            int headPtr = 0;
            int tailPtr = str.length() - 1;
            char temp;
            char[] arr = str.toCharArray();
            while (headPtr <= tailPtr) {
    
                temp = str.charAt(headPtr);
                arr[headPtr] = str.charAt(tailPtr);
                arr[tailPtr] = temp;
                headPtr++;
                tailPtr--;
            }
            
            String reversedString = new String(arr);
            System.out.println("Reversed String: " + reversedString); // !dlroW olleH
            
            
            // two-array solution
            char[] newArr = new char[arr.length];
            for (int i = arr.length - 1, j = 0; i >= 0; i--, j++) {
                newArr[j] = arr[i];
            }
            
            reversedString = new String(newArr);
            System.out.println("Reversed String: " + reversedString); // Hello World!
        }
    }
    

    显然,你只需要一个。虽然双数组解决方案看起来更干净,在大多数情况下可能还可以,但如果您处理的是非常大的字符串,它可能不是最佳解决方案,因为您使用了更多内存。就地解决方案解决了这一问题,因为所有数组操作都是在原始数组中完成的,方法是将其中一个字符移动到临时值,以便进行字符交换

    我相信这就是你的教授希望你能做的功课

  4. # 4 楼答案

    应该是x>=0

    for (int x = total-1 ; x >= 0 ; x--){
        invertedsentence = invertedsentence + sentence.charAt(x);
    }