Java 函数方法,学生分数
我的代码运行良好,但我必须将其放入过程方法中。我不知道怎么做,因为有两个学生。我必须做三个签名吗?您不需要有效的变量。将标记重命名为compareMarks,并将其返回类型更改为void。命名约定规定方法名称应该是动词。最后,只需调用main中的方法。尽量使代码缩进良好。它使它更具可读性Java 函数方法,学生分数,java,Java,我的代码运行良好,但我必须将其放入过程方法中。我不知道怎么做,因为有两个学生。我必须做三个签名吗?您不需要有效的变量。将标记重命名为compareMarks,并将其返回类型更改为void。命名约定规定方法名称应该是动词。最后,只需调用main中的方法。尽量使代码缩进良好。它使它更具可读性 public static void main(String args[]) { Scanner sc = new Scanner(System.in); int x, y; Syst
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;
}
你的代码试图做什么?你为什么要加入一个方法?这个方法应该做什么?宝贝,总是给人们缩进的代码。一般来说,我甚至不看未插入的代码。它不可读。相信我,现在就习惯吧——不要养成给代码缩进不正确的习惯。你在哪里声明有效?你永远不会在main方法中调用方法标记它可以在main方法中调用,你只是现在没有调用它。在第二个sc.nextInt之后,需要调用marksx,y;如果使用eclipse缩进代码,请按Control+Shift+f。它的用途是什么。您不会在方法中的任何位置更改它。这就像说return1。试着理解方法签名。int和void是该方法的返回类型。如果您没有返回任何内容,则该词组应为空。请参阅此链接:您是对的!!您的方法不需要返回任何内容,因为它正在计算结果并自己打印输出,这些是您传递给该方法的参数。因为你想比较两个学生的分数,所以你不需要名誉来提问。只有那些对论坛有价值的问题才是投票候选人
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");
}
}