小编Jas*_*son的帖子

原始检查

每个素数都是6k + 1或6k-1的形式.为了检查数字是否为素数,我们可以使用以下算法.我见过基于这些算法编写的程序.

public boolean isPrime(int n)
{

    if (n <= 1)  return false;
    if (n <= 3)  return true;

    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我们以下列方式编写代码,我不明白会出现什么问题:

public boolean isPrime(int number){
        boolean primeFlag = false;
        if(number == 0 || number ==1){
            return primeFlag;
        }
        if(number == 2 || number == 3){
            primeFlag = true;
        }
        if((number+1)%6 == 0){ …
Run Code Online (Sandbox Code Playgroud)

java primes time-complexity

2
推荐指数
1
解决办法
109
查看次数

mCurrentIndex =(mCurrentIndex + 1)%mQuestionBank.length;

我不懂语法:

mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
Run Code Online (Sandbox Code Playgroud)

(mQuestionBank是一个数组).

是什么原因% mQuestionBank.length

java arrays android modulo

2
推荐指数
1
解决办法
379
查看次数

为什么不是java RuntimeException调用UncheckedException?

为什么Java语言设计者使用术语"RuntimeException"?这个坏术语不是吗?我的意思是在运行时会发生任何异常,无论是检查异常(例如IOException)还是未经检查的异常.

如果他们创建CheckedException和UncheckedException都延长了Throwable,那会不会更好?

java

1
推荐指数
1
解决办法
158
查看次数

带有准备好的语句的 JDBC 插入表示我有 SQL 语法错误

我正在尝试向表中进行简单插入,但我的程序说我有 SQL 语法错误。有任何想法吗?

SQL代码

CREATE TABLE ticket (
    start_time DATETIME NOT NULL,
    end_time DATETIME NOT NULL,
    seat VARCHAR(50) NOT NULL,
    sep VARCHAR(50) NOT NULL, 
    price INT(50) NOT NULL,
    foroom VARCHAR(50) NOT NULL,
    printer INT(15) NOT NULL, 
    PRIMARY KEY (seat)
);
Run Code Online (Sandbox Code Playgroud)

JAVA代码

try
{
    System.out.println("Attempting to connect...\n");
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/baseis?autoReconnect=true&useSSL=false","root","");
    System.out.println("Connection Succesful!!!");
    String sql = "INSERT INTO ticket (start_time)" + " VALUES (?)";
    PreparedStatement prepared = connection.prepareStatement(sql);
    prepared.setString(1,"2016-07-17 19:00:00");
    prepared.executeUpdate(sql);
}
catch(SQLException error)
{
    System.out.println("Error: " + error.getMessage());
}
Run Code Online (Sandbox Code Playgroud)

错误 …

java mysql sql-server jdbc insert

0
推荐指数
1
解决办法
1710
查看次数

将值从 Activity 传递到适配器

我有活动 A 和 B。活动 B 使用回收视图 Adpater 来显示信息列表。从活动 A 中,我将意图发送到用于工具栏标题的活动 B。但我在 Adapter 类中也需要相同的意图。

那么如何将 Intent 值从 Activity A 传递到 B,并在适配器类中也使用相同的 Intent。

我的活动 A 有代码

Intent new = new Intent(itemView.getContext(), B.class);
                phy.putExtra("key","This is title");   itemView.getContext().startActivity(new);
Run Code Online (Sandbox Code Playgroud)

在活动 B 中,我有

   getSupportActionBar().setTitle(getIntent().getStringExtra("key"));
Run Code Online (Sandbox Code Playgroud)

我也想要适配器类中的键值。

活动B的代码是

Intent new = new Intent(itemView.getContext(), B.class);
                phy.putExtra("key","This is title");   itemView.getContext().startActivity(new);
Run Code Online (Sandbox Code Playgroud)

我的适配器类有

   getSupportActionBar().setTitle(getIntent().getStringExtra("key"));
Run Code Online (Sandbox Code Playgroud)

提前致谢。

java android adapter android-arrayadapter android-intent

0
推荐指数
1
解决办法
1844
查看次数

JAVA SQL命令未正确结束

我有这个代码:

buy.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent actionEvent)
    {
        int r;
        r = table.getSelectedRow();
        String num = (String) table.getValueAt(r, 0);//numele jucariei
        //String cop = (String) table.getValueAt(r, 3);//nr de bucati

        try
        {
            pq = stmt.executeQuery("SELECT *" + "FROM buyid_view");
            xv = stmt.executeQuery("SELECT toyid, copies " + "FROM alldatas_view" + "WHERE toyname ='"+num+"'");
            int buyid = pq.getInt("buyid");
            int toyid = xv.getInt("toyid");
            int copies = xv.getInt("copies");
            copies = copies-1;
            CallableStatement cstmt = con.prepareCall("INSERT INTO buy (buyid, toyid)" + "VALUES (?,?)");
            cstmt.setInt("buyid", buyid); …
Run Code Online (Sandbox Code Playgroud)

java oracle

-4
推荐指数
1
解决办法
581
查看次数