Advertisement
thotfrnk

worksheet 2.java

Nov 10th, 2022 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. import java.util.Scanner; // Scanner import
  2. public class worksheet2 { // program begins
  3.     public static void main(String[] args) {
  4.  
  5.         //Question taken from CAPE Pure Mathematics June 2018 Paper 1, #16
  6.  
  7.         Scanner input = new Scanner (System.in); // Scanner declaration
  8.  
  9.         // variable declaration
  10.  
  11.         int answer;
  12.         final int ans1 = 4, ans2 = 15, ans3 = -2, ans4 = 0;
  13.  
  14.         System.out.println("A circle is defined as (x+2)^2 + y^2 = 4. The gradient of the normal to the circle at (0,0) is: ? Your options: 4, 15, 0, -2 or 999 to exit.");
  15.         answer = input.nextInt();
  16.  
  17.         while (answer != 999) { //while loop being used as a continuous loop, and it can be exited when 999 is entered.
  18.  
  19.             //if statements being used to compare the answer the user entered with the ones that have been initialised within program
  20.             if(answer == ans1) {
  21.                 System.out.println("4 is the incorrect answer, please try again.");
  22.             }
  23.             if(answer == ans2) {
  24.                 System.out.println("15 is the incorrect answer, please try again.");
  25.             }
  26.             if(answer == ans3) {
  27.                 System.out.println("-2 is the incorrect answer, please try again.");
  28.             }
  29.             if(answer == ans4) {
  30.                 System.out.println("0 is the correct answer. Congratulations!");
  31.             }
  32.             //updating variable within loop
  33.             System.out.println("A circle is defined as (x+2)^2 + y^2 = 4. The gradient of the normal to the circle at (0,0) is: ? Your options: 4, 15, 0, -2 or 999 to exit.");
  34.             answer = input.nextInt();
  35.         } //while loop ends
  36.  
  37.         System.out.println("Thank you for answering."); //goodbye statement, will appear when 999 is entered
  38.     }
  39. } //program ends
Tags: Java
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement