A Java program to get roll from user and there it is giving an error while compiling the code as marking to School object

huangapple go评论68阅读模式
英文:

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

  1. 你的类名是"school",但在创建sc对象时使用了"School"。
  2. getRollno缺少其左花括号,并返回"roll no"而不是"rollno",并且还接受了输入,原因不明。
  3. 在main方法中,你说sc.setRollno()没有输入任何参数,尽管它需要一个rollno。
  4. 对于public static void main,你将public的首字母大写为"Public",而不是小写的"public"。

此外,我建议在编写代码时使用制表符,因为这样可以使代码不仅对你自己可读,还对其他人阅读代码也更易理解。

英文:

I see multiple errors in the current code given.

  1. your class name is "school" but then when creating the sc object you used "School"
  2. getRollno is missing its opening flower bracket and also returns "roll no" instead of "rollno" and is also accepting input for some reason
  3. in the main method, you said sc.setRollno() without inputting any arguments, even though it is expecting a rollno
  4. 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;
        }
    }
    
}

huangapple
  • 本文由 发表于 2020年8月7日 15:05:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63296843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定