Mein Code kann momentan nicht am Anfang des Programms gestartet werden. Geben Sie "Bitte eingeben ..." ein. Ich möchte, dass der Code jedes Mal, wenn die Auswahl 1 oder 2 ausgeführt wird, zur Anfangsanweisung zurückkehren kann. Die while-Anweisung erfüllt dies nicht, da ich die while-Anweisung nicht verwenden kann, bevor 'choice' deklariert wird. Ich verstehe, dass ich eine tun soll, während Schleife, aber jedes Mal versuche ich es zu implementieren - es gibt mir einen Fehler mit Klammern.Anwenden von do while-Schleifen
Das folgende ist ein Ausschnitt aus meinem Code:
System.out.println("Please type in 1 for a customer to view their portfolio, 2 to trade stocks or 3 to exit the application");
int choice = Integer.parseInt(input.nextLine());
{
while (!"3".equals(choice));
{
if (choice == 1) {
System.out.println(mycustomers[menuchoice].toString());
return;
}
if (choice == 2) {
String StockSelect = "Please select a stock";
for (int i = 0; i < mystocks.length; i++) {
// [i] is the element we are accessing
StockSelect += " " + i + " " + (mystocks[i].getSymbol());
}
System.out.println(StockSelect);
int stockchoice = Integer.parseInt(input.nextLine());
System.out.println("Select 1 to buy or 2 to sell?");
int choice2 = Integer.parseInt(input.nextLine());
if (choice2 == 1) {
System.out.println("How many stocks would you like to buy ");
int volumebought = Integer.parseInt(input.nextLine());
for (int i = 0; i < numofstocks; i++) {
mycustomers[menuchoice].setBalance(
mycustomers[menuchoice].getBalance() - (volumebought * mystocks[i].getprice()));
mycustomers[menuchoice].setPortfolio();
}
System.out.println(mycustomers[menuchoice].toString());
return;
}
if (choice2 == 2) {
System.out.println("How much stocks would you like to sell ");
int volumesold = Integer.parseInt(input.nextLine());
for (int i = 0; i < numofstocks; i++) {
mycustomers[menuchoice].setBalance(
mycustomers[menuchoice].getBalance() + (volumesold * mystocks[i].getprice()));
mycustomers[menuchoice].setPortfolio();
}
System.out.println(mycustomers[menuchoice].toString());
return;
}
}
if (choice == 3) // how to exit application
{
System.out.println("Thank you for using the application");
System.out.println("The current state of all customers are:");
for (int i = 0; i < mycustomers.length; i++) {
System.out.println(mycustomers[i].toString());
}
System.out.println("The current state of all stocks are:");
for (int i = 0; i < mystocks.length; i++) {
System.out.println(mystocks[i].toString());
}
System.exit(0);
}
}
}}}
Wie würde ich die do-while implementieren Schleife, so dass es geht zurück auf die ursprüngliche Mitteilung jedes Mal nach der Ausführung des Codes - nur, wenn wenn der Eingang ist nicht 3?
Das ist kein do while-Schleife – Natecat
Try während (! Choice = 3) –