创建对象并将值输入到用户,然后使用排序算法以升序打印它们。 [英] Creating objects and store the values input bu the user , then print those in ascending order using the sorting algorithm.

查看:78
本文介绍了创建对象并将值输入到用户,然后使用排序算法以升序打印它们。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将用户定义的值存储到对象中?

我应该在哪里插入排序算法?



< b>我尝试了什么:



How do I store the user defined values into the objects?
Where do I supposed to insert the sorting algorithm?

What I have tried:

package Question1;

import java.util.Scanner;
import java.util.*;

public class Question1 {
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 getDepartment()
{
return Department;}

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

}   
    public static void main(String[] args) {
        for (int i=0; i<=4; i++){
        Scanner s = new Scanner(System.in);
        System.out.print("Enter Name:");
        String Name =s.nextLine();
        
        Scanner s1 = new Scanner(System.in);
        System.out.print("Enter Salary:");
        int Salary = s1.nextInt();
        
        Scanner s2 = new Scanner(System.in);
        System.out.print("Enter Age:");
        int Age = s2.nextInt();
        
        Scanner s3 = new Scanner(System.in);
        System.out.print("Enter Department:");
        String Department = s3.nextLine();
        System.out.println("\n");
    }

    }
        
    }

推荐答案

Quote:

如何将用户定义的值存储到对象中?

How do I store the user defined values into the objects?

在Main方法中,您正在收集初始化<4的实例的值 Question1 (而不是infortunate名称, Employee 可能更好)class。

所以,创建一个 Question1 数组,并使用从用户收集的值初始化这些对象。





然后编写排序方法(例如,可以是 Question1 类的静态方法或不相关类的标准方法),接受作为参数,一个 Question1 对象的数组。

In your Main method you are collecting values for initializing 4 instance of the Question1 (rather infortunate name, Employee could be possibly better) class.
So, create an array of Question1 objects and initialize such objects with the values collected from the user.


Then write the sorting method (could be, for instance a static method of the Question1 class or a standard method of a unrelated class), accepting, as parameter, an array of Question1 objects.


Lesson:类和对象(Java™教程>学习Java语言) [ ^ ]


这篇关于创建对象并将值输入到用户,然后使用排序算法以升序打印它们。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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