Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class MathTest
- {
- public static void main (String[] args)
- {
- Scanner sc=new Scanner(System.in);
- String choice,a="";
- double x,y,myans,realans=0,diff=0,x1,y1;
- final double MAX_DIFF=1e-6;
- while(true)
- {
- System.out.println("-------------------------");
- System.out.println("Select mode:");
- System.out.println("\'m\' for a manually-set problem.");
- System.out.println("\'r\' for a random problem.");
- System.out.println("\'q\' to quit the problem.");
- System.out.println("-------------------------");
- System.out.print(">>");
- choice=sc.next();
- if(choice.equals("q"))
- break;
- else if(choice.equals("m"))
- {
- System.out.println("Enter x >> ");
- x=sc.nextDouble();
- System.out.println("Enter y >> ");
- y=sc.nextDouble();
- realans=x/y;
- System.out.println("What is the value of "+x+"/"+y+" ?");
- myans=sc.nextDouble();
- diff=Math.abs(myans-realans);
- if(diff<MAX_DIFF)
- System.out.println("MathTest says\"Your answer is correct!\"");
- else
- {
- if(myans>realans)
- a="high";
- else if(myans<realans)
- a="low";
- System.out.println("Your answer is "+diff+" too "+a);
- }
- System.out.println("Correct answer is "+realans);
- }
- else if(choice.equals("r"))
- {
- x1=Math.random()*100;
- y1=Math.random()*100;
- x=(double)(Math.round(x1*10))/10;
- y=(double)(Math.round(y1*10))/10;
- while(y==0)
- {
- y=(double)(Math.round(y1*10))/10;
- }
- realans=x/y;
- System.out.println("What is the value of "+x+"/"+y+" ?");
- myans=sc.nextDouble();
- diff=Math.abs(myans-realans);
- if(diff<MAX_DIFF)
- System.out.println("MathTest says\"Your answer is correct!\"");
- else
- {
- if(myans>realans)
- a="high";
- else if(myans<realans)
- a="low";
- System.out.println("Your answer is "+diff+" too "+a);
- }
- System.out.println("Correct answer is "+realans);
- }
- System.out.println("-- Press ENTER to take another test --");
- }
- }
- }
Add Comment
Please, Sign In to add comment