java根据位置按降序排序名称
从用户处获取名称和位置值的程序 存储在字符串数组中 按降序显示基于位置的名称值java根据位置按降序排序名称,java,arrays,string,sorting,Java,Arrays,String,Sorting,从用户处获取名称和位置值的程序 存储在字符串数组中 按降序显示基于位置的名称值 public class Samp { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String name[]=new String[5]; System.out.println("enter the name"); for(int i=0;i<name.lengt
public class Samp
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String name[]=new String[5];
System.out.println("enter the name");
for(int i=0;i<name.length;i++)
{
name[i]=sc.nextLine();
}
String location[]=new String[5];
System.out.println("enter the location");
for(int i=0;i<location.length;i++)
{
location[i]=sc.nextLine();
}
//want to sort name with respect to location in descending order
System.out.println("Name and location:");
System.out.println(name[0]+" "+location[0]);
System.out.println(name[1]+" "+location[1]);
System.out.println(name[2]+" "+location[2]);
System.out.println(name[3]+" "+location[3]);
System.out.println(name[4]+" "+location[4]);
}
}
公共类Samp
{
公共静态void main(字符串[]args)
{
扫描仪sc=新的扫描仪(System.in);
字符串名称[]=新字符串[5];
System.out.println(“输入名称”);
对于(int i=0;iUserArrays.Sort();
public class Test{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String name[]=new String[5];
System.out.println("enter the name");
for(int i=0;i<name.length;i++)
{
name[i]=sc.nextLine();
}
String location[]=new String[5];
System.out.println("enter the location");
for(int i=0;i<location.length;i++)
{
location[i]=sc.nextLine();
}
Arrays.sort(location); // asc
Arrays.sort(location,Collections.reverseOrder()); //desc
System.out.println("Name and location:");
System.out.println(name[0]+" "+location[0]);
System.out.println(name[1]+" "+location[1]);
System.out.println(name[2]+" "+location[2]);
System.out.println(name[3]+" "+location[3]);
System.out.println(name[4]+" "+location[4]);
}
}
公共类测试{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
字符串名称[]=新字符串[5];
字符串位置[]=新字符串[5];
对于(int i=0;i您希望我们为您的家庭作业做什么?),然后从endin descending ordermodified进行迭代。检查它,希望它将有助于java程序从用户处获取“n”名称和位置(字符串),并根据降序中的位置对名称进行排序……这是我的问题,您能帮助我吗我是java新手
class Book {
String name;
String author;
public Book(String name, String author) {
this.name = name;
this.author = author;
}
public static int compareBooks(Book a , Book b)
{
return a.name.compareTo(b.name);
}
@Override
public String toString() {
return "name : " + name + "\t" + "author : " + author;
}
}
public static void main(String[] args) {
Book[] books = {
new Book("Book 3" , "Author 1"),
new Book("Book 2" , "Author 2"),
new Book("Book 1" , "Author 3"),
new Book("Book 4" , "Author 4")
};
Arrays.sort(books , Book::compareBooks);
Arrays.asList(books).forEach(System.out::println);
}
public class Test{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String name[]=new String[5];
String location[]=new String[5];
for(int i=0;i<name.length;i++)
{
System.out.println("enter the name");
name[i]=sc.nextLine();
System.out.println("enter the location");
location[i]=sc.nextLine();
}
Arrays.sort(location,Collections.reverseOrder()); //desc
System.out.println("Name and location:");
System.out.println(name[0]+" "+location[0]);
System.out.println(name[1]+" "+location[1]);
System.out.println(name[2]+" "+location[2]);
System.out.println(name[3]+" "+location[3]);
System.out.println(name[4]+" "+location[4]);
}
}