QuestionAsk Questions?

Write a program that could be used to help children practise their subtraction problems. The

problems involve only integers between 10 and 99, inclusive. The program should start by

asking the user how many problems they would like to try, and read this value from the

keyboard. Then the program presents to the user that many problems and allows the user to

enter an answer for each. After user has input each answer, the program then display a

message indicating that the answer is correct, or if it is not correct, display the correct answer.

When all the problems are done, the program should print the percentage of problems answered

correctly.

Below is a sample run of such a program. The user input is shown in boldface.

How many problems would you like? 3

Question 1: What is 10 - 8? 2

Correct!

Question 2: What is 12 - 3? 8

No, 12 - 3 is 9

Question 3: What is 5 - 0? 5

Correct!

Your score is 66.66%

NOTE: a) The subtraction should not result in negative answer. That means if the first

integer is less than the second, you should swap the two randomly generated

integers so that the answer will always be non-negative.

b) Write the solution program in a single file called 'Subtraction.java'. Write the whole program inside the main method

problems involve only integers between 10 and 99, inclusive. The program should start by

asking the user how many problems they would like to try, and read this value from the

keyboard. Then the program presents to the user that many problems and allows the user to

enter an answer for each. After user has input each answer, the program then display a

message indicating that the answer is correct, or if it is not correct, display the correct answer.

When all the problems are done, the program should print the percentage of problems answered

correctly.

Below is a sample run of such a program. The user input is shown in boldface.

How many problems would you like? 3

Question 1: What is 10 - 8? 2

Correct!

Question 2: What is 12 - 3? 8

No, 12 - 3 is 9

Question 3: What is 5 - 0? 5

Correct!

Your score is 66.66%

NOTE: a) The subtraction should not result in negative answer. That means if the first

integer is less than the second, you should swap the two randomly generated

integers so that the answer will always be non-negative.

b) Write the solution program in a single file called 'Subtraction.java'. Write the whole program inside the main method

View Answers

March 18, 2010 at 3:10 PM

Hi Friend,

Try the following code:

import java.util.*;

public class Subtraction {

public static void main(String[] args) throws Exception {

int i=0;

int min=10, max=99;

Scanner scan = new Scanner(System.in);

Random random1 = new Random();

Random random2 = new Random();

System.out.print("How many problems would you like? ");

double q = scan.nextDouble();

System.out.println();

double r=0,w=0;

for (int idx = 1; idx <= q; ++idx){

i=i+1;

System.out.print("Question "+i+") ");

int ran1 = random1.nextInt(max - min + 1) + min;

int ran2 = random2.nextInt(max - min + 1) + min;

if(ran2>ran1){

ran1 = ran1 + ran2;

ran2 = ran1 - ran2;

ran1 = ran1 - ran2;

}

System.out.print(ran1 + " - " +ran2+" = ");

int sub=scan.nextInt();

int total=ran1-ran2;

if(sub!=total){

System.out.println("No, "+ran1+" - "+ran2+" is "+total);

w++;

}

else{

System.out.println("Correct");

r++;

}

}

double percent=r/q;

System.out.println("Your score is: "+percent*100+"%");

}

}

Thanks

Try the following code:

import java.util.*;

public class Subtraction {

public static void main(String[] args) throws Exception {

int i=0;

int min=10, max=99;

Scanner scan = new Scanner(System.in);

Random random1 = new Random();

Random random2 = new Random();

System.out.print("How many problems would you like? ");

double q = scan.nextDouble();

System.out.println();

double r=0,w=0;

for (int idx = 1; idx <= q; ++idx){

i=i+1;

System.out.print("Question "+i+") ");

int ran1 = random1.nextInt(max - min + 1) + min;

int ran2 = random2.nextInt(max - min + 1) + min;

if(ran2>ran1){

ran1 = ran1 + ran2;

ran2 = ran1 - ran2;

ran1 = ran1 - ran2;

}

System.out.print(ran1 + " - " +ran2+" = ");

int sub=scan.nextInt();

int total=ran1-ran2;

if(sub!=total){

System.out.println("No, "+ran1+" - "+ran2+" is "+total);

w++;

}

else{

System.out.println("Correct");

r++;

}

}

double percent=r/q;

System.out.println("Your score is: "+percent*100+"%");

}

}

Thanks

Related Tutorials/Questions & Answers:

Advertisements