Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Done in Java please- Create a program that prompts the user to enter t.docx

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige

Hier ansehen

1 von 2 Anzeige

Done in Java please- Create a program that prompts the user to enter t.docx

Herunterladen, um offline zu lesen

Done in Java please. Create a program that prompts the user to enter two Strings. Then display the Strings in alphabetical order. If the two Strings are equal, display a message that they are equal rather than printing them in alphabetical order.
Solution
public class AlphabeticalOrder
{
public static void main(String[] args)
{
int n;
String temp;
java.util.Scanner scn= new java.util.Scanner(System.in); //creating object for number of strings you want to store
System.out.print(\"Enter number of names you want to enter:\");
n = scn.nextInt(); //assigning number to variable n
String names[] = new String[n];
java.util.Scanner s1 = new java.util.Scanner(System.in); //creating object for strings
System.out.println(\"Enter all the names:\");
for(int i = 0; i < n; i++) //for loop for comparing strings
{
names[i] = s1.nextLine();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print(\"Names in Sorted Order:\"); // printing strings in sorted order
for (int i = 0; i < n - 1; i++)
{
System.out.print(names[i] + \",\");
}
System.out.print(names[n - 1]);
}
}
.

Done in Java please. Create a program that prompts the user to enter two Strings. Then display the Strings in alphabetical order. If the two Strings are equal, display a message that they are equal rather than printing them in alphabetical order.
Solution
public class AlphabeticalOrder
{
public static void main(String[] args)
{
int n;
String temp;
java.util.Scanner scn= new java.util.Scanner(System.in); //creating object for number of strings you want to store
System.out.print(\"Enter number of names you want to enter:\");
n = scn.nextInt(); //assigning number to variable n
String names[] = new String[n];
java.util.Scanner s1 = new java.util.Scanner(System.in); //creating object for strings
System.out.println(\"Enter all the names:\");
for(int i = 0; i < n; i++) //for loop for comparing strings
{
names[i] = s1.nextLine();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print(\"Names in Sorted Order:\"); // printing strings in sorted order
for (int i = 0; i < n - 1; i++)
{
System.out.print(names[i] + \",\");
}
System.out.print(names[n - 1]);
}
}
.

Anzeige
Anzeige

Weitere Verwandte Inhalte

Ähnlich wie Done in Java please- Create a program that prompts the user to enter t.docx (20)

Weitere von edwardk6 (20)

Anzeige

Aktuellste (20)

Done in Java please- Create a program that prompts the user to enter t.docx

  1. 1. Done in Java please. Create a program that prompts the user to enter two Strings. Then display the Strings in alphabetical order. If the two Strings are equal, display a message that they are equal rather than printing them in alphabetical order. Solution public class AlphabeticalOrder { public static void main(String[] args) { int n; String temp; java.util.Scanner scn= new java.util.Scanner(System.in); //creating object for number of strings you want to store System.out.print("Enter number of names you want to enter:"); n = scn.nextInt(); //assigning number to variable n String names[] = new String[n]; java.util.Scanner s1 = new java.util.Scanner(System.in); //creating object for strings System.out.println("Enter all the names:"); for(int i = 0; i < n; i++) //for loop for comparing strings { names[i] = s1.nextLine(); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (names[i].compareTo(names[j])>0) { temp = names[i]; names[i] = names[j]; names[j] = temp; } } }
  2. 2. System.out.print("Names in Sorted Order:"); // printing strings in sorted order for (int i = 0; i < n - 1; i++) { System.out.print(names[i] + ","); } System.out.print(names[n - 1]); } }

×