我无法解析为变量.我正在编写一个将字符串作为参数反转的 Java 程序 [英] i cannot be resolved to a variable.i am writing a java program which reverses a string as argument

查看:37
本文介绍了我无法解析为变量.我正在编写一个将字符串作为参数反转的 Java 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package String;

public class A {


    public static void main(String[] args) {
    System.out.println(reverse("java"));
    System.out.println(reverse("yuvaraj"));
    }
    public static String reverse(String s)
    {
    int len=s.length();
    String s1=" ";

    for(int i=len-1;i>=0;--i);
    {
        s1+=s.charAt(i);//in this line problem occurs
    }
    return s1;
    }
}

我在 charAt(i) 的i"中遇到问题

i am getting a problem in "i" of charAt(i)

推荐答案

去掉循环后的分号

for(int i=len-1;i>=0;--i);   // is causing the issue

循环后的分号导致循环体在那里结束而不做任何类似 for(int i=len-1;i>=0;--i){} 没有体的事情,因为这行 s1+=s.charAt(i); 不在循环体中,导致您遇到错误.所以改成

The semicolon after the loop causing the loop body to ends there without doing anything like for(int i=len-1;i>=0;--i){} no body and because of this the line s1+=s.charAt(i); is not in loop body causing the error you got. So change it to

for(int i=len-1;i>=0;--i)

演示

这篇关于我无法解析为变量.我正在编写一个将字符串作为参数反转的 Java 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆