关于java的问题,我编了个程序,老出错

来源:百度知道 编辑:UC知道 时间:2024/05/21 07:55:08
Public class Length
{
Public static void main(arg[])
{
String word="home";
String middle;
int i=word.length()/2;
if(word.length()%2==1)
middle=word.substring(i,i+1);
else
middle=word.substring(i-1.i+1);
System.out.print(middle);
}
}
把main函数改了还是有错

错误有三处,首先是Public 的首字母p是小写的.即public
第二就是main函数的参数问题.
第三就是word.substring(i-1.i+1); 处中间是逗号,而不是点.改后正确!

1,public 第一个字母不要大写
2,middle=word.substring(i-1,i+1); //这里要是,不是.
我改后的在下面,自己看
public class test
{
public static void main(String arg[])
{
String word="home";
String middle;
int i=word.length()/2;
if(word.length()%2==1)
middle=word.substring(i,i+1);
else
middle=word.substring(i-1,i+1);
System.out.print(middle);
}
}

main 函数是这么写的:Public static void main(String arg[])

if(word.length()%2==1) 是不是应该是:if(word.length()/2==1)