日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

杂记(1)java读取char类型2. 大小写字母的转换3. 字符串的拼接4. 一串数字排序

發(fā)布時(shí)間:2025/4/16 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 杂记(1)java读取char类型2. 大小写字母的转换3. 字符串的拼接4. 一串数字排序 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. java讀取char類型
應(yīng)為java中沒有nextchar();函數(shù),所以只能先讀取一行,即

Sting a=sc.nextline(); char need=a.charAt(0);//取字符串的第一個(gè)字母。

2. 大小寫字母的轉(zhuǎn)換
(1).調(diào)用API(String類):
轉(zhuǎn)大寫

public static String ToUpper(String c) {c=c.toUpperCase();return c;}

轉(zhuǎn)小寫:

public static String ToLower(String c) {c=c.toLowerCase();return c;}

(2).ascll

System.out.println("轉(zhuǎn)換成大寫字母為:"+(char)(need-32));

(A—Z:6590;a—z:97122)
3. 字符串的拼接
(1).利用StringBuffer類型的append()函數(shù),使用該函數(shù)的好處是,不會(huì)產(chǎn)生臨時(shí)字符串。且大多數(shù)情況下線程安全。而StringBuilder雖然是非線程安全,但性能更好。

public static void PlainToSecret(int secret_key) {System.out.print("請(qǐng)輸入明文:");Scanner sc = new Scanner(System.in);String in = sc.nextLine();StringBuffer Secret = new StringBuffer();char[] ch = new char[in.length()];for (int i = 0; i < ch.length; i++) {ch[i] = in.charAt(i);}for (int i = 0; i < ch.length; i++) {if (ch[i] >= 'a' && ch[i] <= 'z') {if ((ch[i] - 32 + secret_key) > 90) {Secret.append((char) (ch[i] - 32 + secret_key - 26));}else {Secret.append((char) (ch[i] - 32 + secret_key));}}if (ch[i] >= 'A' && ch[i] <= 'Z') {if ((ch[i] + 32 + secret_key) > 122) {Secret.append((char) (ch[i] + 32 + secret_key - 26));}else {Secret.append((char) (ch[i] + 32 + secret_key));}}}System.out.println("密文為:"+Secret);}

(2).使用concat()函數(shù)()

String a="a"; String b="b"; String c= a.concat(b);

(3).使用"+"直接拼接;

String a="a"; String b="b"; String c="c"; String d=a+b+c;

此時(shí),第4句經(jīng)JDK編譯后其字節(jié)碼(或)會(huì)自動(dòng)優(yōu)化為等效于下列代碼編譯后的字節(jié)碼。

String d=new StringBuilder().append(a).append(b).append(c).toString();

4. 一串?dāng)?shù)字排序

import java.util.Scanner; import java.util.Arrays; public class Main{public static void main(String[] args){Scanner sc =new Scanner(System.in);int[] num=new int[3];for (int i = 0; i < num.length; i++) {num[i]=sc.nextInt();}Arrays.sort(num);//排序后a[0]最小int max1=num[0]*num[1]*num[2];int max2=num[2]*(num[0]+num[1]);if(max1>max2) {System.out.println(max1);}else {System.out.println(max2);}} }

5. 字符串反轉(zhuǎn)
使用StringBuilder或者StringBuffer中的reverse()函數(shù)。

public class Char_reverse {public static void main(String[] args) {System.out.print("請(qǐng)輸入待反轉(zhuǎn)的字符串:");Scanner sc=new Scanner(System.in);StringBuilder str=new StringBuilder();str=str.append(sc.nextLine()).reverse();System.out.println("反轉(zhuǎn)后的字符串為:"+str);} }

總結(jié)

以上是生活随笔為你收集整理的杂记(1)java读取char类型2. 大小写字母的转换3. 字符串的拼接4. 一串数字排序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。