public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int x, y;
System.out.println("Student 1 please enter your mark: ");
x = sc.nextInt();
System.out.println();
System.out.println("Student 2 please enter your mark: ");
y = sc.nextInt();
}
public static int marks(int x, int y) {
valid = 1;
if(x > 100 || y > 100) {
System.out.println("Invalid mark!! the marks has to be between 0-100");
} else if(x < y) {
System.out.println("Student 2 has higher mark than Student 1");
} else {
System.out.println("Student 1 has higher mark than Student 2");
}
return valid;
}
我的代码工作正常,但我必须把它放在一个过程方法中。我不知道怎么做,因为有两个学生。我必须做三个签名吗?
答案 0 :(得分:0)
您不需要valid
变量。将marks
重命名为compareMarks
并将其返回类型更改为无效。命名约定说方法名称应该是动词。最后只需在main中调用方法即可。尝试将代码缩进。它使它更具可读性
public static void main(String args [])
{
Scanner sc= new Scanner (System.in);
int x, y ;
System.out.println("Student 1 please enter your mark: ");
x= sc.nextInt();
System.out.println("Student 2 please enter your mark: ");
y=sc.nextInt();
compareMarks (x, y);
}
public static void compareMarks (int x, int y)
{
if (x>100 || y>100)
{
System.out.println("Invalid mark!! the marks has to be between 0-100");
}
else if (x<y)
{
System.out.println("Student 2 has higher mark than Student 1");
}
else
{
System.out.println("Student 1 has higher mark than Student 2");
}
}