Java程序設(shè)計(jì)之12個(gè)經(jīng)典樣例
例子1:字符型變量
public class CharacterTest {
public static void main (String args[ ]) {
char chinaWord='你',japanWord='ぁ';
int p1=36328,p2=38358;
System.out.println("漢字'你'在unicode表中的順序位置:"+(int)chinaWord);
System.out.println("日語'ぁ'在unicode表中的順序位置:"+(int)japanWord);
System.out.println("unicode表中第20328位置上的字符是:"+(char)p1);
System.out.println("unicode表中第12358位置上的字符是:"+(char)p2);
}
}
例子2:數(shù)據(jù)類型轉(zhuǎn)換
public classDataTypeTest {
public static void main (String args[ ]) {
int c=2200;
long d=8000;
float f;
double g=123456789.123456789;
c=(int)d;
f=(float)g; //導(dǎo)致精度的損失.
System.out.print("c= "+c);
System.out.println(" d= "+d);
System.out.println("f= "+f);
System.out.println("g= "+g);
}
}
例子3:使用異或?qū)ψ址M(jìn)行加密和解密
class XORTest {
public static void main(String args[]){
char a1='十',a2='點(diǎn)',a3='進(jìn)',a4='攻';
char secret='8';
a1=(char)(a1^secret);
a2=(char)(a2^secret);
a3=(char)(a3^secret);
a4=(char)(a4^secret);
System.out.println("密文:"+a1+a2+a3+a4);
a1=(char)(a1^secret);
a2=(char)(a2^secret);
a3=(char)(a3^secret);
a4=(char)(a4^secret);
System.out.println("原文:"+a1+a2+a3+a4);
}
}
例子4:短路邏輯或(||)和位運(yùn)算(|)的區(qū)別
class OrTest {
public static void main(String args[]) {
int x,y=10;
if(((x=0)==0)||((y=20)==20)) {
System.out.println("現(xiàn)在y的值是:"+y);
}
int a,b=10;
if(((a=0)==0)|((b=20)==20)) {
System.out.println("現(xiàn)在b的值是:"+b);
}
}
}
例子5:用if語句實(shí)現(xiàn)a、b、c的值按從小到大排序
public class SortABC{
public static void main(String args[]){
int a=9,b=5,c=7,t;
if(a>b) {
t=a; a=b; b=t;
}
if(a>c) {
t=a; a=c; c=t;
}
if(b>c) {
t=b; b=c; c=t;
}
System.out.println("a="+a+",b="+b+",c="+c);
}
}
例子6:用if語句判斷給定的成績是否及格
public class Score {
public static void main(String args[]){
int math=65 ,english=85;
if(math>=60) {
System.out.println("數(shù)學(xué)及格了");
}
else {
System.out.println("數(shù)學(xué)不及格");
}
if(english>90) {
System.out.println("英語是優(yōu)");
}
else {
System.out.println("英語不是優(yōu)");
}
System.out.println("我在學(xué)習(xí)控制語句");
}
}
例子7:switch語句的使用
當(dāng)主程序執(zhí)行時(shí),如果第一個(gè)命令行參數(shù)的首字符分別是數(shù)字、小寫字母及大寫字母時(shí),系統(tǒng)會顯示這個(gè)首字符。如果輸入的是非數(shù)字或字母,則顯示不是數(shù)字或字母。
class Ex2_07 {
public static void main(String[] args) {
char ch = args[0].charAt(0);
switch (ch) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
System.out.println("The character is digit " + ch);
break;
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
case 'v': case 'w': case 'x': case 'y': case 'z':
System.out.println("The character is lowercase letter " + ch);
break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
case 'V': case 'W': case 'X': case 'Y': case 'Z':
System.out.println("The character is uppercase letter " + ch);
break;
default:
System.out.println("The character " + ch
+ " is neither a digit nor a letter.");
}
}
}
例子8:使用for循環(huán),計(jì)算 5+ 55 + 555 + 。。。 的前10項(xiàng)的和
public class Example3_6{
public static void main(String args[]){
long sum=0,a=5,item=a,n=10,i=1;
for(i=1;i<=n;i++) {
sum=sum+item;
item=item*10+a;
}
System.out.println(sum);
}
}
例子9:使用while循環(huán),計(jì)算 1 + 1/2! + 1/3! + 1/4! + + 1/20! 的值
class Example3_7 {
public static void main(String args[]) {
double sum=0,a=1;
int i=1;
while(i<=20) {
sum=sum+a;
i=i+1;
a=a*(1.0/i);
}
System.out.println("sum="+sum);
}
}
例子10:計(jì)算給定整數(shù)的各數(shù)字的和
class Ex2_10 {
public static void main(String args[]) {
int x = 12345;
int y=x;
int r=0;
int sum = 0;
while(y!=0) {
r = y % 10;
sum += r;
y = y / 10;
}
System.out.println("x -> " + sum);
}
}
例子11:break和continue使用舉例,分別計(jì)算10以內(nèi)的奇數(shù)的和,計(jì)算50以內(nèi)的素?cái)?shù)
class Example3_8 {
public static void main(String args[]){
int sum=0,i,j;
for( i=1;i<=10;i++){
if(i%2==0) //計(jì)算1+3+5+7+9
continue;
sum=sum+i;
}
System.out.println("sum="+sum);
for(j=2;j<=50;j++) { //求50以內(nèi)的素?cái)?shù)
for( i=2;i<=j/2;i++) {
if(j%i==0)
break;
}
if(i>j/2) {
System.out.println(""+j+"是素?cái)?shù)");
}
}
}
}
例子11:一維數(shù)組舉例,輸出一維整型數(shù)組中的值最小的那個(gè)元素及其下標(biāo))
public class ArrayTest {
public static void main(String args[]) {
int a[] = { 52, 78, 90, 34, 16, 34, 67 };
int indexOfMinElement = 0;
for (int i = 1; i < a.length; i++) {
if (a[indexOfMinElement] > a[i]) {
indexOfMinElement = i;
}
}
System.out.println("a[" + indexOfMinElement + "] = "
+ a[indexOfMinElement]);
}
}
例子12:計(jì)算二維數(shù)組中各行元素之和并查找其值最大的那個(gè)行
public class TableTester {
public static void main(String args[]) {
int myTable[][] = {
{23, 45, 65, 34, 21, 67, 78},
{46, 14, 18, 46, 98, 63, 88},
{98, 81, 64, 90, 21, 14, 23},
{54, 43, 55, 76, 22, 43, 33}};
int sum, max, maxRow=0;
max = 0; //Assume all numbers are positive
for (int row=0; row<4; row++) {
sum = 0;
for (int col=0; col<7; col++)
sum += myTable[row][col];
if (sum > max) {
max = sum;
maxRow = row;
}
}
System.out.println("Row " + maxRow + " has the highest sum of " + max);
}
}
到此這篇關(guān)于Java程序設(shè)計(jì)之11個(gè)經(jīng)典樣例的文章就介紹到這了,更多相關(guān)Java程序設(shè)計(jì)之經(jīng)典樣例內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java 程序設(shè)計(jì)總復(fù)習(xí)題(java基礎(chǔ)代碼)
- Java Swing程序設(shè)計(jì)實(shí)戰(zhàn)
- Java面向?qū)ο蟪绦蛟O(shè)計(jì):類的定義,靜態(tài)變量,成員變量,構(gòu)造函數(shù),封裝與私有,this概念與用法詳解
- Java面向?qū)ο蟪绦蛟O(shè)計(jì):繼承,多態(tài)用法實(shí)例分析
- Java面向?qū)ο蟪绦蛟O(shè)計(jì):抽象類,接口用法實(shí)例分析
- java程序設(shè)計(jì)語言的優(yōu)勢及特點(diǎn)
- java門禁系統(tǒng)面向?qū)ο蟪绦蛟O(shè)計(jì)
- Java網(wǎng)絡(luò)編程之TCP程序設(shè)計(jì)
- Java面向?qū)ο蟪绦蛟O(shè)計(jì)多態(tài)性示例
- Android中通過RxJava進(jìn)行響應(yīng)式程序設(shè)計(jì)的入門指南
相關(guān)文章
java實(shí)戰(zhàn)之飛機(jī)大戰(zhàn)小游戲(源碼加注釋)
這篇文章主要介紹了java實(shí)戰(zhàn)之飛機(jī)大戰(zhàn)小游戲(源碼加注釋),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
Socket結(jié)合線程池使用實(shí)現(xiàn)客戶端和服務(wù)端通信demo
這篇文章主要為大家介紹了Socket結(jié)合線程池的使用來實(shí)現(xiàn)客戶端和服務(wù)端通信實(shí)戰(zhàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
Mybatis的插件運(yùn)行原理及如何編寫一個(gè)插件
這篇文章主要介紹了Mybatis的插件運(yùn)行原理及如何編寫一個(gè)插件 ,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
MyBatis參數(shù)處理實(shí)現(xiàn)方法匯總
這篇文章主要介紹了MyBatis參數(shù)處理實(shí)現(xiàn)方法匯總,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
java基礎(chǔ)之接口組成更新的實(shí)現(xiàn)
本文主要介紹了java基礎(chǔ)之接口組成更新的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04

