Java 随机产生数字计算加减乘法

package java_1;

import java.util.Scanner;

public class Hello{
	
	public static void main(String[] args) {
		Scanner input = new Scanner (System.in);
		int number1 = (int)(Math.random()*10);  //Generate random integers
		int number2 = (int)(Math.random()*10);
		int flag = (int)(Math.random()*4);
		
		try {
			if(flag==0)  //add
			{
				System.out.print(number1 + " + " + number2 + " = ");
				int answer1 = input.nextInt();
				if(answer1!=(number1+number2))
					System.out.print(number1+" + "+number2+" = " +(number1+number2) 
							+" , Your answer is false");
				else System.out.print("Your answer is ture");
			}
			else if(flag==1) //subtract
			{
				System.out.print(number1 + " - " + number2 + " = ");
				int answer1 = input.nextInt();
				if(answer1!=(number1-number2))
					System.out.print(number1+" - "+number2+" = " +(number1-number2) 
							+" , Your answer is false");
				else System.out.print("Your answer is ture");
			}
			else if(flag==2)  //multiply
			{
				System.out.print(number1 + " * " + number2 + " = ");
				int answer1 = input.nextInt();
				if(answer1!=(number1*number2))
					System.out.print(number1+" * "+number2+" = " +(number1*number2) 
							+" , Your answer is false");
				else System.out.print("Your answer is ture");
			}
//			else if(flag==3) //division
//			{
//				System.out.print(number1 + " / " + number2 + " = ");
//				int answer1 = input.nextInt();
//				if(answer1!=(number1/(float)number2))
//					System.out.print(number1+" / "+number2+" = " +(number1/(float)number2) 
//							+" , Your answer is false");
//				else System.out.print("Your answer is ture");
//			}
			
		}
		
		finally {
			input.close();  //close the input
		}
	}
}

 

你可能感兴趣的:(java)