`我正在為我的計算機科學課撰寫一個程式,但由于某種原因無法讓它在完成任務后回傳程式的原始選單。
我需要程式回到原來的選單,而不是在完成一個選項后停止。我試過在程式的開頭添加一個while回圈和一個for回圈,但它沒有修復它。應該是一個簡單的修復,但不知道它是什么。謝謝。
import javax.lang.model.type.ArrayType;
import java.sql.SQLOutput;
import java.util.ArrayList; //import the arrayList class
import java.util.Scanner;
public class Main {
static ArrayList<String> planets = new ArrayList<>();
public static void main(String[] args) {
//Default values, can leave blank or remove
planets.add(0, "Mars");
planets.add(1, "Jupiter");
planets.add(2, "Earth");
planets.add(3, "Venus");
planets.add(4, "Neptune");
planets.add(5, "Saturn");
boolean isactive;
if (isactive =true) {
System.out.println("Choose your favorite planets with this program. This program edits arrays, please choose an array position to edit [0,1,2,3]");
System.out.println("1. Print Array");
System.out.println("2. Edit Array");
System.out.println("3. Add to array");
System.out.println("4. Exit the program");
ArrayList<String> cars = new ArrayList<String>();
Scanner scn = new Scanner(System.in);
int MenuOption = scn.nextInt(); //Scan result
if (MenuOption == 1) {
System.out.println(planets);
}
if (MenuOption == 2) {
System.out.println("Please choose which element to edit");
System.out.println("0");
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("4");
System.out.println("5");
System.out.println("6");
System.out.println("7");
System.out.println("Press 9 to exit");
Scanner editMenu = new Scanner(System.in);
int editMenuOption = editMenu.nextInt();
if (editMenuOption == 0) {
System.out.println("what would you like to change position 0 to");
Scanner posZeroScan = new Scanner(System.in);
String posZeroScanEdit = posZeroScan.next();
planets.add(0, posZeroScanEdit);
System.out.println(planets);
}
if (editMenuOption == 1) {
System.out.println("what would you like to change position 1 to");
Scanner posOneScan = new Scanner(System.in);
String posOneScanEdit = posOneScan.next();
planets.add(1, posOneScanEdit);
System.out.println(planets);
}
if (editMenuOption == 2) {
System.out.println("what would you like to change position 2 to");
Scanner posTwoScan = new Scanner(System.in);
String posTwoScanEdit = posTwoScan.next();
planets.add(2, posTwoScanEdit);
System.out.println(planets);
}
if (editMenuOption == 3) {
System.out.println("what would you like to change position 3 to");
Scanner posThreeScan = new Scanner(System.in);
String posThreeScanEdit = posThreeScan.next();
planets.add(3, posThreeScanEdit);
System.out.println(planets);
}
if (editMenuOption == 4) {
System.out.println("what would you like to change position 4 to");
Scanner posFourScan = new Scanner(System.in);
String posFourScanEdit = posFourScan.next();
planets.add(4, posFourScanEdit);
System.out.println(planets);
}
if (editMenuOption == 5) {
System.out.println("what would you like to change position 4 to");
Scanner posFiveScan = new Scanner(System.in);
String posFiveScanEdit = posFiveScan.next();
planets.add(5, posFiveScanEdit);
System.out.println(planets);
}
if (editMenuOption == 6) {
System.out.println("what would you like to change position 4 to");
Scanner posSixScan = new Scanner(System.in);
String posSixScanEdit = posSixScan.next();
planets.add(6, posSixScanEdit);
System.out.println(planets);
}
if (editMenuOption == 7) {
System.out.println("what would you like to change position 4 to");
Scanner posSevenScan = new Scanner(System.in);
String posSevenScanEdit = posSevenScan.next();
planets.add(7, posSevenScanEdit);
System.out.println(planets);
} else {
System.exit(1);
}
}
if (MenuOption == 3) {
System.out.println("What would you like to add");
Scanner add = new Scanner(System.in);
String addRes = add.next();
planets.add(addRes);
System.out.println(planets);
}
if (MenuOption == 4) {
System.out.println("Thanks for checking out my program!");
System.exit(0);
}
}
}
}
`
uj5u.com熱心網友回復:
這條線if (isactive =true)你必須用回圈替換它。例如,您可以替換為,while (isactive =true)并且 isactive 必須初始化為true
uj5u.com熱心網友回復:
由于您似乎只使用主方法,我假設您想從頭開始重復主方法。在這種情況下,您可以像這樣簡單地呼叫它:
main(null);
uj5u.com熱心網友回復:
初始化isactive:boolean isactive = true;
將 if 更改為 while-loop:while(isactive)而不是if(isactive =true)
并修改選項 4
if (MenuOption == 4) {
System.out.println("Thanks for checking out my program!");
isactive=false;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/526604.html
