給你一個前幾天才幫人寫的
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序定制開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了桂平免費(fèi)建站歡迎大家使用!
“計算整錢兌零”。程序要求用戶輸入一個雙精度數(shù)代表總元數(shù),就會列出總值與其等價的1元幣、二角五分幣、5分幣和1分幣的數(shù)目。程序報告的數(shù)目是1元幣的最大數(shù)、其次是二角五分幣的最大數(shù),等等,依此類推。只顯示非零的單位。對單個單位顯示單數(shù)單詞,對多于一個單位的顯示復(fù)數(shù)單詞
import java.util.Scanner;
public class MoneyCalculate {
public static void main(String[] args) {
int max100 = 0;
int max25 = 0;
int max5 = 0;
int max1 = 0;
double money = getMoneyFromInput();
String str = String.valueOf(money).trim();
String[] ary = str.split("\\.");
max100 = Integer.parseInt(ary[0]);
if(ary.length == 2){
int fen = Integer.parseInt(ary[1]);
if(ary[1].trim().length() == 1){
fen = Integer.parseInt(ary[1]) * 10;
}
max25 = fen / 25;
if(fen % 25 != 0){
fen = fen % 25;
}else{
fen = 0;
}
max5 = fen / 5;
max1 = fen % 5;
}
StringBuilder sb = new StringBuilder(money + " = ");
if(max100 != 0){
sb.append(max100);
sb.append("*1 ");
}
if(max25 != 0){
sb.append(max25);
sb.append("*0.25 ");
}
if(max5 != 0){
sb.append(max5);
sb.append("*0.05 ");
}
if(max1 != 0){
sb.append(max1);
sb.append("*0.01 ");
}
System.out.println(sb.toString());
}
private static double getMoneyFromInput() {
Scanner scanner = new Scanner(System.in);
return scanner.nextDouble();
}
}
-----------
2.49
2.49 = 2*1 1*0.25 4*0.05 4*0.01
-----------
2.5
2.5 = 2*1 2*0.25
-----------
37.23
37.23 = 37*1 4*0.05 3*0.01
-----------------
123.569
123.569 = 123*1 22*0.25 3*0.05 4*0.01
;
StringBuilder 結(jié)合了字符數(shù)組和字符串的好些優(yōu)點,所以實現(xiàn)大整數(shù)類的時候如果利用 StringBuilder 可以省掉不少功夫,比如:
import java.util.*;
class SPBI {????// SimplePositiveBigInteger 的縮略
public static void main(String[] args) {
try {
System.out.println("輸入兩個 30 位數(shù)以內(nèi)的正整數(shù)和一個符號('+' 或 '*'):");
Scanner scn = new Scanner(System.in);
SPBI a = new SPBI(scn.nextLine().trim()),
b = new SPBI(scn.nextLine().trim());
String operator = scn.nextLine().trim();
if (a.toString().length() 30 || b.toString().length() 30)
throw new Exception("至少有一個整數(shù)超過 30 位數(shù)");
if ( ! operator.matches("\\+|\\*"))
throw new Exception("此程序不支持的符號:" + operator);
System.out.println(
"\n\n" +
a.toStringWithDigitGrouping() + operator + "\n" +
b.toStringWithDigitGrouping() + "\n" +
"------------------------------ \n");
if (operator.equals("+"))
System.out.println(a.add(b).toStringWithDigitGrouping());
else
System.out.println(a.multiply(b).toStringWithDigitGrouping());
} catch (Exception ex) {
System.out.println("錯誤:" + ex.getMessage() + "。請重試。");
}
}
// 此 SBPI 所代表的整數(shù)(注:個位數(shù)在左端)
private StringBuilder reversedDigits;
// 唯一的構(gòu)造器
public SPBI(String spbi) {
if ( ! spbi.matches("\\d+"))
throw new IllegalArgumentException(spbi + " 不符合正整數(shù)格式");
reversedDigits = new StringBuilder(spbi).reverse();
normalize();
}
// 去掉這個 SPBI 中多余的前導(dǎo)零(全在 reversedDigits 的右端)
private void normalize() {
reversedDigits = new StringBuilder(reversedDigits.toString().replaceAll("(?!^)0+$", ""));
}
public String toString() {
return "" + new StringBuilder(reversedDigits).reverse();
}
// 除了在返回的字符串中加入了千位分組符外,跟 toString() 沒差別
public String toStringWithDigitGrouping() {
return "" + new StringBuilder(reversedDigits.toString().replaceAll(".{3}(?!$)", "$0,")).reverse();
}
// 加法操作(等于 this += that 然后返回 this)
public SPBI add(SPBI that) {
int maxLength = Math.max(reversedDigits.length(), that.reversedDigits.length());
reversedDigits.setLength(maxLength);????// 可能造成 reversedDigits 的右端被填入 '\0'
int carry = 0;
for (int i = 0; i reversedDigits.length(); i++) {
int digitOfThis = reversedDigits.charAt(i) != '\0' ? reversedDigits.charAt(i) - '0' : 0,
digitOfThat = i that.reversedDigits.length() ? that.reversedDigits.charAt(i) - '0' : 0,
sum?????????= digitOfThis + digitOfThat + carry;
carry = sum 9 ? 1 : 0;
reversedDigits.setCharAt(i, (char) (sum % 10 + '0'));
}
reversedDigits.append(carry);
normalize();
return this;
}
// 乘法操作(等于 this *= that 然后返回 this)
public SPBI multiply(SPBI that) {
SPBI multiplesOfTenOfOriginalThis = new SPBI(toString());
reversedDigits = new StringBuilder("0");????// this 歸零
for (int iThat = 0; iThat that.reversedDigits.length(); iThat++) {
for (int addCount = 0; addCount that.reversedDigits.charAt(iThat) - '0'; addCount++)
add(multiplesOfTenOfOriginalThis);
multiplesOfTenOfOriginalThis.reversedDigits.insert(0, 0);????// 乘 10
}
return this;
}
}
隨便給你寫了一個
package com.wys.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Test {
public static ListInteger smallNumbers,largeNumbers;
public static int sum1 = 0,sum2 = 0;
public static void main(String[] args) {
smallNumbers = new ArrayListInteger();
largeNumbers = new ArrayListInteger();
run();
}
public static void run() {
int i = 0;
for (int j = 0; j 50; j++) {
Random rand = new Random();
i = rand.nextInt(1000);
if (i500) {
small(i);
}else{
large(i);
}
}
System.out.println("隨機(jī)輸出的50個數(shù)字中:");
System.out.println("大于500的數(shù)(包括500)共有"+largeNumbers.size()+"個");
System.out.print("他們是"+largeNumbers);
System.out.println();
System.out.println("他們的和是"+sum1);
System.out.println("小于500的數(shù)共有"+smallNumbers.size()+"個");
System.out.print("他們是"+smallNumbers);
System.out.println();
System.out.println("他們的和是"+sum2);
}
private static void large(int number) {
largeNumbers.add(number);
sum1 += number;
}
private static void small(int number) {
smallNumbers.add(number);
sum2 += number;
}
}
當(dāng)前文章:中等難度Java代碼 java 難
當(dāng)前網(wǎng)址:http://www.rwnh.cn/article38/doshdsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站制作、關(guān)鍵詞優(yōu)化、網(wǎng)站改版、網(wǎng)站設(shè)計公司、網(wǎng)站導(dǎo)航
聲明:本網(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)