国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

Java南陽OJ-ASCII碼排序

2019-11-14 12:42:19
字體:
來源:轉載
供稿:網(wǎng)友
import java.util.Scanner;public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); while(n-->0){ String st = sc.next(); char ch[]= new char[3]; ch[0] = st.charAt(0); ch[1] = st.charAt(1); ch[2] = st.charAt(2); ch = sort(ch); for(int i=0;i<3;i++){ if(i==2){ System.out.PRintln(ch[2]); }else{ System.out.print(ch[i]+" "); } } } } //插入排序 public static char[] sort(char[] ch){ int i = 1; for (;i<3;i++){ char key = ch[i]; if(ch[i]<ch[i-1]){ int j = i-1; while(j>=0&&key<ch[j]) j--; for(int k=i;k>j+1;k--) ch[k]=ch[k-1]; ch[j+1]=key; } } return ch; }}

1.

String st = sc.next();

若改為sc.nextLine()則會報Unknown source的錯誤

2.

System.out.print(ch[i]+" ");

不是‘ ’,否則當成字符空格處理,和ch[i]相加,最終只輸出一個字符

3.

public static char[] sort(char[] ch)

main方法是static的就只能調(diào)用static方法,所以sort()需要是static的

4.

while(j>=0&&key<ch[j]) j--;

必須要加上j>=0的條件,否則出現(xiàn)越界,而且必須是j>=0&&key<ch[j]不能是key<ch[i]&&j>=0表達式的執(zhí)行是有順序的


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 铜梁县| 温宿县| 双辽市| 始兴县| 太谷县| 鄯善县| 灵璧县| 汽车| 集安市| 弋阳县| 蓬安县| 高雄市| 视频| 东阿县| 涡阳县| 常州市| 阿合奇县| 津南区| 桂东县| 南昌市| 杂多县| 康定县| 九龙城区| 托克托县| 湟源县| 晋中市| 肥东县| 珠海市| 疏附县| 东源县| 潍坊市| 聊城市| 禹城市| 长寿区| 弋阳县| 承德市| 方正县| 广灵县| 临海市| 炎陵县| 北流市|