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("日語(yǔ)'ぁ'在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語(yǔ)句實(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語(yǔ)句判斷給定的成績(jī)是否及格
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ǔ)是優(yōu)");
}
else {
System.out.println("英語(yǔ)不是優(yōu)");
}
System.out.println("我在學(xué)習(xí)控制語(yǔ)句");
}
}
例子7:switch語(yǔ)句的使用
當(dāng)主程序執(zhí)行時(shí),如果第一個(gè)命令行參數(shù)的首字符分別是數(shù)字、小寫(xiě)字母及大寫(xiě)字母時(shí),系統(tǒng)會(huì)顯示這個(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)容請(qǐng)搜索腳本之家以前的文章或繼續(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ǔ)言的優(yōu)勢(shì)及特點(diǎn)
- java門(mén)禁系統(tǒng)面向?qū)ο蟪绦蛟O(shè)計(jì)
- Java網(wǎng)絡(luò)編程之TCP程序設(shè)計(jì)
- Java面向?qū)ο蟪绦蛟O(shè)計(jì)多態(tài)性示例
- Android中通過(guò)RxJava進(jìn)行響應(yīng)式程序設(shè)計(jì)的入門(mén)指南
相關(guān)文章
分享一個(gè)你不知道的Java異常實(shí)現(xiàn)的缺陷
Java中一個(gè)大家熟知的知識(shí)點(diǎn)就是異常捕獲,try...catch...finally組合,但是很多人不知道這里面有一個(gè)關(guān)于Java的缺陷,或者說(shuō)是異常實(shí)現(xiàn)的一點(diǎn)不足之處。本文就通過(guò)一個(gè)很簡(jiǎn)單的實(shí)驗(yàn)給大家演示下效果玩玩兒,希望大家能覺(jué)得有趣2022-12-12
SpringBoot 二維碼生成base64并上傳OSS的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot 二維碼生成base64并上傳OSS的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Maven學(xué)習(xí)教程之搭建多模塊企業(yè)級(jí)項(xiàng)目
本篇文章主要介紹了Maven學(xué)習(xí)教程之搭建多模塊企業(yè)級(jí)項(xiàng)目 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
利用Java和c語(yǔ)言寫(xiě)一個(gè)計(jì)算器
這篇文章我們就來(lái)分享如何利用Java和c語(yǔ)言來(lái)寫(xiě)一個(gè)計(jì)算器,文章附有代碼詳細(xì)說(shuō)明,感興趣得小伙伴可以參考下面文章得具體內(nèi)容2021-10-10
SpringBoot 2.0 整合sharding-jdbc中間件實(shí)現(xiàn)數(shù)據(jù)分庫(kù)分表
這篇文章主要介紹了SpringBoot 2.0 整合sharding-jdbc中間件,實(shí)現(xiàn)數(shù)據(jù)分庫(kù)分表,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-06-06
方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別
這篇文章主要介紹了方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Java實(shí)現(xiàn)文件復(fù)制及文件夾復(fù)制幾種常用的方式
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)文件復(fù)制及文件夾復(fù)制幾種常用的方式,java復(fù)制文件的方式其實(shí)有不少種,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09
SpringBoot接口防抖(防重復(fù)提交)的實(shí)現(xiàn)方法
SpringBoot接口防抖主要通過(guò)前端和后端兩種方式實(shí)現(xiàn),前端通過(guò)JavaScript控制用戶操作,后端通過(guò)攔截器、過(guò)濾器等機(jī)制控制請(qǐng)求頻率,文中介紹的非常詳細(xì),感興趣的可以了解一下2024-11-11

