按特定顺序打印年龄。 [英] Printing the age in specific order.

查看:70
本文介绍了按特定顺序打印年龄。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用任何排序算法按升序打印他们的年龄。

我应该在哪里开始算法?



我尝试过:



包客户;

import java.util.Scanner;

public class Customer {// properties



private String Name;

private int Salary;

private int年龄;

私有字符串部门;



void setName(字符串名称)// setter和getter方法

{

this.Name = Name;}

String getName()

{

return Name;}



void setSalary(int Salary){

this.Salary = Salary;

}

int getSalaray(){

返回工资;}



void setAge(int Age){

this.Age =年龄;}

int getAge()

{

返回年龄;}



void setDepartment(String Department){

this.Department = Department;}

字符串部门()

{

返回部门;}



客户(String Name,int Age,int Salary,String Department)//构造函数

{

this.Name = Name;

this.Age = Age;

this.Salary =薪水;

this.Department = Department;

}





void Setting(){//方法或函数

System.out.println(Name:+ Name);

System.out。 println(薪水:+年龄);

System.out.println(年龄:+薪水);

System.out.println(部门:+部门;

System.out.println(\ n);}







public static void main(String [] args){

Customer [] array = new Customer [6];

array [0] = new Customer(Rashika,2000,18,Computer Science);

array [1] =新客户(Ayesha,1000,20,英文);

array [2] =新客户(Aleena,5000,29,数学);

array [3] =新客户(Aimen,8000,58,Humanities);

array [4] =新客户(Amna,3000, 28,计算机科学);

array [0] .Setting();

array [1] .Setting();

array [2] .Setting();

array [3] .Setting();

array [4] .Setting();

}

}

I want to print their ages in ascending order using any sorting algorithm.
Where should I start the algorithm?

What I have tried:

package customer;
import java.util.Scanner;
public class Customer { //properties

private String Name;
private int Salary;
private int Age;
private String Department;

void setName(String Name) //setter and getter methods
{
this.Name =Name;}
String getName()
{
return Name;}

void setSalary(int Salary){
this.Salary = Salary;
}
int getSalaray(){
return Salary;}

void setAge(int Age){
this.Age = Age;}
int getAge()
{
return Age;}

void setDepartment(String Department){
this.Department= Department;}
String Department()
{
return Department;}

Customer(String Name, int Age, int Salary, String Department) //constructor
{
this.Name =Name;
this.Age = Age;
this.Salary = Salary;
this.Department = Department;
}


void Setting(){ //method or function
System.out.println("Name: "+Name);
System.out.println("Salary: "+Age);
System.out.println("The age: "+Salary);
System.out.println("The department: "+Department);
System.out.println("\n");}



public static void main(String[] args) {
Customer[] array = new Customer[6];
array[0] = new Customer("Rashika" , 2000, 18, "Computer Science");
array[1] = new Customer("Ayesha" , 1000, 20, "English");
array[2] = new Customer("Aleena" , 5000, 29, "Maths");
array[3] = new Customer("Aimen" , 8000, 58, "Humanities");
array[4] = new Customer("Amna" , 3000, 28, "Computer Science");
array[0].Setting();
array[1].Setting();
array[2].Setting();
array[3].Setting();
array[4].Setting();
}
}

推荐答案

我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!

但是......你可能想看看你的设置功能:

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
But...you might want to look at your Setting function:
System.out.println("Salary: "+Age);
System.out.println("The age: "+Salary);

那些不对,以后可能会让你感到困惑......



如果您遇到特定问题,请询问相关问题,我们会尽力而为帮助。但是我们不打算为你做这一切!

Those are not right and may confuse you later...

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


OriginalGriff 指出你已经交换了age / salaray输出。使用构造函数添加新元素时也是这样做的:

OriginalGriff pointed out that you have swapped the age / salaray output. You have done that too when adding new elements using the constructor:
Customer(String Name, int Age, int Salary, String Department) //constructor
// ...
array[0] = new Customer("Rashika" , 2000, 18, "Computer Science");



关于排序阅读你的课程书和/或在网上搜索java sort class array之类的东西。提示:

关键字是​​Comparable interface和 compareTo()方法。


这篇关于按特定顺序打印年龄。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆