成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

羅馬數(shù)字java代碼實現(xiàn),數(shù)字轉(zhuǎn)羅馬數(shù)字 java

JAVA 羅馬數(shù)字轉(zhuǎn)換成公歷年

已經(jīng)經(jīng)過驗證,可以實現(xiàn),但輸入數(shù)字不能有空格

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序定制開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了梁平免費建站歡迎大家使用!

public class Roman {

private final int num; // 羅馬數(shù)字轉(zhuǎn)換后的阿拉伯數(shù)字(十進制)

public Roman(String roman) throws RuntimeException {

if (roman.length() == 0) // 輸入為空

{

throw new RuntimeException("不能輸入空字符");

}

roman = roman.toUpperCase(); // 所有羅馬數(shù)字都轉(zhuǎn)換為大寫

int i = 0; // 記錄羅馬數(shù)字每個字符的位置

int arabic = 0; // 轉(zhuǎn)換后的阿拉伯數(shù)字

while (i roman.length()) {

char letter = roman.charAt(i); // 羅馬數(shù)字當前位置的字符

int number = letterToNumber(letter); // 字符轉(zhuǎn)化為阿拉伯數(shù)字

if (number 0) {

throw new RuntimeException("羅馬數(shù)字中不包含" + letter);

}

i++; // 移動到字符串的下一個位置

if (i == roman.length()) // 羅馬數(shù)字已處理完畢

{

arabic += number;

} else {

char nextLetter = roman.charAt(i);

int nextNumber = letterToNumber(nextLetter);

if (nextNumber number) // 后邊的字符比前邊的大

{

int result = nextNumber - number;

if (result == 4 || result == 9 || result == 40 || result == 90 || result == 400 || result == 900) {

arabic += result;

i++;

if (i == roman.length()) // 羅馬數(shù)字已處理完畢

{

break;

} else {

char afterNextLetter = roman.charAt(i);

int afterNextNumber = letterToNumber(afterNextLetter);

if (afterNextNumber result) {

throw new RuntimeException("不合法的羅馬數(shù)字" + letter + nextLetter + afterNextLetter);

}

}

} else {

throw new RuntimeException("不合法的羅馬數(shù)字" + letter + nextLetter);

}

} else {

if ((number == 5 || number == 50 || number == 500) number == nextNumber) // V、L、D用于大數(shù)右邊(相加),使用超過1次。

{

throw new RuntimeException("不合法的羅馬數(shù)字" + letter + nextLetter);

}

if (number == nextNumber) {

i++; // 還要再看下一個字符

if (i == roman.length()) // 羅馬數(shù)字已處理完畢

{

arabic += number + nextNumber;

break;

}

char afterNextLetter = roman.charAt(i);

int afterNextNumber = letterToNumber(afterNextLetter);

if (afterNextNumber nextNumber) // I、X、C在在大數(shù)左邊(即相減時)使用超過2個

{

throw new RuntimeException("不合法的羅馬數(shù)字" + letter + nextLetter + afterNextLetter);

} else if (afterNextNumber == nextNumber) // 出現(xiàn)3個字符都相同的情況,如III

{

i++; // 還要再看下一個字符,可能會出現(xiàn)IIII這種情況(不允許的,應拋出異常)

if (i == roman.length()) // 羅馬數(shù)字已處理完畢

{

arabic += number + nextNumber + afterNextNumber;

break;

}

char afterNextNextLetter = roman.charAt(i);

int afterNextNextNumber = letterToNumber(afterNextNextLetter);

if (afterNextNextNumber == afterNextNumber) // 出現(xiàn)IIII這種情況

{

throw new RuntimeException("不合法的羅馬數(shù)字" + letter + nextLetter + afterNextLetter + afterNextNextLetter);

} else {

arabic += number;

i = i - 2; // 回退2個字符(因為考慮了4個字符)

}

} else {

arabic += number + nextNumber;

}

} else {

arabic += number;

}

}

}

}

if (arabic 3999) {

throw new RuntimeException("輸入的數(shù)字不能超過3999");

}

num = arabic;

}

/**

* 羅馬字符轉(zhuǎn)換為阿拉伯數(shù)字

*

* @param letter

* 羅馬字符

* @return 正常羅馬字符,返回阿拉伯數(shù)字;否則,返回-1

*/

private int letterToNumber(char letter) {

switch (letter) {

case 'I':

return 1;

case 'V':

return 5;

case 'X':

return 10;

case 'L':

return 50;

case 'C':

return 100;

case 'D':

return 500;

case 'M':

return 1000;

default:

return -1;

}

}

public int getNum() {

return num;

}

public static void main(String[] args) {

while (true) {

System.out.println();

System.out.print("請輸入羅馬數(shù)字(按Q鍵退出):");

Scanner cin = new Scanner(System.in);

String romanStr = cin.nextLine();

if (romanStr.equals("q") || romanStr.equals("Q")) // 退出循環(huán)

{

break;

}

Roman roman = null;

try {

roman = new Roman(romanStr);

System.out.println(romanStr+" is the year:" + roman.getNum());

} catch (RuntimeException e) {

System.out.println(e);

}

}

}

}

Java 編程實例 要編寫 羅馬數(shù)字的加減運算

Java中有各國的文字段,正則表達式這玩意專門匹配各國語言的~

比如匹配漢字:

Pattern p=Pattern.Compile("[\u4e00-\u9fa5]");而匹配羅馬數(shù)字的是:

^[1-9]\d*$   

//匹配正整數(shù)

^-[1-9]\d*$  

//匹配負整數(shù)

^-?[1-9]\d*$

//匹配整數(shù)

^[1-9]\d*|0$ 

//匹配非負整數(shù)(正整數(shù) + 0)

^-[1-9]\d*|0$

//匹配非正整數(shù)(負整數(shù) + 0)

^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ 

  //匹配正浮點數(shù)

^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ 

//匹配負浮點數(shù)

^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ 

//匹配浮點數(shù)

^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ 

  //匹配非負浮點數(shù)(正浮點數(shù) + 0)

^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ 

 //匹配非正浮點數(shù)(負浮點數(shù) + 0)

用JAVA寫個程序,用戶輸入一個3999以內(nèi)的阿拉伯數(shù)字,可以輸出對應的羅馬數(shù)字

public?class?Test?{

public?static?void?main(String[]?args)?{

Scanner?sc?=?new?Scanner(System.in);

int?s?=?Integer.parseInt(sc.next());

System.out.println(toRome(s));

sc.close();

}

//?阿拉伯數(shù)字轉(zhuǎn)羅馬數(shù)字:

//?把所有小數(shù)字在前的組合也作為基本數(shù)字,再做一個對應的數(shù)值表就可以解決問題了。

//?I、V、X、???L、???C、?????D、?????M

//?1.5、10、50、100、500、1000

private?static?String?toRome(int?aNumber){

if(aNumber??1?||?aNumber??3999){

return?"-1";

}

int[]?aArray?=?{1000,900,500,400,100,90,50,40,10,9,5,4,1};

String[]?rArray?=?{"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};

String?rNumber?=?"";

for(int?i=0;?iaArray.length;?i++){

while(aNumber?=?aArray[i]){

rNumber?+=?rArray[i];

aNumber?-=?aArray[i];

}

}

return?rNumber;

}

}

JAVA 整數(shù)變羅馬數(shù)字

一直對古羅馬數(shù)字感興趣,借這個題學習了一下,得出

羅馬數(shù)轉(zhuǎn)阿拉伯數(shù),算法偽代碼:

設(shè)輸入"MCMLIV"為t,設(shè)輸出數(shù)為sum=0,設(shè)最大字符數(shù)值為m=0

從右往左遍歷t中的字符,比較方便

當遍歷字符代表的值c大于等于m時,m=c且sum+=c(對應3III等的情況);否則sum減去該值c(對應4IV,9IX等情況)

比如依次VILMCM依次對應+5, -1, +50, +1000, -100, +1000, 得1954

遍歷完輸出阿拉伯數(shù)sum.

==============

阿拉伯數(shù)轉(zhuǎn)羅馬數(shù),算法:

視羅馬數(shù)為十進制,每數(shù)位有1,2,3,4,5,6,7,8,9共9種符號(無0)

設(shè)全進制單位為a(比如I1, X10, C100, M1000),設(shè)半進制單位為b(比如V5, L50, D500),

分別對應固定形式表示法:

1 a; 2 aa; 3 aaa; 4 ab; 5 b; 6 ba; 7 baa; 8 baaa; 9 a(a2)

對于數(shù)字2016逐數(shù)位分解

2千 得 MM

0百 得 無

1十 得X

6得 VI

合并得MMXVI即2016

原理通了代碼實現(xiàn)就非常簡單了

JAVA編程題:羅馬數(shù)字轉(zhuǎn)大寫漢字

public class test {

public static void main(String args[]) {

int ss = Integer.parseInt(args[0]);

if(ss==0)

System.out.println("零");

else if (ss==1)

System.out.println("壹");

else if (ss==2)

System.out.println("貳");

else if (ss==3)

System.out.println("叁");

else if (ss==4)

System.out.println("肆");

else if (ss==5)

System.out.println("伍");

else if (ss==6)

System.out.println("陸");

else if (ss==7)

System.out.println("柒");

else if (ss==8)

System.out.println("捌");

else if (ss==9)

System.out.println("玖");

else

System.out.println("");

}

}

文章名稱:羅馬數(shù)字java代碼實現(xiàn),數(shù)字轉(zhuǎn)羅馬數(shù)字 java
地址分享:http://jinyejixie.com/article10/dsedhdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、移動網(wǎng)站建設(shè)網(wǎng)站收錄、ChatGPT、商城網(wǎng)站、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)
阿拉善盟| 宁海县| 大理市| SHOW| 西畴县| 布尔津县| 肃南| 涿州市| 沁水县| 措勤县| 刚察县| 永寿县| 松滋市| 抚远县| 华蓥市| 乌拉特后旗| 乐安县| 屏边| 松滋市| 拉萨市| 高青县| 清涧县| 舞钢市| 双柏县| 西乡县| 尼勒克县| 通榆县| 北流市| 晴隆县| 环江| 哈尔滨市| 信阳市| 无棣县| 韶山市| 江山市| 绩溪县| 托克托县| 阳高县| 马龙县| 稻城县| 祁东县|