Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a simple formula test program based on Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to write a simple formula test program based on Java". The editor shows you the operation process through an actual case, the operation method is simple and fast, and it is practical. I hope this article "how to write a simple formula test program based on Java" can help you solve the problem.

1. Program function

This program can automatically generate a specified number of addition, subtraction and multiplication "three operations" questions, the user can answer, after the answer, will display the number of correct questions, accuracy and time spent.

two。 Realization idea

First, we create a new class file named Calculate, and then we can type our code. Next, we first write a main function, and then the first step is to create a Scanner object that accepts the number of questions entered by the user. Next we define several variables to save the information of the program, that is, result saves the answer of the formula, right saves the number of correct answers, and accuracy saves the accuracy. Next, start the timing, and then create a Random object to generate random numbers. In the for loop, we control the range of random numbers by setting the number in parentheses in r.nextInt (). Then, we decide which formula operation to do through the three cases of the third random number. Next, determine whether the answer is consistent with the user's input, and output the prompt information. Then finish the timing and output the results.

3. Specific code import java.util.*;public class Calculate {public static void main (String [] args) {/ / accept user input Scanner sc = new Scanner (System.in); System.out.println ("Please enter the number of questions:"); int n = sc.nextInt () / / result saves arithmetic answers, right saves the number of correct answers, accuracy save accuracy int result = 0, right = 0; double accuracy; / / start timing long startTime = System.currentTimeMillis (); / / print addition and subtraction multiplication Random r = new Random (); for (int I = 0; I < n Int +) {System.out.println i1 = r.nextInt (50); int i2 = r.nextInt (50); int i3 = r.nextInt (3); / / determine which operation if (i3 = = 0) {System.out.println (i1 + "-" + i2 + "="); result = i1-i2 } else if (i3 = = 1) {System.out.println (i1 + "+ i2 +" = "); result = i1 + i2;} else {System.out.println (i1 +" * "+ i2 +" = "); result = i1 * i2 } / / output correct or not information, and judge whether to increase the number of correct questions int a = sc.nextInt (); if (a = = result) {System.out.println ("correct!") ; right + = 1;} else {System.out.println ("wrong answer, keep going!") ;}} / / calculation accuracy accuracy = (right * 1.0 / n) * 100; / / end timing long endTime = System.currentTimeMillis () / / output System.out.print ("Congratulations, you have answered" + right + "questions" + "," + "correct rate:" + accuracy + "%" + "," + "time:" + (endTime-startTime) / 1000 + "seconds"); / / turn off the use of resources without adding sc.close ();}} 4. Program execution result

This is the end of the content about "how to write a simple formula test program based on Java". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report