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

问题描述 投票:-1回答:3

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

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

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);
    }

    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
java string reverse repl.it
3个回答
0
投票
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();
    }
}

0
投票

这是固定的最终结果

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--;
    }

  }

}

0
投票

导致此错误的原因是,您将代码置于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;
        }

    }    
}
© www.soinside.com 2019 - 2024. All rights reserved.