这三个题,难倒我了,我初学java不是很熟练。这三个题,难倒我了,我初学java不是很熟练。
将字符串转为char数组,排序后,转为字符串。
第三题核心代码:
private String reverseText(String text) {
return new StringBuffer(text).reverse().toString();
}
System.out.println(reverseText(输入的字符串));
如有帮助,请采纳
有问题==》V:uoshuan-j
第三题
public static void main(String[] args) {
System.out.print("反转前===>");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
StringBuffer stringBuffer = new StringBuffer(str);
StringBuffer reverse = stringBuffer.reverse();
System.out.println("反转后===>"+reverse);
}
第二题
public static void main(String[] args) {
int i = 3;
do {
System.out.println("请输入用户名:");
Scanner scanner = new Scanner(System.in);
String username = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
if ("admin".equals(username) && "123".equals(password)){
System.out.println("登录成功");
break;
}else {
System.out.println("您还有"+--i+"次机会");
continue;
}
}while (i>=1);
}
第一题
public static void main(String[] args) {
System.out.print("排序前:");
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
char[] chs = s.toCharArray();
bubbleSort(chs);
String result = String.valueOf(chs);
System.out.println("排序后:" + result);
}
public static void bubbleSort(char[] chs) {
for (int x = 0; x < chs.length - 1; x++) {
for (int y = 0; y < chs.length - 1 - x; y++) {
if (chs[y] > chs[y + 1]) {
char temp = chs[y];
chs[y] = chs[y + 1];
chs[y + 1] = temp;
}
}
}
}
题目一希望对你有所帮助
import java.util.Arrays;
public class characterSorting {
public static void main(String[] args) {
String str = "wangshiqiwangyinuo";
System.out.println("之前:"+str);
char[] chars = str.toCharArray();
Arrays.sort(chars);
//正序遍历输出
System.out.println("之后:");
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i]);
}
}
}
public static void main(String[] args) {
System.out.print("排序前:");
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
StringBuffer stringBuffer = new StringBuffer(s);
System.out.println("排序后:"+stringBuffer.reverse());
}
public static void main(String[] args) {
System.out.print("排序前:");
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
char[] chs = s.toCharArray();
bubbleSort(chs);
String result = String.valueOf(chs);
System.out.println("排序后:" + result);
}
public static void bubbleSort(char[] chs) {
for (int x = 0; x < chs.length - 1; x++) {
for (int y = 0; y < chs.length - 1 - x; y++) {
if (chs[y] > chs[y + 1]) {
char temp = chs[y];
chs[y] = chs[y + 1];
chs[y + 1] = temp;
}
}
}
public static void main(String[] args) {
int i = 3;
do {
System.out.println("请输入用户名:");
Scanner scanner = new Scanner(System.in);
String username = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
if ("admin".equals(username) && "123".equals(password)){
System.out.println("登录成功");
break;
}else {
i--;
System.out.println("您还有"+i+"次机会");
continue;
}
}while (i>=1);
}