她的错误是什么:(
انابحاجهاليحلهذاالسؤالفكرتةفيطباعهالحروفالمتكررةفمجملةيدخلهاالمستخدم
public class Checker {
Scanner Scan = new Scanner(System.in);
int x;
public Checker()
{
System.out.print("Enter The Word ; ");
String Word = Scan.nextLine();
System.out.print("Enter The character : ");
char Char = Scan.next().charAt(0);
}
for( int i = 0 ; i < Word.lenght ; i++ )
{
char Ch = Word.charAt(i);
if(Ch == Char )
x++ ;
}
System.out.println(x);
}
答案 0 :(得分:1)
你需要在main / check等方法中将所有代码包装在构造函数之外。像:
Scanner Scan = new Scanner(System.in);
int x;
String Word;
char Char;
public Checker()
{
System.out.print("Enter The Word ; ");
Word = Scan.nextLine();
System.out.print("Enter The character : ");
Char = Scan.next().charAt(0);
}
public void check() {
for( int i = 0 ; i < Word.lenght ; i++ )
{
char Ch = Word.charAt(i);
if(Ch == Char )
x++ ;
}
System.out.println(x);
}
public static void main(String args[]) {//lets use Checker now to check
Checker c = new Checker();
c.check();
}
答案 1 :(得分:0)
我也不理解你的问题;从查看你的代码;缺少一件事(但无关紧要):“x”未在代码中初始化。