<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
html>
<html>
<head>
<meta charset="UTF-8">
<title>判断数值正负并显示不同颜色title>
head>
<body>
<%
// 定义一个变量来存储要判断的数值
double number = 10.5;
// 判断数值正负
if (number > 0) {
// 正数,使用绿色显示
out.print("这是一个正数");
} else if (number < 0) {
// 负数,使用红色显示
out.print("这是一个负数");
} else {
// 零,使用黑色显示
out.print("这是零");
}
%>
body>
html>
First defines a variable, number, to store the value to be judged. Then use the if and else if statements to determine whether the value is positive or negative, and print the corresponding text using out.print(). When you print the text, wrap it with the div tag and use the style attribute to set the text color. If the value is zero, "This is zero" is displayed, using black font.