Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何缩短此代码以适应行限制_Java_Eclipse_Debugging - Fatal编程技术网

Java 如何缩短此代码以适应行限制

Java 如何缩短此代码以适应行限制,java,eclipse,debugging,Java,Eclipse,Debugging,所以我有一个任务要做一个游戏,我做了,但它刚刚超过了线的限制。我试着在不破坏语法规则的情况下尽可能多地删除空行和其他内容,并保持适当的形式(大括号)。游戏是,计算机生成4个数字(1-10),并得到一个最终数字,该数字可以从4个数字(随机1或2)中加上或减去,用户需要计算出每个数字是加还是减。代码没有问题,只需要稍微缩短一点。(也许几行?) 这是我的密码: //ROHAN DATTA (ICS2O8-C) - Magic Operations Game import java.util.Scann

所以我有一个任务要做一个游戏,我做了,但它刚刚超过了线的限制。我试着在不破坏语法规则的情况下尽可能多地删除空行和其他内容,并保持适当的形式(大括号)。游戏是,计算机生成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!");
    }
}

将部分代码切换到此:

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;
    }
}
baseNum=(int)(Math.random()*10+1);
方程=baseNum+“”;
finalAns=baseNum;
对于(int i=0;i<3;i++){
num=(int)(Math.random()*10+1);
if(Math.random()*2<1){
方程+=“+”+num;
finalAns+=num;
}
否则{
方程+=“-”+num;
finalAns-=num;
}
}
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!");
    }
}
//罗汉达塔(ICS2O8-C)-魔法操作游戏
导入java.util.Scanner;
公共类MagicNumber操作{
公共静态void main(字符串[]args){
System.out.println(“您现在正在玩:幻数运算”);
System.out.println(“在这个游戏中,计算机生成4个随机数(1-10),\n您必须选择对每个数字使用哪种操作(+或-),以获得最终结果(由计算机确定!);
try(扫描仪输入=新扫描仪(System.in)){
做{
int finalAns,num;
字符串方程=“”+(finalAns=(int)(Math.random()*10+1));
System.out.print(“\n\n这是您的4个数字:“+等式+”,”;//语句
对于(int i=0;i<3;i++){
System.out.print((num=(int)(Math.random()*10+1))+(i<2?,“:”\n”);
布尔r=Math.random()*2<1;
方程+=(r?“+”:“-”)+num;
finalAns+=r?num:-num;
}
System.out.println(“所有数字加起来应该是:+finalAns”);
System.out.print(“\n输入整个方程式(无空格):”;
如果(!input.nextLine().equals(等式)){
System.out.println(“不正确!”);
System.out.println(“正确答案是:“+等式”);
中断;//若答案是错误的,循环中断,游戏结束!
}
System.out.print(“正确!\n\n是否要再次播放(y/n)?”;
}而(!input.nextLine().equals(“n”));
}
System.out.println(“\n\n名称结束!”);
}
}

System.out.println(“x”)
后跟
System.out.println(“y”)
可以替换为
System.out.println(“x\ny”)
2-1+1==2
。那么为什么要写
2-1+1
?前3行可以进一步缩短为
equation=“”+(finalAns=(int)(Math.random()*10+1))
和ìf/else语句可以替换为以下3行:
boolean r=Math.random()*2<1
方程+=(r?+:“-”)+num
finalAns+=r?num:-num