问题是: 用//注释的 我用异或交换两个元素不成功,请问为什么?
- package com.itcast.shuzu;
- import java.util.Scanner;
- public class Demo7_StringPaiXu {
- /*
- *
- 4.键盘录入一个字符串,格式:"12 78 34 56 67 45 23 89";(要求字符串是由数字和空格组成的)
- 将字符串中的数字排序格式:"12 23 34 45 56 67 78 89";(要求不能使用Arrays.sort()方法,结果为字符串)
- 把字符串中所有的"6"替换成"9";
- */
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入一个数字字符串序列");
- String s = sc.nextLine();
-
- String[] str = s.split(" ");
- int[] arr = new int[str.length];
- for (int i = 0; i < str.length; i++) {
- //Integer i1 = new Integer(str[i]);
- arr[i] = Integer.parseInt(str[i]);
-
- }
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i]+" ");
- }
- System.out.println();
- System.out.println("=============");
-
- //排序:冒泡排序
- for (int i = 0; i < arr.length-1; i++) {
- for (int j = 0; j < arr.length-1-i; j++) {
- if(arr[j] > arr[j+i]){
- int temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;
- // arr[j] = arr[j] ^ arr[j+1];
- // arr[j+1] = arr[j] ^ arr[j+1];
- // arr[j] = arr[j] ^ arr[j+1];
- }
- }
- }
- //打印数组
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < arr.length; i++) {
- if(i == arr.length-1){
- sb.append(arr[i]);
- }else
- sb.append(arr[i] + " ");
- }
- System.out.println(sb);
-
- String str1 = sb.toString();
- String str2 = str1.replace("6", "9");
- System.out.println(str2);
- }
- }
复制代码 |
|