网站建设资讯

NEWS

网站建设资讯

中等难度Java代码 java 难

谁能给一个Java程序代码我,要50行到100行就可以啦。最好有几行解释

给你一个前几天才帮人写的

网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、微信小程序定制开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了桂平免费建站欢迎大家使用!

“计算整钱兑零”。程序要求用户输入一个双精度数代表总元数,就会列出总值与其等价的1元币、二角五分币、5分币和1分币的数目。程序报告的数目是1元币的最大数、其次是二角五分币的最大数,等等,依此类推。只显示非零的单位。对单个单位显示单数单词,对多于一个单位的显示复数单词

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

有些难度的java编程题

;

StringBuilder 结合了字符数组和字符串的好些优点,所以实现大整数类的时候如果利用 StringBuilder 可以省掉不少功夫,比如:

import java.util.*;

class SPBI {    // SimplePositiveBigInteger 的缩略

public static void main(String[] args) {

try {

System.out.println("输入两个 30 位数以内的正整数和一个符号('+' 或 '*'):");

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("至少有一个整数超过 30 位数");

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 所代表的整数(注:个位数在左端)

private StringBuilder reversedDigits;

// 唯一的构造器

public SPBI(String spbi) {

if ( ! spbi.matches("\\d+"))

throw new IllegalArgumentException(spbi + " 不符合正整数格式");

reversedDigits = new StringBuilder(spbi).reverse();

normalize();

}

// 去掉这个 SPBI 中多余的前导零(全在 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;

}

}

Java 的代码,50行,要三个函数,要有循环

随便给你写了一个

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("随机输出的50个数字中:");

System.out.println("大于500的数(包括500)共有"+largeNumbers.size()+"个");

System.out.print("他们是"+largeNumbers);

System.out.println();

System.out.println("他们的和是"+sum1);

System.out.println("小于500的数共有"+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;

}

}


当前文章:中等难度Java代码 java 难
本文URL:http://cdweb.net/article/doshdsp.html