英文:
Sorting an array in a decreasing order in Java
问题
package com.company;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static void sortString(String str) {
char[] chArr = str.toCharArray();
String SortString = "";
for (int i = 0; i < chArr.length; i++) {
for (int j = 0; j < chArr.length; j++) {
if (chArr[i] > chArr[j]) {
char temp = chArr[i];
chArr[i] = chArr[j];
chArr[j] = temp;
}
}
}
String[] SortedString = new String[5];
for (int k = 0; k < chArr.length; k++) {
SortString = SortString + chArr[k];
}
Arrays.sort(SortedString);
for (int counter = 0; counter < 5; counter++) {
System.out.println(SortedString[counter]);
}
}
public static void main(String[] args) {
Scanner UserInput = new Scanner(System.in);
String[] names = new String[5];
for (int counter = 0; counter < 5; counter++) {
do {
System.out.print("Input String #" + (counter + 1) + ": ");
names[counter] = UserInput.next().toLowerCase();
} while (names[counter].length() > 25);
}
UserInput.close();
Arrays.sort(names);
for (int counter = 0; counter < 5; counter++) {
sortString(names[counter]);
}
}
}
英文:
I'm trying to code a program that will sort the given strings one by one first and then sorting all the strings both in decreasing order I managed to sort the string character by character but I am having trouble in sorting all the sorted (character by character) strings. I tried using the Array.sort() but it does not sort it decreasingly and it only sorts the first input not the already sorted array
package com.company;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static void sortString(String str)
{
char[] chArr = str.toCharArray();
String SortString = "";
for (int i = 0; i< chArr.length; i++)
{
for (int j = 0; j< chArr.length; j++)
{
if(chArr[i] > chArr[j])
{
char temp = chArr[i];
chArr[i] = chArr[j];
chArr[j] = temp;
}
}
}
String[] SortedString = new String[5];
for (int k = 0; k<chArr.length;k++)
{
SortString = SortString + chArr[k];
}
Arrays.sort(SortedString);
for (int counter = 0; counter<5; counter++)
{
System.out.println(SortedString0+网站访问量);
}
}
public static void main(String[] args)
{
Scanner UserInput = new Scanner (System.in);
String[] names = new String[5];
for (int counter = 0; counter<5; counter++)
{
do
{
System.out.print("Input String #" + (counter+1) + ": ") ;
names0+网站访问量 = UserInput.next().toLowerCase();
}while(names0+网站访问量.length() > 25);
}
UserInput.close();
Arrays.sort(names);
for (int counter = 0; counter<5; counter++)
{
sortString(names0+网站访问量);
}
}
}
答案1
得分: 1
static String sortString(String str)
{
char[] chArr = str.toCharArray();
for (int i = 0; i < chArr.length; i++)
{
for (int j = 0; j < chArr.length; j++)
{
if(chArr[i] > chArr[j])
{
char temp = chArr[i];
chArr[i] = chArr[j];
chArr[j] = temp;
}
}
}
return new String(chArr);
}
public static void main(String[] args)
{
Scanner UserInput = new Scanner (System.in);
String[] names = new String[5];
for (int counter = 0; counter < 5; counter++)
{
do
{
System.out.print("Input String #" + (counter+1) + ": ");
names[counter] = UserInput.next().toLowerCase();
} while(names[counter].length() > 25);
}
UserInput.close();
// Arrays.sort(names); No point sorting here
String[] strings = new String[5];
for (int counter = 0; counter < 5; counter++)
{
strings[counter] = sortString(names[counter]);
}
Arrays.sort(strings);
// increasing order:
for(String s : strings) {
System.out.println(s);
}
// decreasing order:
for(int i = 4; i >= 0; i--) {
System.out.println(strings[i]);
}
}
英文:
static String sortString(String str)
{
char[] chArr = str.toCharArray();
for (int i = 0; i< chArr.length; i++)
{
for (int j = 0; j< chArr.length; j++)
{
if(chArr[i] > chArr[j])
{
char temp = chArr[i];
chArr[i] = chArr[j];
chArr[j] = temp;
}
}
}
return new String(chArr);
}
public static void main(String[] args)
{
Scanner UserInput = new Scanner (System.in);
String[] names = new String[5];
for (int counter = 0; counter<5; counter++)
{
do
{
System.out.print("Input String #" + (counter+1) + ": ") ;
names0+网站访问量 = UserInput.next().toLowerCase();
}while(names0+网站访问量.length() > 25);
}
UserInput.close();
// Arrays.sort(names); No point sorting here
String[] strings = new String[5];
for (int counter = 0; counter<5; counter++)
{
strings0+网站访问量 = sortString(names0+网站访问量);
}
Arrays.sort(strings);
// increasing order:
for(String s : strings) {
System.out.println(s);
}
// decreasing order:
for(int i = 4; i >= 0; i--) {
System.out.println(strings[i]);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论