编写Java程序,输入N个用户的姓名和电话号码,按照用户姓名的词典顺序排列输出用户的姓名和电话号码

编写Java程序,输入N个用户的姓名和电话号码,按照用户姓名的词典顺序排列输出用户的姓名和电话号码,第1张

如下:看看满不满足要求,不对追问。

import java.util.ArrayList

import java.util.Collections

import java.util.Comparator

import java.util.HashMap

import java.util.Scanner

public class B {

 

    public static void main(String[] args) {

     Scanner in = new Scanner(System.in)

     int n = in.nextInt()

     if(n > 50) {

  汪灶差   System.out.print("No more than 困皮50 contacts")

     in.close()

     return 

     }

     ArrayList<HashMap.Entry<String, String>> list

     HashMap<String, String> map = new HashMap<>()

     //String [][]contact = new String [n][2]

     for(int i = 0 i < n i ++) {

     String [] temp = new String [2]

     temp[0] = in.next()

     if(temp[0].length() > 10) {

     temp[0] = temp[0].substring(0, 10)

     }

     temp[1] = in.next()

     if(temp[1].length() > 10) {

     temp[1] = temp[1].substring(0, 10)

     }

     map.put(temp[0], temp[1])

     }

     in.close()

     list = new ArrayList<>(map.entrySet())

     Collections.sort(list, new Compare())

     for(int i = 0 i < n i ++) {

     System.out.println(format(list.get(i).getKey()) + format(list.get(i).getValue()))

     }

    }

    static String format(String s) {

     char [] c = new char[12 - s.length()]

     for(int i = 0 i < c.length i ++) {

     c[i] = '#'

     }

     return String.valueOf(c) + s

    }

    static class Compare implements Comparator<HashMap.Entry<String, String>> {

@Override

public int compare(HashMap.Entry<String, String> o1, HashMap.Entry<String, String> o2) {

int temp = o1.getKey().compareTo(o2.getKey())

if (temp > 0) {

return 1

} else if (temp == 0) {

return 0

} else {

return -1

}

}

}

}

结果辩和:

package test

public class Student {

private String name

private String id

private String clazz

private int age

private String address

/**

* sayHello方法唤晌枝

*/

public void sayHello() {

System.out.println("学号为" + this.id + "的同学的具体信息如下:")

System.out.println("姓名谨肢:" + this.name)

System.out.println("班级:" + this.clazz)

System.out.println("年龄:" + this.age)

System.out.println("家庭住址:" + this.address)

}

/**

* 测试方法

*

* @param args

*/

public static void main(String[] args) {

// 第一问

Student student = new Student()

student.setAddress("百度知道")

student.setAge(1)

student.setClazz("一班")

student.setId("071251000")

student.setName("lsy605604013")

student.sayHello()

// 第二问

Student studentNew = new Student()

studentNew.setAddress("搜搜知道")

studentNew.setAge(2)

studentNew.setClazz("二班")

studentNew.setId("和敏071251001")

studentNew.setName("lady")

if (student.getAge() <studentNew.getAge())

studentNew.sayHello()

else if (student.getAge() >studentNew.getAge())

student.sayHello()

else

System.out.println("两人一样大")

}

public String getName() {

return name

}

public void setName(String name) {

this.name = name

}

public String getId() {

return id

}

public void setId(String id) {

this.id = id

}

public String getClazz() {

return clazz

}

public void setClazz(String clazz) {

this.clazz = clazz

}

public int getAge() {

return age

}

public void setAge(int age) {

this.age = age

}

public String getAddress() {

return address

}

public void setAddress(String address) {

this.address = address

}

}

importjava.awt.*importjava.awt.event.*classShopFrameextendsFrameimplementsActionListener{Labellabel1,label2,label3,label4Buttonbutton1,button2,button3,button4,button5TextAreatextPanelpanel1,panel2staticfloatsum=0.0fShopFrame(Strings){super(s)setLayout(newBorderLayout())label1=newLabel("面纸:3元",Label.LEFT)label2=newLabel("钢笔:5元",Label.LEFT)label3=newLabel("液镇书:10元",Label.LEFT)label4=newLabel("袜子:8元",Label.LEFT)button1=newButton("加入购物车")button2=newButton("加入购物车")button3=newButton("加入购物车")button4=newButton("加入购物车")button5=newButton("查看购物车")text=newTextArea("商品有:"+"\n",5,10)text.setEditable(false)addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0)}})button1.addActionListener(this)button2.addActionListener(this)button3.addActionListener(this)button4.addActionListener(this)button5.addActionListener(this)panel1=newPanel()panel2=newPanel()panel1.add(label1)panel1.add(button1)panel1.add(label2)panel1.add(button2)panel1.add(label3)panel1.add(button3)panel1.add(label4)panel1.add(button4)panel2.setLayout(newBorderLayout())panel2.add(button5,BorderLayout.NORTH)panel2.add(text,BorderLayout.SOUTH)this.add(panel1,BorderLayout.CENTER)this.add(panel2,BorderLayout.SOUTH)setBounds(100,100,350,250)setVisible(true)validate()}publicvoidactionPerformed(ActionEvente){if(e.getSource()==button1){text.append("一个面纸、")sum=sum+3}elseif(e.getSource()==button2){text.append("一只钢笔、")sum=sum+5}elseif(e.getSource()==button3){text.append("一本书、")sum=sum+10}elseif(e.getSource()==button4){text.append("一双袜子、")sum=sum+8}elseif(e.getSource()==button5){text.append("\n"+"总价为:"+"拿烂\n"+sum)}}}publicclassShopping{publicstaticvoidmain(String[]args){newShopFrame("购物车")}}我没用Swing可能显闹敏粗示不出来你的效果。不满意得话我在给你编一个。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12380575.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存