My personal project homework errors and solutions

1. Console input

Error - My problem with the first review was console input, the code is as follows:

Scanner s = new Scanner(System.in);
		//Create a Scanner through new Scanner(System.in),
		//The console will wait for input until the Enter key is pressed, and pass the input to the Scanner as the scanning object.
		System.out.println("Please input the number of four arithmetic expressions to be generated (0~1000): ");
		OperationalFormula = s.nextInt();//Number of formulas input from the console
		System.out.println("Please enter the number of operators (3~5) contained in each expression: ");
		operator = s.nextInt();//Number of operators input from the console

 Modification - and this cannot be used for command line output, so after the first evaluation, I added a try-catch, the code is as follows:

try{
			OperationalFormula=Integer.parseInt(args[0]);
			if(OperationalFormula>1002||OperationalFormula<0){
			    System.out.println("The value does not meet the requirements, please re-enter");
			}
		}catch(Exception e){
			System.out.println("The value does not meet the requirements, please re-enter 1");
		}

 After this modification, the command line can be output, but you must add utf-8, enter 100 after a line break, and then change the line. The reason is the Chinese prompt input by Scanner.

2. Compilation error, does not support utf-8 and gbk compilation

Error - My problem in the second review was a compilation error, which does not support utf-8 and gbk compilation. I found the big guy Yuxin and found that the evaluation program must input Java Main 100, and then the program running results are output to the result file.

Modification - delete the Chinese prompt input by Scanner, and change the Main file to the following code:

import java.io.IOException;


public class Main {
	public static void main(String args[]) {
		String[] questionList = new String[1001];//Open an array of 0~1001, that is, the length is 1001
		int OperationalFormula=0;//Defines the number of an operational formula
		try {
                 OperationalFormula = Integer.parseInt(args[0]);// Enter the number of operational formulas for the question
                  } catch (Exception e) {
                   System.out.println("Please enter a positive integer as parameter");
                  }
		if(OperationalFormula>0){
		Lib.makeQuestions(questionList, OperationalFormula);
		//Call the makeQuestions function in Lib
		try {
			Lib.Filefile(questionList, OperationalFormula);
			//Call the file in Lib to output the result to the path
			
		}
		catch (IOException yes) {
			System.out.println("File generation error");
        }//Output the OperationalFormula expressions and results in makeQuestions to the path file
}
		
	}
}

 After this modification, the command line output can successfully meet the requirements.

3. Occasionally oversized numbers

Bug - Occasionally super large multipliers at runtime.

Modification - I found the big guy Xiaozhen and ran it a few more times. After investigation, it was found that the large numbers appeared after the multiplication sign. Later, I found that the generation condition of the number after my multiplication sign is unnecessary, and there is no such situation when I comment it out directly. The code is as follows:

			     else if( QuestionOfOp[j] == '*') {
//						QuestionOfNum[j] = (int)(Math.random()*20);
//						QuestionOfNum[j+1] = (int)(Math.random()*100/QuestionOfNum[j]);
					}

 4. Occasional negative numbers

Error - Occasionally negative numbers appear at runtime.

Modification - I found the big guy Xiaozhen and ran it several times. After checking, it was found that the negative numbers all appeared after the minus sign. Later, I found that the generation condition of the number after my minus sign was wrong. The modified code is as follows:

if( QuestionOfOp[j] == '-') {
						String dis = "" + QuestionOfNum[0];
						for(int k=0; k<j; k++)
							dis = dis +  QuestionOfOp[k] + QuestionOfNum[k+1];
						int Num = calQuestion(dis);
						if(Num<0){
							flag = false;
							break;
						}
						if(Num<=100){
							QuestionOfNum[j+1] = (int)(Math.random()*Num);
						}else{
							QuestionOfNum[j+1] = (int)(Math.random()*100);
						}
					}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324609284&siteId=291194637