import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Main{
public static double max(double...values)
{
double largest=Double.MIN_VALUE;
for (double v:values)
if(v>largest) largest=v;
return largest;
}
public static void main(String args[]) {
System.out.println("Max:"+max(1,11,300,2,3));
}
}
![]()
// MethodOverload.java
// Using overloaded methods
public class Main {
public static void main(String[] args) {
System.out.println("The square of integer 7 is " + square(7));
System.out.println("\nThe square of double 7.5 is " + square(7.5));
}
public static int square(int x) {
return x * x;
}
public static double square(double y) {
return y * y;
}
}
![]()
import java.math.BigInteger;
import java.util.Scanner;
public class CalculateN {
/**
* @param args
*/
public static void main(String[] args) {
System.out.print("请输入N:");
Scanner scanner=new Scanner(System.in);
int number=scanner.nextInt();
System.out.println(number+"!="+calculateN2(number));
}
public static long calculateN(int n) {
if(n==1 || n==0){
return 1;
}
return n*calculateN(n-1);
}
public static BigInteger calculateN2(int n) {
if(n==1 || n==0){
return BigInteger.valueOf(1);
}
return BigInteger.valueOf(n).multiply(calculateN2((n-1)));
}
}
![]()
import java.util.Random;
public class Main
{
public static void main(String[] args)
{
Random r1 = new Random(50);
System.out.println("第一个种子为50的Random对象");
System.out.println("r1.nextBoolean():\t" + r1.nextBoolean());
System.out.println("r1.nextInt():\t\t" + r1.nextInt());
System.out.println("r1.nextDouble():\t" + r1.nextDouble());
System.out.println("r1.nextGaussian():\t" + r1.nextGaussian());
System.out.println("---------------------------");
Random r2 = new Random(50);
System.out.println("第二个种子为50的Random对象");
System.out.println("r2.nextBoolean():\t" + r2.nextBoolean());
System.out.println("r2.nextInt():\t\t" + r2.nextInt());
System.out.println("r2.nextDouble():\t" + r2.nextDouble());
System.out.println("r2.nextGaussian():\t" + r2.nextGaussian());
System.out.println("---------------------------");
Random r3 = new Random(100);
System.out.println("种子为100的Random对象");
System.out.println("r3.nextBoolean():\t" + r3.nextBoolean());
System.out.println("r3.nextInt():\t\t" + r3.nextInt());
System.out.println("r3.nextDouble():\t" + r3.nextDouble());
System.out.println("r3.nextGaussian():\t" + r3.nextGaussian());
Random r4 = new Random(System.currentTimeMillis());
System.out.println("以当前时间为种子的Random对象");
System.out.println("r3.nextBoolean():\t" + r4.nextBoolean());
System.out.println("r3.nextInt():\t\t" + r4.nextInt());
System.out.println("r3.nextDouble():\t" + r4.nextDouble());
System.out.println("r3.nextGaussian():\t" + r4.nextGaussian());
}
}
![]()
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Random rand = new Random();
System.out.println("rand.nextBoolean():" + rand.nextBoolean());
byte[] buffer = new byte[16];
rand.nextBytes(buffer);
System.out.println(Arrays.toString(buffer));
//生成0.0~1.0之间的伪随机double数
System.out.println("rand.nextDouble():" + rand.nextDouble());
//生成0.0~1.0之间的伪随机float数
System.out.println("rand.nextFloat():" + rand.nextFloat());
//生成平均值是 0.0,标准差是 1.0的伪高斯数
System.out.println("rand.nextGaussian():" + rand.nextGaussian());
//生成一个处于long整数取值范围的伪随机整数
System.out.println("rand.nextInt():" + rand.nextInt());
//生成0~26之间的伪随机整数
System.out.println("rand.nextInt(26):" + rand.nextInt(26));
//生成一个处于long整数取值范围的伪随机整数
System.out.println("rand.nextLong():" + rand.nextLong());
}
}
![]()
public class Main
{
public static void main(String[] args)
{
/*---------下面是三角运算---------*/
//将弧度转换角度
System.out.println("Math.toDegrees(1.57):" + Math.toDegrees(1.57));
//将角度转换为弧度
System.out.println("Math.toRadians(90):" + Math.toRadians(90));
//计算反余弦,返回的角度范围在 0.0 到 pi 之间。
System.out.println("Math.acos(0.3):" + Math.acos(1.2));
//计算反正弦;返回的角度范围在 -pi/2 到 pi/2 之间。
System.out.println("Math.asin(0.8):" + Math.asin(0.8));
//计算反正切;返回的角度范围在 -pi/2 到 pi/2 之间。
System.out.println("Math.atan(2.3):" + Math.atan(2.3));
//计算三角余弦。
System.out.println("Math.cos(1.57):" + Math.cos(1.57));
//计算值的双曲余弦。
System.out.println("Math.cosh(1.2 ):" + Math.cosh(1.2 ));
//计算正弦
System.out.println("Math.sin(1.57 ):" + Math.sin(1.57 ));
//计算双曲正弦
System.out.println("Math.sinh(1.2 ):" + Math.sinh(1.2 ));
//计算三角正切
System.out.println("Math.tan(0.8 ):" + Math.tan(0.8 ));
//计算双曲余弦
System.out.println("Math.tanh(2.1 ):" + Math.tanh(2.1 ));
//将矩形坐标 (x, y) 转换成极坐标 (r, thet));,返回所得角 theta。
System.out.println("Math.atan2(0.1, 0.2):" + Math.atan2(0.1, 0.2));
/*---------下面是取整运算---------*/
//取整,返回小于目标数的最大整数。
System.out.println("Math.floor(-1.2 ):" + Math.floor(-1.2 ));
//取整,返回大于目标数的最小整数。
System.out.println("Math.ceil(1.2):" + Math.ceil(1.2));
//四舍五入取整
System.out.println("Math.round(2.3 ):" + Math.round(2.3 ));
/*---------下面是乘方、开方、指数运算---------*/
//计算平方根。
System.out.println("Math.sqrt(2.3 ):" + Math.sqrt(2.3 ));
//计算立方根。
System.out.println("Math.cbrt(9):" + Math.cbrt(9));
//返回欧拉数 e 的n次幂。
System.out.println("Math.exp(2):" + Math.exp(2));
//返回 sqrt(x2:" +y2),没有中间溢出或下溢。
System.out.println("Math.hypot(4 , 4):" + Math.hypot(4 , 4));
// 按照 IEEE 754 标准的规定,对两个参数进行余数运算。
System.out.println("Math.IEEEremainder(5 , 2):" + Math.IEEEremainder(5 , 2));
//计算乘方
System.out.println("Math.pow(3, 2):" + Math.pow(3, 2));
//计算自然对数
System.out.println("Math.log(12):" + Math.log(12));
//计算底数为 10 的对数。
System.out.println("Math.log10(9):" + Math.log10(9));
// 回参数与 1 之和的自然对数。
System.out.println("Math.log1p(9):" + Math.log1p(9));
/*---------下面是符号相关的运算---------*/
//计算绝对值。
System.out.println("Math.abs(-4.5):" + Math.abs(-4.5));
//符号赋值,返回带有第二个浮点数符号的第一个浮点参数。
System.out.println("Math.copySign(1.2, -1.0):" + Math.copySign(1.2, -1.0));
//符号函数;如果参数为 0,则返回 0;如果参数大于 0,则返回 1.0;如果参数小于 0,则返回 -1.0。
System.out.println("Math.signum(2.3):" + Math.signum(2.3));
/*---------下面是大小相关的运算运算---------*/
//找出最大值
System.out.println("Math.max(2.3 , 4.5):" + Math.max(2.3 , 4.5));
//计算最小值
System.out.println("Math.min(1.2 , 3.4):" + Math.min(1.2 , 3.4));
//返回第一个参数和第二个参数之间与第一个参数相邻的浮点数。
System.out.println("Math.nextAfter(1.2, 1.0):" + Math.nextAfter(1.2, 1.0));
//返回比目标数略大的浮点数
System.out.println("Math.nextUp(1.2 ):" + Math.nextUp(1.2 ));
//返回一个伪随机数,该值大于等于 0.0 且小于 1.0。
System.out.println("Math.random():" + Math.random());
}
}
![]()
public class Main{
public static void main(String[] args) {
int result;
for (int x = 1; x <= 10; x++) {
result = square(x);
// Math库中也提供了求平方数的方法
// result=(int)Math.pow(x,2);
System.out.println("The square of " + x + " is " + result + "\n");
}
}
// 自定义求平方数的静态方法
public static int square(int y) {
return y * y;
}
}
![]()
// RollDie.java
// Roll a six-sided die 6000 times
import javax.swing.*;
public class Main {
public static void main( String args[] )
{
int frequency1 = 0, frequency2 = 0,
frequency3 = 0, frequency4 = 0,
frequency5 = 0, frequency6 = 0, face;
// summarize results
for ( int roll = 1; roll <= 6000; roll++ ) {
face = 1 + (int) ( Math.random() * 6 );
switch ( face ) {
case 1:
++frequency1;
break;
case 2:
++frequency2;
break;
case 3:
++frequency3;
break;
case 4:
++frequency4;
break;
case 5:
++frequency5;
break;
case 6:
++frequency6;
break;
}
}
JTextArea outputArea = new JTextArea( 7, 10 );
outputArea.setText(
"Face\tFrequency" +
"\n1\t" + frequency1 +
"\n2\t" + frequency2 +
"\n3\t" + frequency3 +
"\n4\t" + frequency4 +
"\n5\t" + frequency5 +
"\n6\t" + frequency6 );
JOptionPane.showMessageDialog( null, outputArea,
"Rolling a Die 6000 Times",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
![]()
// RandomInt.java
// Shifted, scaled random integers
import javax.swing.JOptionPane;
public class Main {
public static void main( String args[] )
{
int value;
String output = "";
for ( int i = 1; i <= 20; i++ ) {
value = 1 + (int) ( Math.random() * 6 );
output += value + " ";
if ( i % 5 == 0 )
output += "\n";
}
JOptionPane.showMessageDialog( null, output,
"20 Random Numbers from 1 to 6",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
![]()
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n=10000;
Scanner scanner = new Scanner(System.in);
int totalQuestions = 30;
int correctAnswers = 0;
int wrongAnswers = 0;
int countdownSeconds = 100; // 设置倒计时秒数
// CountdownTimer countdownTimer = new CountdownTimer(countdownSeconds);
// countdownTimer.start();
for (int i = 1; i <= totalQuestions; i++) {
System.out.println("问题 " + i + ":");
String question = generateQuestion();
System.out.println(question);
int userAnswer = scanner.nextInt();
int correctAnswer = evaluateQuestion(question);
while (userAnswer == correctAnswer)
{
System.out.println("回答正确!\n");
System.out.println("倒计时\n"+n);
n-=5;
correctAnswers++;
break;
}
while(userAnswer != correctAnswer)
{
System.out.println("回答错误。正确答案是: " + correctAnswer + "\n");
System.out.println("倒计时\n"+n);
n-=3;
wrongAnswers++;
break;
}
}
double accuracy = (double) correctAnswers / totalQuestions * 100;
System.out.println("答题结束。正确答案数量: " + correctAnswers);
System.out.println("错误答案数量: " + wrongAnswers);
System.out.println("正确率: " + accuracy + "%");
scanner.close();
}
private static int Judge(int a,int b,int c) {
if(a==b){
System.out.println("回答正确!\n");
b++;
return 0;
}else{
System.out.println("回答错误,正确答案是:"+b+"\n");
c++;
}
return 0;
}
// 生成随机四则运算题目
private static String generateQuestion() {
Random random = new Random();
int num1 = random.nextInt(100);
int num2 = random.nextInt(100);
int operator = random.nextInt(4);
String op = "";
int answer = 0;
switch (operator) {
case 0: // Addition
op = "+";
answer = num1 + num2;
break;
case 1: // Subtraction
op = "-";
answer = num1 - num2;
if (answer < 0) {
// 确保减法不出现负数
num1 = num2 + random.nextInt(1000 - num2); // 重新生成num1
answer = num1 - num2;
}
break;
case 2: // Multiplication
op = "x";
num1 = random.nextInt(100);
num2 = random.nextInt(100);
answer = num1 * num2;
if (answer >= 10000) {
// 确保乘法不出现4位数
num1 = random.nextInt(100);
num2 = random.nextInt(100);
answer = num1 * num2;
}
break;
case 3: // Division
op = "/";
num2 = random.nextInt(10) + 1; // 避免除以0
num1 = num2 * random.nextInt(100);
answer = num1 / num2;
break;
}
return num1 + " " + op + " " + num2 + " = ";
}
// 计算四则运算题目的答案
private static int evaluateQuestion(String question) {
String[] parts = question.split(" ");
int num1 = Integer.parseInt(parts[0]);
String operator = parts[1];
int num2 = Integer.parseInt(parts[2]);
switch (operator) {
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "x":
return num1 * num2;
case "/":
return num1 / num2;
default:
throw new IllegalArgumentException("无效的运算符: " + operator);
}
}
}
<!DOCTYPE html>
<html>
<head>
<title>User Login</title>
<style>
.container {
width: 300px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
text-align: center;
}
.input-group {
margin-bottom: 10px;
}
.input-group label {
display: block;
margin-bottom: 5px;
}
.input-group input {
width: 100%;
padding: 5px;
}
.input-group img {
margin-top: 10px;
}
.input-group button {
width: 100%;
padding: 7px;
background-color: #4CAF50;
color: white;
border: none;
}
</style>
<script>
function generateCaptcha() {
var captcha = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var captchaLength = 4;
for (var i = 0; i < captchaLength; i++) {
captcha += characters.charAt(Math.floor(Math.random() * characters.length));
}
// Display the generated captcha
document.getElementById('captcha').textContent = captcha;
// Update the captcha image source with the generated captcha
document.getElementById('captchaImage').src = 'https://dummyimage.com/150x50/000/fff&text=' + captcha;
}
function validateForm(event) {
event.preventDefault(); // Prevent form submission
var captchaInput = document.getElementById('captchaInput').value;
var captchaLabel = document.getElementById('captcha').textContent;
if (captchaInput.toUpperCase() === captchaLabel.toUpperCase()) {
// Correct captcha, perform actions for successful login
alert('Login successful!');
// Redirect to success page here
// Clear form data
document.getElementById('username').value = '';
document.getElementById('password').value = '';
document.getElementById('captchaInput').value = '';
// Generate a new captcha
generateCaptcha();
} else {
// Incorrect captcha, show error message
alert('Incorrect captcha!');
// Clear captcha input
document.getElementById('captchaInput').value = '';
// Generate a new captcha
generateCaptcha();
}
}
</script>
</head>
<body onload="generateCaptcha()">
<div class="container">
<h2>User Login</h2>
<form action="/login" method="post" onsubmit="validateForm(event)">
<div class="input-group">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
</div>
<div class="input-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
</div>
<div class="input-group">
<label for="captcha">Captcha:</label>
<input type="text" id="captchaInput" name="captcha" required>
<span id="captcha"></span>
<img src="" alt="Captcha" id="captchaImage">
<button type="button" onclick="generateCaptcha()">Refresh</button>
</div>
<div class="input-group">
<button type="submit">Login</button>
</div>
</form>
</div>
</body>
</html>