我有如下表格,它需要向我的 java Servlet 发送一个操作来更新数据库。
如何在没有页面重新加载的情况下提交表单?目前action="myServlet"它让我直接进入一个新页面。如果我删除对 myServlet 的操作,则输入不会添加到我的数据库中。
<form name="detailsForm" method="post" action="myServlet"
onsubmit="return submitFormAjax()">
name: <input type="text" name="name" id="name"/> <br/>
<input type="submit" name="add" value="Add" />
</form>
Run Code Online (Sandbox Code Playgroud)
在我的 Java servlet 视图中,request.getParameter将查找名称并继续将其添加到我的数据库中。
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
if (request.getParameter("add") != null) {
try {
Table.insert(name);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的 JavaScript 部分,我有一个submitFormAjax函数
function submitFormAjax()
{
var xmlhttp;
if (window.XMLHttpRequest) {
// code for modern browsers
xmlhttp …Run Code Online (Sandbox Code Playgroud) int num = 10;
Random rand = new Random();
int ran = rand.nextInt(num);
if (ran==0){
ran= ran+1;
}
System.out.println("random : "+ran);
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止编码的,有更好的方法吗?当随机为0时,我觉得这是硬编码,我加1.
当将int转换为二进制时,如何将其输出为8个字符,目前它只显示1和短的7个零
int x = 1;
String bin = Integer.toBinaryString(x);
System.Out.Println(bin);
Run Code Online (Sandbox Code Playgroud)
示例输出到0000 0001
例如下面的代码.它有一个随机类.然而,它总是在各处产生相同的输出.在这种情况下哪个项目是种子?
来源:链接
import java.util.Random;
public class RandomTest {
public static void main(String[] s) {
Random rnd1 = new Random(42);
Random rnd2 = new Random(42);
System.out.println(rnd1.nextInt(100)+" - "+rnd2.nextInt(100));
System.out.println(rnd1.nextInt()+" - "+rnd2.nextInt());
System.out.println(rnd1.nextDouble()+" - "+rnd2.nextDouble());
System.out.println(rnd1.nextLong()+" - "+rnd2.nextLong());
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个文本文件data.txt.我想将数据输入到Hashmap中并进行一些数据映射.什么时候我没有点()来达到值.我会收到一个错误
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
Run Code Online (Sandbox Code Playgroud)
如何通过跳过没有点(.)的条目来克服它.
我创建了一个小片段来说明我的问题.
static HashMap<String, String> newList = new HashMap<>();
public static void main(String[] args) throws FileNotFoundException, IOException {
String inputFile = "data.txt";
BufferedReader brInput = new BufferedReader(new FileReader(inputFile));
String line;
while ((line = brInput.readLine()) != null) {
newList.put(line, "x");
}
for (Map.Entry<String, String> entry : newList.entrySet()) {
String getAfterDot = entry.getKey();
String[] split = getAfterDot.split("\\.");
String beforeDot = "";
beforeDot = getAfterDot.substring(0, getAfterDot.lastIndexOf("."));
System.out.println(beforeDot);
}
}
Run Code Online (Sandbox Code Playgroud)
data.txt中
0 …Run Code Online (Sandbox Code Playgroud) const char chars[] = "abcdef ....";
char result[...];
memcpy(result, chars, sizeof(chars));
for (unsigned i = 0; i < (sizeof(chars)-1); ++i) {
unsigned j = rand() % sizeof(chars);
char tmp = result[j];
result[j] = result[i];
result[i] = tmp;
}
Run Code Online (Sandbox Code Playgroud)
将结果写入文本文件的问题.
我创建了一个文本文件love.txt:
i love you
you love me
Run Code Online (Sandbox Code Playgroud)
如何将它们存储在独立的阵列,即line1和line2,然后在控制台显示出来?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line1[30];
string line2[30];
ifstream myfile("love.txt");
int a = 0;
int b = 0;
if(!myfile)
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
while(!myfile.eof())
{
getline(myfile,line1[a],' ');
cout<<"1."<<line1[a]<<"\n";
getline(myfile,line2[b],' ');
cout<<"2."<<line2[b]<<"\n";
}
}
Run Code Online (Sandbox Code Playgroud)