Java程序基于标记分割数组。

时间:2021-10-26 07:17:38

Create class Student with below attributes: rollNo Name Marks

创建具有以下属性的类学生:rollNo Name标记。

Create class StudentDemo with main method. Declare array of 5 student objects in main method. Initialize this array. Declare another method in this class – splitStudentArray. This method will take the student array and a character as input parameters. If the input character is ‘o’ this method will return array of students with odd value of marks. If the input character is ‘e’ then this method will return array of students with even value or marks. It will return null array if there is any other character specified. Display name and marks for this returned array from main method.

用主方法创建类学生演示。在主方法中声明5个学生对象数组。这个数组进行初始化。在这个类中声明另一个方法——splitStudentArray。该方法将以学生数组和字符作为输入参数。如果输入字符是“o”,该方法将返回具有奇数值的学生数组。如果输入字符是“e”,那么该方法将返回具有偶数或标记的学生数组。如果指定了其他字符,它将返回空数组。从main方法中显示返回的数组的名称和标记。

i have made the program but it is returning the reference of an array.

我已经做了这个程序,但它正在返回一个数组的引用。

Solution: package assignment2;

解决方案:计划任务;

public class StudentDemo {

公开课StudentDemo {

public static void main(String[] args) 

{
    Student[] st=new Student[5];
    Student st1=new Student(1, "abhi", 200.5);
    Student st2=new Student(2, "maggie", 200);
    Student st3=new Student(3, "suraj", 107);
    Student st4=new Student(4, "naveen", 249);
    Student st5=new Student(5, "jajan",100 );

    Student[] sts = {st1,st2,st3,st4,st5}; 
    System.out.println("splitStudentArray : " + splitStudentArray(sts, 'o'));
}

public static Student[] splitStudentArray(Student[] sts, char ch)
{
    int Marks = 0;
    if (ch=='o' && Marks == 1 || Marks==3 || Marks==5)
    {
    System.out.println("number is odd");
    }

    if (ch=='e'&& Marks == 2 || Marks==4 )
    {
     System.out.println("number is even");
    }
    return sts ;

}

}

}

class Student { private int rollNo; private String Name; private double Marks;

班级学生{private int rollNo;私人字符串名称;私人双标记;

public Student(int rollNo, String name, double marks) 
{

    this.rollNo = rollNo;
    this.Name = name;
    this.Marks = marks;
}


public double getMarks() {
    return Marks;
}


public void setMarks(double marks) {
    Marks = marks;
}


public int getRollNo() {
    return rollNo;
}


public String getName() {
    return Name;
}

}

}

2 个解决方案

#1


0  

First of all, shouldn't this be on * rather than here?

首先,这不是*而不是这里吗?

Secondly - if I understand what you're trying to do correctly, in your splitStudentArray(...) you should do something like:

第二,如果我理解了你想要做的正确的事情,在你的splitStudentArray(…)中,你应该做如下的事情:

 ArrayList<Students> helper = new ArrayList<>();
 int j = 0;
   for(int i; i < sts.length; i++){
      if(checkIfOdd(sts[i]) == true){
      helper[j] = sts[i]
      j++;
   }

and then you return helper instead of sts.

然后你返回助手而不是sts。

Cheers

干杯

#2


0  

You are passing in "sts" and returning the same thing you are passing in "sts". You are giving a method an apple just to get an apple. Just keep the apple. No. What do you want back, is what you should give back. You are only returning "sts". If you want to return something, then return something instead of just printing to the screen.

您正在传递“sts”并返回您在“sts”中传递的相同的东西。你给了一个方法一个苹果只是为了得到一个苹果。把苹果。不。你想要的是什么,是你应该回报的。你只是返回“sts”。如果您想要返回某个东西,那么返回一些东西,而不是直接打印到屏幕上。

/**
*   Hint!
*   EVEN: using modulus:  if mynumber%2==0
*   This could be useful in returning ODD and EVEN arrays too.
*
*   @param1 ...
*   @param2 ...
*   @param3 ...
**/
public static Student[] splitStudentArray(Student[] sts, char ch)
{
    int Marks = 0;
    if (ch=='o' && Marks == 1 || Marks==3 || Marks==5)
    {
      System.out.println("number is odd");
      //return oddArray(str)  create function or insert for loop here
    }
    else if (ch=='e'&& Marks == 2 || Marks==4 )
    {
      System.out.println("number is even");
      //return evenArray(str) create function or insert for loop here
    }
    else
      return null;
}

#1


0  

First of all, shouldn't this be on * rather than here?

首先,这不是*而不是这里吗?

Secondly - if I understand what you're trying to do correctly, in your splitStudentArray(...) you should do something like:

第二,如果我理解了你想要做的正确的事情,在你的splitStudentArray(…)中,你应该做如下的事情:

 ArrayList<Students> helper = new ArrayList<>();
 int j = 0;
   for(int i; i < sts.length; i++){
      if(checkIfOdd(sts[i]) == true){
      helper[j] = sts[i]
      j++;
   }

and then you return helper instead of sts.

然后你返回助手而不是sts。

Cheers

干杯

#2


0  

You are passing in "sts" and returning the same thing you are passing in "sts". You are giving a method an apple just to get an apple. Just keep the apple. No. What do you want back, is what you should give back. You are only returning "sts". If you want to return something, then return something instead of just printing to the screen.

您正在传递“sts”并返回您在“sts”中传递的相同的东西。你给了一个方法一个苹果只是为了得到一个苹果。把苹果。不。你想要的是什么,是你应该回报的。你只是返回“sts”。如果您想要返回某个东西,那么返回一些东西,而不是直接打印到屏幕上。

/**
*   Hint!
*   EVEN: using modulus:  if mynumber%2==0
*   This could be useful in returning ODD and EVEN arrays too.
*
*   @param1 ...
*   @param2 ...
*   @param3 ...
**/
public static Student[] splitStudentArray(Student[] sts, char ch)
{
    int Marks = 0;
    if (ch=='o' && Marks == 1 || Marks==3 || Marks==5)
    {
      System.out.println("number is odd");
      //return oddArray(str)  create function or insert for loop here
    }
    else if (ch=='e'&& Marks == 2 || Marks==4 )
    {
      System.out.println("number is even");
      //return evenArray(str) create function or insert for loop here
    }
    else
      return null;
}