我该怎么做才能缩短此代码以适应行限制

时间:2018-01-19 02:14:56

标签: java eclipse debugging

所以我有一个制作游戏的任务,我做了它,但它刚好超过了线路限制。我尝试尽可能多地删除空白行和其他内容而不破坏语法规则以及保持正确的形式(大括号)。游戏是计算机生成4个数字(1-10)并获得从4个数字(随机1或2)中添加或减去的最终数字,并且该人需要确定是否添加或减去每个数字。代码没有问题,只需要缩短一点。 (也许几行?)

这是我的代码:

//ROHAN DATTA (ICS2O8-C) - Magic Operations Game
import java.util.Scanner;
public class MagicNumberOperations {
    public static void main(String[] args) {
        int num1, num2, num3, num4, operation, finalAns = 0;
        String equation = "", userAns, playAgain;
        Scanner input = new Scanner (System.in);
        System.out.println("You are now playing: MAGIC NUMBER OPERATIONS");
        System.out.println("In this game, the computer generates 4 random numbers (1-10), \nand you must choose which operation (+ or -) to use with each number in order to get the final result (Determined by the computer!)");
        do {
            num1 = (int)(Math.random()*(10-1+1)+1);     //Randomly generating 4 numbers
            num2 = (int)(Math.random()*(10-1+1)+1);
            num3 = (int)(Math.random()*(10-1+1)+1);
            num4 = (int)(Math.random()*(10-1+1)+1);
            operation = (int)(Math.random()*(2-1+1)+1);
            if (operation == 1) {               //Determining whether or not to add or subtract
                equation = num1 + "+" + num2;   //Making word equation to compare with user answer later
                finalAns = num1 + num2;         //Creating integer answer to create the final number
            }
            else if (operation == 2) {
                equation = num1 + "-" + num2;
                finalAns = num1 - num2;
            }
            operation = (int)(Math.random()*(2-1+1)+1);
            if (operation == 1) {
                equation += "+" + num3;
                finalAns += num3;
            }
            else if (operation == 2) {
                equation += "-" + num3;
                finalAns -= num3;
            }
            operation = (int)(Math.random()*(2-1+1)+1);
            if (operation == 1) {
                equation += "+" + num4;
                finalAns += num4;
            }
            else if (operation == 2) {
                equation += "-" + num4;
                finalAns -= num4;
            }
            System.out.println("\n\nHere are your 4 numbers: " + num1 + ", " + num2 + ", " + num3 + ", " + num4);       //Statements
            System.out.println("And here is what all the numbers together should be: " + finalAns);
            System.out.print("\nEnter the whole equation (NO SPACES): ");
            userAns = input.nextLine();
            if (!userAns.equals(equation)) {
                System.out.println("Incorrect!");
                System.out.println("The correct answer was: " + equation);
                break;                          //If answer is wrong, loop breaks, GAME OVER!
            } 
            System.out.print("Correct! \n\nWould you like to play again (y / n) ?");
            playAgain = input.nextLine();
        } while (!playAgain.equals("n"));
        input.close();
        System.out.println("\n\nGAME OVER!");
    }
}

2 个答案:

答案 0 :(得分:0)

将部分代码切换为:

baseNum = (int)(Math.random() * 10 + 1);
equation = baseNum + "";
finalAns = baseNum;
for (int i = 0; i < 3; i++) {
    num = (int)(Math.random() * 10 + 1);
    if (Math.random() * 2 < 1) {
        equation += "+" + num;
        finalAns += num;
    }
    else {
        equation += "-" + num;
        finalAns -= num;
    }
}

答案 1 :(得分:0)

短27行:

//ROHAN DATTA (ICS2O8-C) - Magic Operations Game
import java.util.Scanner;
public class MagicNumberOperations {
    public static void main(String[] args) {
        System.out.println("You are now playing: MAGIC NUMBER OPERATIONS");
        System.out.println("In this game, the computer generates 4 random numbers (1-10), \nand you must choose which operation (+ or -) to use with each number in order to get the final result (Determined by the computer!)");
        try (Scanner input = new Scanner(System.in)) {
            do {
                int finalAns, num;
                String equation = "" + (finalAns = (int)(Math.random() * 10 + 1));
                System.out.print("\n\nHere are your 4 numbers: " + equation + ", "); // Statements
                for (int i = 0; i < 3; i++) {
                    System.out.print((num = (int)(Math.random() * 10 + 1)) + (i < 2 ? ", " : "\n"));
                    boolean r = Math.random() * 2 < 1;
                    equation += (r ? "+" : "-") + num;
                    finalAns += r ? num : -num;
                }
                System.out.println("And here is what all the numbers together should be: " + finalAns);
                System.out.print("\nEnter the whole equation (NO SPACES): ");
                if (!input.nextLine().equals(equation)) {
                    System.out.println("Incorrect!");
                    System.out.println("The correct answer was: " + equation);
                    break; // If answer is wrong, loop breaks, GAME OVER!
                }
                System.out.print("Correct! \n\nWould you like to play again (y / n) ?");
            } while (!input.nextLine().equals("n"));
        }
        System.out.println("\n\nGAME OVER!");
    }
}