Home > front end >  Just learn for loop written yourself groping, simple ATM, deposit and withdrawal, check balances, th
Just learn for loop written yourself groping, simple ATM, deposit and withdrawal, check balances, th

Time:09-17

Var num=100
For (var I=1; I & lt;=Infinity; I++) {
Var text=prompt (" please input you want to operate: \ n 1. Save \ n (2) draw money \ n 3. \ n show balance 4. Exit ')
If (parseInt (text)==1) {
Var cunnum=prompt (" please enter the amount: ')
Num +=parseInt (cunnum)
Alert (' you has a balance of: '+ num);
continue;
} else if (parseInt (text)==2) {
Var qunum=prompt (" please enter the amount you want to access: ');
If (parseInt (qunum) & gt; Num) {
Alert (' sorry, insufficient balance);
continue;
}
Num=qunum;

Alert (' you has a balance of: '+ num)
continue;
} else if (parseInt (text)==3) {
Alert (' you has a balance of: '+ num)
continue;
} else if (parseInt (text)==4) {
break;
} else {
Alert (' I ')
}
}

CodePudding user response:

The code is not complete,,,,,,,,,

CodePudding user response:

Var text=prompt (" please input you want to operate: \ n 1. Save \ n (2) draw money \ n 3. \ n show balance 4. Exit ')
Why write cycle in?? Every time you want to hint at a time??
Don't have to use text directly with parseInt=="1"

CodePudding user response:

Functionality is ok, then I also imitate a
 
Let money=100//initial amount
SetTimeout ()=& gt; {//wait 1 seconds began to perform
For (;; ) {//no input conditions to enter an infinite loop
Let inputData=https://bbs.csdn.net/topics/getInput (" please input you want to operate: \ n 1. Save \ n (2) draw money \ n 3. \ n show balance 4. Exit ')
//by getting input method to extract data
The switch (inputData) {
Case 1:
Let setMoney=parseInt (prompt (' please enter the access amount:))
Money +=setMoney
AlertBalances ()
break;
Case 2:
Let getMoney=parseInt (prompt (" please enter the amount you want to access: '))
If (getMoney & gt; Money) {
Alert (' sorry, insufficient balance ')
Break
}
Money -=getMoney
AlertBalances ()
break;
Case 3:
AlertBalances ()
break;
Case 4:
Alert (' exit 'success)
Return
break;
Default:
Alert (' I ')
break;
}
}

}, 1000)
The function getInput (MSG) {
Let the input=prompt (MSG | | 'please enter the content')
If (input) {
Input=input. Replace (\ D/g, ' ')
//regular excluding non-numeric parameters
} else {//if you click cancel, enter a null value, or 0
Input=0
}
The return Number (input)//make sure return parameter for Number type
}
The function alertBalances () {//tip the balance function
Alert (` your balance is ${money} `)
}

1. An infinite loop can be used for (;; {} {}) or the while (1) to implement
2. Specification of follow-up with clear variables use
3. The more the if () else if () can be used to switcch () {case} instead of better performance, the written clearer
4. Repeat the function or method for out package convenient unified maintenance
5. Reduce duplication of code

CodePudding user response:

If is an infinite loop, it is better to use a while loop,
Suitable For limited cycle For loop, such as walking down group,

Judgment, with switch beyond constant-like than if the else better readability,

Is the continue to skip the following statement, to begin a new cycle, here also was not used, because behind the if no other statements, so you can delete,

CodePudding user response:

Thank you for your bosses to give directions! A personal learning feeling a bit behind closed doors,
  • Related