在 repl.it 上反转 java 中的字符串

Reverse a string in java on repl.it

我需要一些代码方面的帮助。我正在尝试反转用户输入的字符串。有人可以有一些经验 repl.it 请帮助我。

这是我正在使用的代码(忽略已经有效的代码的第一部分)

主要

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);
        System.out.println("enter a number");
        int i = inp.nextInt();
        int o =i+i;
        System.out.println(o);
    }

    Scanner inp = new Scanner(System.in);
    System.out.print("In:");
    String word = inp.nextLine();
    int counter = word.length();
    for(counter>0){
        System.out.println(word.charAt(counter));
        int counter-1;
    }
}

这是我遇到的一长串错误

exit status 1
Main.java:16: error: <identifier> expected    
    System.out.print("In:");   
                    ^    
Main.java:16: error: illegal start of type    
    System.out.print("In:");    
                     ^    
Main.java:19: error: illegal start of type   
    for(counter>0){    
    ^    
Main.java:19: error: <identifier> expected    
    for(counter>0){    
               ^    
Main.java:19: error: ';' expected    
    for(counter>0){    
                ^    
Main.java:19: error: illegal start of type    
    for(counter>0){    
                 ^    
Main.java:19: error: <identifier> expected    
    for(counter>0){    
                  ^    
Main.java:19: error: ';' expected    
    for(counter>0){    
                   ^    
Main.java:20: error: illegal start of type    
      System.out.println(word.charAt(counter));    
            ^    
Main.java:20: error: ';' expected    
      System.out.println(word.charAt(counter));   
                ^    
Main.java:20: error: invalid method declaration; return type required    
      System.out.println(word.charAt(counter));    
                 ^   
Main.java:20: error: <identifier> expected    
      System.out.println(word.charAt(counter));    
                                    ^    
Main.java:20: error: ';' expected    
      System.out.println(word.charAt(counter));    
                                     ^    
Main.java:20: error: illegal start of type    
      System.out.println(word.charAt(counter));   
                                            ^   
Main.java:20: error: <identifier> expected    
      System.out.println(word.charAt(counter));   
                                             ^   
Main.java:21: error: ';' expected    
    int counter-1;    
               ^   
Main.java:24: error: class, interface, or enum expected  
    }   
    ^    
17 errors
public static void main(String [] args) {
    try (Scanner scanner = new Scanner(System.in)) {
        System.out.println("In: ");
        String word = scanner.nextLine();
        System.out.println(new StringBuilder(word).reverse());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这里是固定的最终结果

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {

    Scanner inp = new Scanner(System.in);
    System.out.println("enter a number");
    int i = inp.nextInt();
    int o =i+i;
    System.out.println(o);
  Scanner in = new Scanner(System.in);
  System.out.print("In:");
    String word = in.nextLine();
    //write your code below
    int counter = word.length();
    while(counter>0){
      System.out.print(word.charAt(counter-1));
      counter--;
    }

  }

}

此错误背后的原因是您将代码放在 main 方法之外。您要执行的每个代码都必须属于方法 public static void main.

修复方法如下:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);
        System.out.println("enter a number");
        int i = inp.nextInt();
        int o =i+i;
        System.out.println(o);

        System.out.print("In:");
        String word = inp.nextLine();
        int counter = word.length();
        for(counter>0){
            System.out.println(word.charAt(counter));
            int counter-1;
        }

    }    
}