如何将String转换为String转换为String?

时间:2018-04-04 04:29:23

标签: java

import java.util.Scanner;
public class Lab7 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        System.out.println("Type the integer you'd like to be reversed:");

        int num = s.nextInt();
        String strNum = ""+num;
        reverse(s1);
        System.out.println(reverse(s1));
    }

    public static int reverse(int strNum) {

        String s1 = Integer.toString(strNum);
        for(int i=s1.length()-1;i>=0;i--){
              System.out.print(s1.charAt(i));
        }

        return 0;
    }
}

非常直接 - 我想扭转我对数字的输入,但我无法做到。

reverse(s1);”和“System.out.println(reverse(s1));”存在错误,因为无法识别s1。我无法弄清楚原因。

7 个答案:

答案 0 :(得分:1)

您的代码不会按原样编译。 s1不在主要方法中,reverse方法正在接受int而不是String。 另外,如果您需要从方法中返回int,那么为什么不是反向的int

您的代码编辑很少,可以解决您的问题。

  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);

    System.out.println("Type the integer you'd like to be reversed:");

    int num = s.nextInt();
    System.out.println(reverse(num));
}


public static int reverse(int num) {
    String strNum = String.valueOf(num);
    StringBuffer sb = new StringBuffer();

    for(int i=strNum.length()-1;i>=0;i--)
        sb.append(strNum.charAt(i));

    return Integer.parseInt(sb.toString());
   }

答案 1 :(得分:0)

你没有任何变量s1 您已将字符串编号的值指定为' strNum',请改用它 此外,您必须在reverse()

的定义中使用字符串作为参数类型
import java.util.Scanner;
public class Lab7 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        System.out.println("Type the integer you'd like to be reversed:");

        int num = s.nextInt();
        String strNum = ""+num;
        reverse(s1);
        System.out.println(reverse(s1));
    }

    public static int reverse(String strNum) {

        for(int i=strNum.length()-1;i>=0;i--){
              System.out.print(strNum.charAt(i));
        }

        return 0;
    }
}

答案 2 :(得分:0)

import java.util.Scanner;
public class Lab7 {
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);

    System.out.println("Type the integer you'd like to be reversed:");

    int num = s.nextInt();
    String strNum = ""+num;
    reverse(strNum);
}

public static int reverse(String strNum) {

    String s1 =strNum;
    for(int i=s1.length()-1;i>=0;i--){
          System.out.print(s1.charAt(i));
    }

    return 0;
}

}

答案 3 :(得分:0)

StringBuilder sb =  new StringBuilder("your string");
String result = sb.reverse().toString();

答案 4 :(得分:0)

// ===============try the below code===============================
public static void main(String sr[])
{
     Scanner s = new Scanner(System.in);

     System.out.println("Type the integer you'd like to be reversed:");

     int num = s.nextInt();
     String strNum = ""+num;
     String inputString = String.valueOf(num);
     StringBuilder builder = new StringBuilder(inputString);
     System.out.println(builder.reverse());
}

答案 5 :(得分:0)

您可以轻松使用堆栈,下面是使用堆栈反转字符串的简单实现。您可以轻松更改Integer的代码,复杂度为O(n),这是我认为的好。

host A

答案 6 :(得分:-1)

import java.util.Scanner;
public class Lab7 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Type the integer you'd like to be reversed:");
        int num = s.nextInt();
        System.out.println(new StringBuilder(String.valueOf(num)).reverse().toString());
    }
}

我们可以使用stringBuffer或StringBuilder进行反转