diff --git a/TomcatTest_0415/src/main/java/com/junit/junit_y.java b/TomcatTest_0415/src/main/java/com/junit/junit_y.java new file mode 100644 index 0000000000000000000000000000000000000000..705f25c7e6e55b5afb5189ed6c128acedb7b2498 --- /dev/null +++ b/TomcatTest_0415/src/main/java/com/junit/junit_y.java @@ -0,0 +1,61 @@ +package com.junit; + +import java.util.Scanner; +import java.util.Stack; + +public class junit_y { + public boolean kuohao(String s) { + /*题目描述 + * https://leetcode.cn/problems/valid-parentheses/description/ + * 给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。 + + 有效字符串需满足: + + 左括号必须用相同类型的右括号闭合。 + 左括号必须以正确的顺序闭合。 + 每个右括号都有一个对应的相同类型的左括号。 + + */ + Stack stack = new Stack<>(); + + //如果字符串长度不是偶数,则一定无法匹配。返回false + if (s.length() % 2 != 0) { + return false; + } else { //如果字符串长度是偶数,可能存在true,继续 + for (char item : s.toCharArray()) { //把输入的字符串变成字符数组,使用item遍历 + if (item == '(' || item == '[' || item == '{') { //如果遍历到的是左括号,则进栈,继续下一轮循环 + stack.push(item); + } else if (item == ')' || item == ']' || item == '}') { //如果遍历到的是右括号,可能涉及到出栈 + if (stack.isEmpty()) { //如果当前栈是空的,无法出栈。但是现在匹配到了右括号但没有左括号,说明匹配失败 + return false; + } else { //如果当前栈不为空 + char jtem = stack.pop(); //出栈,把栈顶元素赋值给jtem + if ((jtem == '(' && item == ')') || (jtem == '[' && item == ']') || (jtem == '{' && item == '}')) { //如果可以匹配成功,则继续下一轮循环 + continue; + } else { // 当前栈不空,且item与jtem无法匹配,说明匹配失败 + return false; + } + } + } + } + if (!stack.isEmpty()) { //如果遍历完字符数组以后栈不是空的,说明有多余的左括号,失败 + return false; + } else { //遍历完数组不为空,成功 + return true; + } + } + } + + public static void main(String [] args){ + junit_y y = new junit_y(); + Scanner scanner = new Scanner(System.in); + System.out.println("请输入需要判断是否匹配的字符串的值"); + String s = scanner.next(); + System.out.println("结果为:"+y.kuohao(s)); + + + } + +} + + diff --git a/TomcatTest_0415/src/main/java/com/maven/tomcattest_0415/HelloServlet.java b/TomcatTest_0415/src/main/java/com/maven/tomcattest_0415/HelloServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..1920296f93ccec7f94471b0bafb38c8437d31ebb --- /dev/null +++ b/TomcatTest_0415/src/main/java/com/maven/tomcattest_0415/HelloServlet.java @@ -0,0 +1,27 @@ +package com.maven.tomcattest_0415; + +import java.io.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; + +@WebServlet(name = "helloServlet", value = "/hello-servlet") +public class HelloServlet extends HttpServlet { + private String message; + + public void init() { + message = "Hello World!"; + } + + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + response.setContentType("text/html"); + + // Hello + PrintWriter out = response.getWriter(); + out.println(""); + out.println("

" + message + "

"); + out.println(""); + } + + public void destroy() { + } +} \ No newline at end of file diff --git a/TomcatTest_0415/src/main/webapp/WEB-INF/web.xml b/TomcatTest_0415/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000000000000000000000000000000000..d80081d1318531b6c30eaf0d748bf80a0b2e042a --- /dev/null +++ b/TomcatTest_0415/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/TomcatTest_0415/src/main/webapp/index.jsp b/TomcatTest_0415/src/main/webapp/index.jsp new file mode 100644 index 0000000000000000000000000000000000000000..3db4dc0cc1b4c659fee195ad5934808e585c56b5 --- /dev/null +++ b/TomcatTest_0415/src/main/webapp/index.jsp @@ -0,0 +1,15 @@ +<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> + + + + JSP - Hello World + + +

<%= "Hello World!" %> +

+

add something for homework which named git

+
+Hello Servlet +

change for homework named git

+ + \ No newline at end of file diff --git a/TomcatTest_0415/src/test/java/com/junit/junit_yTest.java b/TomcatTest_0415/src/test/java/com/junit/junit_yTest.java new file mode 100644 index 0000000000000000000000000000000000000000..52a43f2d550aa3f59b49d2db7d98c0aa4c608ed7 --- /dev/null +++ b/TomcatTest_0415/src/test/java/com/junit/junit_yTest.java @@ -0,0 +1,29 @@ +package com.junit; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * @author YangJinwen + */ +class junit_yTest { + @Test + public void kuohao_tiaojian() { + junit_y y = new junit_y(); + //测试长度不是偶数的情况 + assertFalse(y.kuohao("(")); + + + //测试长度是偶数的情况 + //测试当前遍历右括号但栈空的情况 + assertFalse(y.kuohao("()))")); + //测试当前遍历右括号,栈不空,无法匹配的情况 + assertFalse(y.kuohao("{())")); + //测试遍历完数组以后栈不空的情况 + assertFalse(y.kuohao("((")); + + //测试所有条件都为真的情况 + assertTrue(y.kuohao("()[]{}")); + } +} \ No newline at end of file diff --git a/TomcatTest_0415/target/TomcatTest_0415-1.0-SNAPSHOT/META-INF/MANIFEST.MF b/TomcatTest_0415/target/TomcatTest_0415-1.0-SNAPSHOT/META-INF/MANIFEST.MF new file mode 100644 index 0000000000000000000000000000000000000000..153e08e77dfd3f70c5677e1bd7a240930181ce87 --- /dev/null +++ b/TomcatTest_0415/target/TomcatTest_0415-1.0-SNAPSHOT/META-INF/MANIFEST.MF @@ -0,0 +1,5 @@ +Manifest-Version: 1.0 +Created-By: IntelliJ IDEA +Built-By: YangJinwen +Build-Jdk: Oracle OpenJDK version 19.0.2 + diff --git a/TomcatTest_0415/target/TomcatTest_0415-1.0-SNAPSHOT/WEB-INF/web.xml b/TomcatTest_0415/target/TomcatTest_0415-1.0-SNAPSHOT/WEB-INF/web.xml new file mode 100644 index 0000000000000000000000000000000000000000..d80081d1318531b6c30eaf0d748bf80a0b2e042a --- /dev/null +++ b/TomcatTest_0415/target/TomcatTest_0415-1.0-SNAPSHOT/WEB-INF/web.xml @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/TomcatTest_0415/target/TomcatTest_0415-1.0-SNAPSHOT/index.jsp b/TomcatTest_0415/target/TomcatTest_0415-1.0-SNAPSHOT/index.jsp new file mode 100644 index 0000000000000000000000000000000000000000..e739f7424be110ae74ae79aeac0ccfb44692ced6 --- /dev/null +++ b/TomcatTest_0415/target/TomcatTest_0415-1.0-SNAPSHOT/index.jsp @@ -0,0 +1,16 @@ +<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> + + + + JSP - Hello World + + +

<%= "Hello World!" %> +

+

add something for homework which named git

+gghtrsgnjurgnjvgrne +,vljfgihaglajl +
+Hello Servlet + + \ No newline at end of file