我是Java的新手,我的大部分知识都是自学成才。你能帮我解决这个问题。
我们的老师希望我们制作一个关于Java的菜单。输出的地方就像这样..
菜单 1 - Java历史 2- Java关键字 3 - Java Arrays等。 。
你想读一个(是/否): //如果是的话
请输入菜单编号: //然后它会显示一个信息..
//我的问题是如何将新输入的值连接到第一个方法,这样我就不会再将其全部写入...
这是我心中的想法.. 但我卡住了..
import java.util.Scanner;
public class Program {
public static void main (String [] args) {
System.out.println("Please enter a number:");
int x = nextInt();
if (x == 1) {
for (int x = 5; x == 5;x++) // array of 1(first menu)
System.out.println ("Do you want to read another? (Yes/No):");
System.out.println ("Please enter a menu number:")
// return to if with it's new entered value....
}
else if (x == 2) {
for loop of array 2
System.out.println ("Do you want to read another? (Yes/No):");
System.out.println ("Please enter a menu number:")
// return to if with it's new entered value....
}
else if (x == 3) {
for loop of array 3
System.out.println ("Do you want to read another? (Yes/No):");
System.out.println ("Please enter a menu number:")
// return to if with it's new entered value....
else if (x ==4) {
for loop of array 4
else if (x == 5) {
for loop of array 5
else if (x == 6) {
for loop of array 6
else if (x == 7) {
for loop of array 7
else if (x == 8) {
for loop of array 8
else if (x == 9) {
for loop of array 9
else if (x == 10) {
for loop of array 10
else {
//exit the program
答案 0 :(得分:0)
如果我理解正确,您可能希望在程序中实现While或do while语句。 Example of While and Do While Loops
我不确定你要用你的程序完成什么,但这里有一个小例子我很快就如何在你的情况下使用for循环。 `import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
boolean keepGoing = true; //As long as this is true the while statement will continue;
while (keepGoing) {
System.out.println("Enter a number");
int x = scan.nextInt();
//logic
if (x == 1) {
System.out.println("X is equal to 1");
} else if (x == 2)
System.out.println("X is equal to 2");
//prompts the user if they want to keep going.
System.out.println("Would you like to keep going? Y/N");
String decision = scan.next();
if (decision.equalsIgnoreCase("y")) {
keepGoing = true;
} else {
System.out.println("Quitting...");
keepGoing = false;
}
}
}
}
`