मेरा प्रश्न मेरे कोड के एटीएम के सदृश लाइन के संबंध में है। वर्तमान संतुलन 10000
, जो मैंने लिखा है वह उपयोगकर्ता को एक संदेश दिखाता है कि यदि वे दिए गए शेष को वापस लेते हैं कि यह Insufficient balance
है। हालाँकि इस तरह से बैलेंस बदलता रहता है जब Inquiring balance
बैलेंस अब होता है <अनुमत बैलेंस। मेरा सवाल यह है कि जब Insufficient balance
संदेश उपयोगकर्ता को दिखाया जाए तो शेष राशि के लिए डबल मान को रीसेट करने के बारे में मुझे कैसे जाना चाहिए?
यहाँ मेरा कोड है
import java.util.Scanner;
class app {
public static void main(String[] args)
{
long pin = 2927942074l;
double balance = 10000.0;
int attempts = 3;
System.out.println("Please enter your pin.");
while (attempts > 0) {
Scanner keyboardpin = new Scanner(System.in);
long input = keyboardpin.nextLong();
if (input == pin) {
System.out.println("Correct");
System.out.println("Welcome to your ATM");
while (true) { // Keep printing your options unless "Quit" is chosen
int a = 1;
int b = 2;
int c = 3;
int d = 0;
System.out.println(a + " - Inquire Balance");
System.out.println(b + " - Withdraw");
System.out.println(c + " - Deposit");
System.out.println(d + " - Quit");
System.out.println("Please select what you want to do.");
Scanner menuselect = new Scanner(System.in);
int menuinput = menuselect.nextInt();
if (menuinput == a) {
System.out.println(balance);
continue;
}
if (menuinput == b) {
System.out.println("Please enter a withdrawal amount.");
Scanner withdrawamount = new Scanner(System.in);
double withdrawbalace = withdrawamount.nextDouble();
balance = (balance - withdrawbalace);
if (withdrawbalace > balance)
System.out.println("Insufficient balance");
if (withdrawbalace <= balance)
System.out.println("Youre new balance is " + balance);
}
if (menuinput == c) {
}
if (menuinput == d) {
break;
}
if (attempts == 0) {
System.out.println("Maximum number of attempts exceeded");
}
}
} else {
System.out.println("Wrong");
attempts--;
System.out.println("You have " + attempts + " attempts remaining.");
}
}
}
}
2 जवाब
बस जांचें कि क्या राशि को घटाने से पहले उपयोगकर्ता के पास पर्याप्त संतुलन है।
if (menuinput == b) {
System.out.println("Please enter a withdrawal amount.");
Scanner withdrawamount = new Scanner(System.in);
double withdrawbalace = withdrawamount.nextDouble();
if (withdrawbalace > balance)
System.out.println("Insufficient balance");
if (withdrawbalace <= balance)
balance = (balance - withdrawbalace);
System.out.println("Youre new balance is " + balance);
}
आपकी मुख्य समस्या पिछले उत्तर में हल हो गई है, लेकिन आपके कोड तर्क में कुछ त्रुटियां हैं। मैंने इसे आपके लिए तय किया है।
import java.util.Scanner;
class app {
public static void main(String[] args) {
final long pin = 2927942074L;
double balance = 10000.0;
int attempts = 3;
System.out.println("Please enter your pin.");
Scanner keyboard = new Scanner(System.in);
while (attempts > 0) {
long input = keyboard.nextLong();
if (input == pin) {
System.out.println("Correct");
System.out.println("Welcome to your ATM");
// Keep printing your options unless "Quit" is chosen
while (true) {
int a = 1;
int b = 2;
int c = 3;
int d = 0;
System.out.println(a + " - Inquire Balance");
System.out.println(b + " - Withdraw");
System.out.println(c + " - Deposit");
System.out.println(d + " - Quit");
System.out.println("Please select what you want to do.");
int menuInput = keyboard.nextInt();
if (menuInput == a) {
System.out.println(balance);
} else if (menuInput == b) {
System.out.println("Please enter a withdrawal amount.");
double withdrawBalance = keyboard.nextDouble();
if (balance >= withdrawBalance) {
balance -= withdrawBalance;
System.out.println("Your new balance is " + balance);
} else System.out.println("Insufficient balance");
} else if (menuInput == c) {
// Deposit code here
} else if (menuInput == d) break;
}
} else {
attempts--;
if (attempts == 0) {
System.out.println("Maximum number of attempts exceeded");
break;
}
System.out.println("Wrong");
System.out.println("You have " + attempts + " attempts remaining.");
}
}
keyboard.close();
}
}
और यहाँ कुछ सुझाव दिए गए हैं ...
- सार्थक चर नाम चुनने का प्रयास करें।
- यदि चर का मान स्थिर है तो इसे
final
करें - यदि चर नाम में दो, या अधिक शब्द होते हैं, तो शब्द दूसरे शब्द के पहले अक्षर को कैपिटल करते हैं (जैसे।
menuInput
) - यदि आप विभिन्न स्थितियों के लिए एक ही स्थिति की जाँच करते हैं, तो आप
switch
याif
,else if
,else
का उपयोग कर सकते हैं। - उन वस्तुओं को बंद करना न भूलें जिन्हें आपने मेमोरी जारी करने के लिए उपयोग किया था
शुभ लाभ
संबंधित सवाल
नए सवाल
java
जावा एक उच्च स्तरीय प्रोग्रामिंग भाषा है। इस टैग का उपयोग तब करें जब आपको भाषा का उपयोग करने या समझने में समस्या हो। इस टैग का उपयोग शायद ही कभी किया जाता है और इसका उपयोग अक्सर [वसंत], [वसंत-बूट], [जकार्ता-ई], [Android], [javafx], [हडूप], [श्रेणी] और [मावेन] के साथ किया जाता है।