public class DABiP {
int nbBillet100;
int nbBillet50;
int nbBillet20 ;
int nbBillet10 ;
int nbPiece2;
int nbPiece1;
//Constructeur privé
private DABiP(int b10, int b5, int b2, int b1, int p2, int p1){
nbBillet100 = b10;
nbBillet50 = b5;
nbBillet20 = b2;
nbBillet10 = b1;
nbPiece2 = p2;
nbPiece1 = p1;
}
//DABiP initialisé avec 100 unités de chaque, soit 18300euros
public DABiP(){
this(100, 100, 100, 100, 100, 100);
}
/************************************************************
* SEULES LES METHODES QUI SUIVENT DOIVENT ETRE MODIFIEES
*************************************************************/
//Calculer le montant restant dans le DABiP
public int calculerMontantRestantDansDABiP(){
int a;
a = nbBillet100 * 100 + nbBillet50 * 50 + nbBillet20 * 20 + nbBillet10 * 10 + nbPiece2 * 2 + nbPiece1 * 1;
return a;
}
//Retirer une somme d'argent du DABiP
public boolean retrait(int somme){
int x = 0;
if(somme > calculerMontantRestantDansDABiP()) {
return false;
}
else if(somme < 0) {
return false;
}
else{
if(somme/100 > 1){
x = somme/100;
if(nbBillet100 - x >= 0){
nbBillet100 = nbBillet100 - x;
somme = somme - 100 * x;
}
else {
x = nbBillet100;
nbBillet100 = 0;
somme = somme - 100 * x;
}
}
if(somme/50 > 1){
x = somme/50;
if(nbBillet50 - x >= 0){
nbBillet50 = nbBillet50 - x;
somme = somme - 50 * x;
}
else{
x = nbBillet50;
nbBillet50 = 0;
somme = somme - 50 * x;
}
}
if(somme/20 > 1){
x = somme/20;
if(nbBillet20 - x >= 0){
nbBillet20 = nbBillet20 - x;
somme = somme - 20 * x;
}
else{
x = nbBillet20;
nbBillet20 = 0;
somme = somme - 20 * x;
}
}
if(somme/10 > 1){
x = somme/10;
if(nbBillet10 - x >= 0){
nbBillet10 = nbBillet10 - x;
somme = somme - 10 * x;
}
else{
x = nbBillet10;
nbBillet10 = 0;
somme = somme - 10 * x;
}
}
if(somme/2 > 1){
x = somme/2;
if(nbPiece2 - x >= 0){
nbPiece2 = nbPiece2 - x;
somme = somme - 2 * x;
}
else{
x = nbPiece2;
nbPiece2 = 0;
somme = somme - 2 * x;
}
}
if(somme >= 1){
x = somme;
if(nbPiece1 - x >= 0){
nbPiece1 = nbPiece1 - x;
somme = somme - x;
}
else{
x = nbPiece1;
nbPiece1 = 0;
somme = somme - x;
}
}
return true;
}
}
}
我的测试:
public class Notation {
public static void main(String[] args) throws InterruptedException {
DABiP dab;
double note = 0;
double score = 0;
//Test 1
System.out.print("Test 01: ");
dab = new DABiP();
if(dab.calculerMontantRestantDansDABiP() == 18300){
score++;
System.out.println("ok");
}
else{
System.out.println("non");
}
//Test 2
System.out.print("Test 02: ");
dab.nbBillet100 = 95;
if(dab.calculerMontantRestantDansDABiP() == 17800){
score++;
System.out.println("ok");
}
else{
System.out.println("non");
}
//Test 3
System.out.print("Test 03: ");
dab.nbBillet50 = 97;
dab.nbPiece2 = 96;
dab.nbPiece1 = 97;
if(dab.calculerMontantRestantDansDABiP() == 17639){
score++;
System.out.println("ok");
}
else{
System.out.println("non");
}
//Test 4
System.out.print("Test 04: ");
int tab[] = {100, 50, 20, 10, 2, 1};
int p = 0;
for(int i = 0; i < 10; i++){
int rand = tab[(int)(Math.random()*100)%tab.length];
int nb = (int)(Math.random()*100)%6;
p += (rand==100?100*nb:0);
p += (rand==50?50*nb:0);
p += (rand==20?20*nb:0);
p += (rand==10?10*nb:0);
p += (rand==2?2*nb:0);
p += (rand==1?1*nb:0);
dab.nbBillet100-=(rand==100?nb:0);
dab.nbBillet50-=(rand==50?nb:0);
dab.nbBillet20-=(rand==20?nb:0);
dab.nbBillet10-=(rand==10?nb:0);
dab.nbPiece2-=(rand==2?nb:0);
dab.nbPiece1-=(rand==1?nb:0);
}
if(dab.calculerMontantRestantDansDABiP() == 17639-p){
score++;
System.out.println("ok");
}
else{
System.out.println("non");
}
//Test 5
System.out.print("Test 05: ");
dab.nbBillet100 = 0;
dab.nbBillet50 = 0;
dab.nbBillet20 = 0;
dab.nbBillet10 = 0;
dab.nbPiece2 = 0;
dab.nbPiece1 = 0;
if(dab.calculerMontantRestantDansDABiP() == 0){
score++;
System.out.println("ok");
}
else{
System.out.println("non");
}
//Test 6
System.out.print("Test 06: ");
dab = new DABiP();
boolean r = dab.retrait(0);
if(r && dab.nbBillet100 == 100 && dab.nbBillet50 == 100 && dab.nbBillet20 == 100 && dab.nbBillet10 == 100 && dab.nbPiece2 == 100 && dab.nbPiece1 == 100){
score++;
System.out.println("ok");
}
else{
System.out.println("non");
}
//Test 7
System.out.print("Test 07: ");
dab = new DABiP();
r = dab.retrait(40);
if(r && dab.nbBillet100 == 100 && dab.nbBillet50 == 100 && dab.nbBillet20 == 98 && dab.nbBillet10 == 100 && dab.nbPiece2 == 100 && dab.nbPiece1 == 100){
score++;
System.out.println("ok");
}
else{
System.out.println("non");
}
//Test 8
System.out.print("Test 08: ");
dab = new DABiP();
r = dab.retrait(367);
if(r && dab.nbBillet100 == 97 && dab.nbBillet50 == 99 && dab.nbBillet20 == 100 && dab.nbBillet10 == 99 && dab.nbPiece2 == 97 && dab.nbPiece1 == 99){
score++;
System.out.println("ok");
}
else{
System.out.println("non");
}
//Test 9
System.out.print("Test 09: ");
dab = new DABiP();
r = dab.retrait(18300);
if(r && dab.nbBillet100 == 0 && dab.nbBillet50 == 0 && dab.nbBillet20 == 0 && dab.nbBillet10 == 0 && dab.nbPiece2 == 0 && dab.nbPiece1 == 0){
score++;
System.out.println("ok");
}
else{
System.out.println("non");
}
//Test 10
System.out.print("Test 10: ");
dab = new DABiP();
r = dab.retrait(18301);
if(!r && dab.nbBillet100 == 100 && dab.nbBillet50 == 100 && dab.nbBillet20 == 100 && dab.nbBillet10 == 100 && dab.nbPiece2 == 100 && dab.nbPiece1 == 100){
score++;
System.out.println("ok");
}
else{
System.out.println("non");
}
//Test 11
System.out.print("Test 11: ");
dab = new DABiP();
r = dab.retrait(19634);
if(!r && dab.nbBillet100 == 100 && dab.nbBillet50 == 100 && dab.nbBillet20 == 100 && dab.nbBillet10 == 100 && dab.nbPiece2 == 100 && dab.nbPiece1 == 100){
score++;
System.out.println("ok");
}
else{
System.out.println("non");
}
//Test 12
System.out.print("Test 12: ");
dab = new DABiP();
dab.nbBillet100 = 2;
r = dab.retrait(300);
if(r && dab.nbBillet100 == 0 && dab.nbBillet50 == 98 && dab.nbBillet20 == 100 && dab.nbBillet10 == 100 && dab.nbPiece2 == 100 && dab.nbPiece1 == 100){
score+=2;
System.out.println("ok");
}
else{
System.out.println("non");
}
//Test 13
System.out.print("Test 13: ");
dab = new DABiP();
dab.nbBillet100 = 2;
dab.nbBillet50 = 1;
dab.nbBillet20 = 5;
dab.nbBillet10 = 6;
dab.nbPiece2 = 50;
r = dab.retrait(407);
if(r && dab.nbBillet100 == 0 && dab.nbBillet50 == 0 && dab.nbBillet20 == 0 && dab.nbBillet10 == 1 && dab.nbPiece2 == 47 && dab.nbPiece1 == 99){
score+=2;
System.out.println("ok");
}
else{
System.out.println("non");
}
//Test 14
System.out.print("Test 14: ");
dab = new DABiP();
dab.nbBillet100 = 0;
dab.nbBillet50 = 0;
dab.nbBillet20 = 0;
dab.nbBillet10 = 0;
dab.nbPiece2 = 0;
r = dab.retrait(57);
if(r && dab.nbBillet100 == 0 && dab.nbBillet50 == 0 && dab.nbBillet20 == 0 && dab.nbBillet10 == 0 && dab.nbPiece2 == 0 && dab.nbPiece1 == 43){
score+=2;
System.out.println("ok");
}
else{
System.out.println("non");
}
//Test 15
System.out.print("Test 15: ");
dab = new DABiP();
r = dab.retrait(-1634);
if(!r && dab.nbBillet100 == 100 && dab.nbBillet50 == 100 && dab.nbBillet20 == 100 && dab.nbBillet10 == 100 && dab.nbPiece2 == 100 && dab.nbPiece1 == 100){
score++;
System.out.println("ok");
}
else{
System.out.println("non");
}
//Test 16
System.out.print("Test 16: ");
dab = new DABiP();
dab.nbBillet100 = 5;
dab.nbBillet50 = 0;
dab.nbBillet20 = 0;
dab.nbBillet10 = 0;
dab.nbPiece2 = 0;
dab.nbPiece1 = 5;
r = dab.retrait(12);
if(!r && dab.nbBillet100 == 5 && dab.nbBillet50 == 0 && dab.nbBillet20 == 0 && dab.nbBillet10 == 0 && dab.nbPiece2 == 0 && dab.nbPiece1 == 5) {
score+=2;
System.out.println("ok");
}
else {
System.out.println("non");
}
/**
* NOTATION
*/
System.out.println("--------------------------");
System.out.println("Note: "+score+"/20");
}
}
抱歉我的英语不好我是法国人
答案 0 :(得分:1)
要修正测试8,正如Andreas指出的那样,您需要更改所有使用大于(&gt;)的比较:
if(somme/100 > 1){ ...
使用大于或等于(&gt; =)代替:
if(somme/100 >= 1){ ...
当我进行此更改并再次运行您的代码时,测试8通过了。这是因为当somme
是67,并且您到达代码块以分配50件时,somme/50
是 int 值,因此小数位被切断,它等于1. 1不大于1,所以你的if语句被跳过了。感谢Andreas指出这一点。
关于测试16 - 编写代码的方式,如果机器没有足够的特定面额的钱 - 例如,如果它需要提供12个,但它只有5个 - 机器将给出即使这还不够,它还有所有的钱,然后它完成所有代码并返回 true 。
我认为如果没有足够的钱,你可能想要插入最终检查,如果没有足够的钱,则返回 false 。我会改变这个:
if(somme >= 1){
x = somme;
if(nbPiece1 - x >= 0){
nbPiece1 = nbPiece1 - x;
somme = somme - x;
}
else{
x = nbPiece1;
nbPiece1 = 0;
somme = somme - x;
}
}
对此:
if(somme >= 1){
x = somme;
if(nbPiece1 - x >= 0){
nbPiece1 = nbPiece1 - x;
somme = somme - x;
}
else{
x = nbPiece1;
nbPiece1 = 0;
somme = somme - x;
// ADD A CHECK HERE
if(somme > 0){
return false;
}
}
}
此外,你的测试16认为机器中剩下的1的数量应该是5,但请记住你编写了代码以便能够提供所有的钱。剩下1的实际数字将为0,因为即使没有足够的数据,它也试图将它们排除。这看起来像你想要它做的,所以也许你只是写了错误的测试16。
我希望这个答案很有帮助。如果您有其他问题或疑问,请告诉我们,我们会为您解答。
Et si vous voulezmareéponseenfrançais,je peux essayer - maismonfrançaisn'estpastrèsbien! (如果你想用法语,我可以尝试,但我的法语不是很好。)