Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class MathTest_UsingMethod
- {
- public static void main(String[] args)
- {
- String choice;
- while(true)
- {
- choice = showMainMenuAndGetChoice();
- if(isChoiceQuit(choice))
- break;
- processChoice(choice);
- }
- showByebyeMessage();
- }
- public static String showMainMenuAndGetChoice()
- {
- showMainMenu();
- String choicemade=getChoice();
- return choicemade;
- }
- public static void showMainMenu()
- {
- 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(">>");
- }
- public static String getChoice()
- {
- Scanner sc=new Scanner(System.in);
- String ch = sc.next();
- return ch;
- }
- public static boolean isChoiceQuit(String choice)
- {
- return choice.equals("q");
- }
- public static void processChoice(String choice)
- {
- if(choice.equals("m"))
- mathtestInputByYourself();
- else if(choice.equals("r"))
- mathtestInputByRandom();
- }
- public static void mathtestInputByYourself()
- {
- Scanner sc=new Scanner(System.in);
- double x,y,myans,realans=0;
- System.out.println("Enter x >> ");
- x=sc.nextDouble();
- System.out.println("Enter y >> ");
- y=sc.nextDouble();
- realans=realAns(x,y);
- myans=myAns(x,y);
- System.out.println(checkAns(myans,realans));
- }
- public static void mathtestInputByRandom()
- {
- double x,y,myans,realans=0,x1,y1;
- 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=realAns(x,y);
- myans=myAns(x,y);
- System.out.println(checkAns(myans,realans));
- }
- public static double myAns(double x,double y)
- {
- Scanner sc=new Scanner(System.in);
- double myanswer;
- System.out.println("What is the value of "+x+"/"+y+" ?");
- myanswer=sc.nextDouble();
- return myanswer;
- }
- public static double realAns(double x,double y)
- {
- return x/y;
- }
- public static String checkAns(double myans,double realans)
- {
- String a="";
- double diff=0;
- final double MAX_DIFF=1e-6;
- String result;
- diff=Math.abs(myans-realans);
- if(diff<MAX_DIFF)
- result = "MathTest says\"Your answer is correct!\"";
- else
- {
- if(myans>realans)
- a="high";
- else if(myans<realans)
- a="low";
- result = "Your answer is "+diff+" too "+a;
- }
- result = result+"\nCorrect answer is "+realans;
- return result;
- }
- public static void showByebyeMessage()
- {
- System.out.println("\n\n---------------Good Luck----------------");
- }
- }
Add Comment
Please, Sign In to add comment