计划的困难

时间:2015-02-23 00:04:42

标签: java arrays if-statement

我试图通过使用if语句让我的程序遇到不同的困难,但我遇到了一些错误。我的public static int[]也遇到了问题。我试图找到一种完全摆脱它的方法,并让每个数组都在自己的if语句中设置。

import java.util.*;

class CAI {
    public static void main (String[] arguments) {
        menu();//calls menu method
        compute();//calls compute method
    }
    public static void menu() {//method that displays menu
        System.out.println (" CAI MENU " );
        System.out.println ("\n1) DIFFICULTY 1\n2) DIFFICULTY 2\n3) DIFFICULTY 3\n4) DIFFICULTY 4\n5)DIFFICULTY 5");
    }
    public static int[] Blop(){
        Random rand = new Random();
        int arr[] = new int[4];
        arr[0] = rand.nextInt(9);
        arr[1] = rand.nextInt(9);
        arr[3] = rand.nextInt(99);
        arr[4] = rand.nextInt(99);
        return arr;
    }
    public static void compute(){
        int option;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter an option: ");
        option = input.nextInt();
        while (option != 0){
            if(option == 1){
                int num[] = new int[2];
                int ans;
                String choice;
                do{
                    num = Blop();
                    do{
                        System.out.print("How much is " + num[0] + " times " + num[1] + " ? :");
                        ans = input.nextInt();
                        String Correct;
                        String Wrong;
                        String[] correct = {"Very good! ","Excellent! ","Nice work! ","Keep up the good work! "};
                        String[] wrong = {"No. Please try again. ","Wrong. Try once more. ","Don’t give up! ","No. Keep trying "};
                        Random rand = new Random();
                        Correct = correct[rand.nextInt(correct.length)];
                        Wrong = wrong[rand.nextInt(wrong.length)];
                        if(ans == (num[0]*num[1])){
                            System.out.print(Correct);
                        }else{
                            System.out.print(Wrong);
                        }
                    }while(ans != (num[0]*num[1]));
                    System.out.print("Do you want more questions(yes/no) :");
                    input.nextLine();
                    choice = input.nextLine();
                }while(choice.equalsIgnoreCase("yes"));
            }
            if(option == 2){
                int num[] = new int[2];
                int ans;
                String choice;
                do{
                    num = Blop();
                    do{
                        System.out.print("How much is " + num[2] + " times " + num[3] + " ? :");
                        ans = input.nextInt();
                        String Correct;
                        String Wrong;
                        String[] correct = {"Very good! ","Excellent! ","Nice work! ","Keep up the good work! "};
                        String[] wrong = {"No. Please try again. ","Wrong. Try once more. ","Don’t give up! ","No. Keep trying "};
                        Random rand = new Random();
                        Correct = correct[rand.nextInt(correct.length)];
                        Wrong = wrong[rand.nextInt(wrong.length)];
                        if(ans == (num[2]*num[3])){
                            System.out.print(Correct);
                        }else{
                            System.out.print(Wrong);
                        }
                    }while(ans != (num[2]*num[3]));
                    System.out.print("Do you want more questions(yes/no) :");
                    input.nextLine();
                    choice = input.nextLine();
                }while(choice.equalsIgnoreCase("yes"));
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您实例化对象CAI,那么您可以取消静态方法

例如

public static void main (String[] arguments) {

    CAI myCai = new CAI();
    myCai.menu();//calls menu method
    myCai.compute();//calls compute method
}

private void menu() {//method that displays menu
    System.out.println (" CAI MENU " );
    System.out.println ("\n1) DIFFICULTY 1\n2) DIFFICULTY 2\n3) DIFFICULTY 3\n4) DIFFICULTY 4\n5)DIFFICULTY 5");
}

你也有错误

private int[] Blop(){
  Random rand = new Random();
  int arr[] = new int[4];
  arr[0] = rand.nextInt(9);
  arr[1] = rand.nextInt(9);
  arr[2] = rand.nextInt(99);  // change index
  arr[3] = rand.nextInt(99);  // change index

由于上面的代码现在是非静态的,它在类的每个实例中执行,它只对实例化的类本地化。每个实例都将单独执行此代码。