功能方法,学生标记

时间:2014-10-29 00:47:13

标签: java

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;
}

我的代码工作正常,但我必须把它放在一个过程方法中。我不知道怎么做,因为有两个学生。我必须做三个签名吗?

1 个答案:

答案 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");
      }
   }