我有这个项目要做,我必须设置一个菜单驱动的程序(eclipse中的java)。它不一定是GUI,它可以通过一系列system.out.println来完成,但这是我遇到问题的地方。除了设置部分,我完成了大部分代码。每个部分都必须有一个退出选项,程序本身有3个部分。第一个是从用户接收1到100的3个数字,第二个是按升序排列,第三个是查看数字是否可以形成三角形的边。我已完成第2步和第3步的代码,但无法使开始菜单部分工作。它需要是这样的,当用户输入1时,它会选择1,2,2和3,3,但选项2和3不能工作,除非选项1已经完成。这就是我到目前为止,我怎样才能让其余的工作?
import java.util.*;
import java.io.*;
public class Projcet {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
int A;
int B;
int C;
int num1;
int num2;
int num3;
int opt2;
int opt3;
int Exit;
System.out.println("This program is used to determine if three integers between 1-100 can form the sides of a triangle");
System.out.println("Enter your first number between 1 and 100");
num1= console.nextInt();
System.out.println("Enter your second number between 1 and 100");
num2 = console.nextInt();
System.out.println("Enter your third number between 1 and 100");
num3 = console.nextInt();
System.out.println("Select your next step");
System.out.println("Do not select step 3 if step 2 is not completed");
System.out.println("Exit");
System.out.println("opt2-Order your number in ascending order");
System.out.println("opt3-Determine if the three inputs form a triangle");
opt3 = console.nextInt();
opt2 = console.nextInt();
Exit =
opt2 =
public static void int(opt2)
{
import javax.swing.JOptionPane;
import java.util.*;
public class projecttest
{ public static void main(String[] args)
{
int num, i, j, temp;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of integers to sort:");
num = input.nextInt();
int array[] = new int[num];
System.out.println("Enter " + num + " integers: ");
for (i = 0; i < num; i++)
array[i] = input.nextInt();
for (i = 0; i < ( num - 1 ); i++) {
for (j = 0; j < num - i - 1; j++) {
if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println("Sorted list of integers:");
for (i = 0; i < num; i++)
System.out.println(array[i]);
public staic void int(opt3)
{
import java.util.*;
public class triangle {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
int a;
int b;
int c;
System.out.println("Enter a number for a");
System.out.println("Enter a number for b");
System.out.println("Enter a number for c");
a = console.nextInt();
b = console.nextInt();
c = console.nextInt();
if (a+b>c && a+c>b && b+c>a)
{
System.out.print("TRIANGLE");
}
else
{
System.out.print("NO TRIANGLE");
}
}
}
答案 0 :(得分:0)
你的第2部分&amp;在使用第1部分时,3没有意义。但由于你只要求第1部分,我会假设你在解决第1部分之后会进行集成。
boolean opt1Done = false;
System.out.println("Select your next step");
System.out.println("1: Exit");
System.out.println("2: Order your number in ascending order");
System.out.println("3: Determine if the three inputs form a triangle");
int answer = console.nextInt();
if (answer == 1) {
//do whatever for option 1
opt1Done = true;
} else if (answer == 2) {
if (opt1Done) {
//...... do whatever to order the numbers
} else {
System.out.println("you must complete Step 1 before Step 2");
}
} else if (answer == 3) {
if (opt1Done) {
//... do whatever to determine if triangle or not
} else {
System.out.println("you must complete Step 1 before Step 3");
}
}
希望这有帮助。