2 u013460382 u013460382 于 2014.01.13 03:34 提问

关于Junit测试代码的编写

实在是搞不定了,苦逼程序女就各位大神帮助
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());
}

public static double getMoneyFromInput() {
Scanner scanner1 = new Scanner(System.in);
double r=0;
Scanner scanner2 = new Scanner(System.in);
double p=0;
System.out.println("请输入商品价格(单位为元):");
r=scanner1.nextDouble();
System.out.println("请输入付款数(单位为元):");
p=scanner2.nextDouble();
double n=p-r;
if(r<=0||r>10||p<=0||p>10||n<0){
System.out.println("输入错误") ;
return 0.0;
}
return n;
}
}

Csdn user default icon
上传中...
上传图片
插入图片