英文:
A Java program to get roll from user and there it is giving an error while compiling the code as marking to School object
问题
以下是翻译好的代码部分:
public class school {
int rollno;
public void setRollno(int r) {
rollno = r;
}
public int getRollno() {
return rollno;
}
}
class enca {
public static void main(String[] args) {
school sc = new school();
sc.setRollno();
System.out.println(sc.getRollno());
}
}
请注意,我已经修复了代码中的一些错误,包括将 public
改为小写、修复了 getRollno
方法的参数和返回语句。请根据需要进一步调试和完善代码以解决其他错误。
英文:
I have written a code in java-
public class school
{
int rollno;
public void setRollno(int r)
{
rollno = r;
}
public int getRollno(int r)
return roll no;
}
}
class enca
{
Public static void main (String []args)
{
School sc = new School();
sc.setRollno();
System.out.println(sc.getRollno());
}
}
A Java program to get roll from user and there it is giving an error while compiling the code as marking to School object
There is error in School object ...
So how do I solve it?
答案1
得分: 1
- 你的类名是"school",但在创建sc对象时使用了"School"。
- getRollno缺少其左花括号,并返回"roll no"而不是"rollno",并且还接受了输入,原因不明。
- 在main方法中,你说sc.setRollno()没有输入任何参数,尽管它需要一个rollno。
- 对于public static void main,你将public的首字母大写为"Public",而不是小写的"public"。
此外,我建议在编写代码时使用制表符,因为这样可以使代码不仅对你自己可读,还对其他人阅读代码也更易理解。
英文:
I see multiple errors in the current code given.
- your class name is "school" but then when creating the sc object you used "School"
- getRollno is missing its opening flower bracket and also returns "roll no" instead of "rollno" and is also accepting input for some reason
- in the main method, you said sc.setRollno() without inputting any arguments, even though it is expecting a rollno
- for public static void main, you maid your public capitalized as "Public" instead of "public"
also, I recommend using tabs when creating your code because it makes it readable for not just yourself but also for others reading your code
答案2
得分: 0
同意 @slayerofthend
这可能是你需要的答案。你可以与你的代码进行比较。
public class Enca {
public static void main(String[] args) {
School sc = new School();
sc.setRollno(1);
System.out.println(sc.getRollno());
}
public static class School {
int rollno;
public void setRollno(int r) {
rollno = r;
}
public int getRollno() {
return rollno;
}
}
}
英文:
agree with @slayerofthend
and this is the answer u need maybe. u can compare with urs.
public class Enca {
public static void main(String[] args) {
School sc = new School();
sc.setRollno(1);
System.out.println(sc.getRollno());
}
public static class School {
int rollno;
public void setRollno(int r) {
rollno = r;
}
public int getRollno() {
return rollno;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论