2015-04-02 70 views
0

我有这个项目,我必须设置一个菜单驱动的程序(在eclipse中的java)。它不一定是一个GUI,它可以用一系列system.out.println完成,但这是我遇到问题的地方。除了设置部分外,我还完成了大部分代码。每个部分必须有一个退出选项,程序本身有3个部分。第一种是从用户中取1到100的数字,其次是从小到大排列,第三是数字是否可以形成三角形的边。我已经完成了第2步和第3步的代码,但无法使打开的菜单部分正常工作。它需要是一些东西,当用户输入1时,它选择1,2,2和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

你的部分与第1部分工作时,2 & 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"); 
    } 
} 

希望这会有所帮助。