我有这个在线发现的Python代码,想知道如何将它翻译成Java.我的问题不是算法,而是如何处理函数的参数.
这是代码:
def ternarySearch(f, left, right, absolutePrecision):
#left and right are the current bounds; the maximum is between them
if (right - left) < absolutePrecision:
return (left + right)/2
leftThird = (2*left + right)/3
rightThird = (left + 2*right)/3
if f(leftThird) < f(rightThird):
return ternarySearch(f, leftThird, right, absolutePrecision)
return ternarySearch(f, left, rightThird, absolutePrecision)
Run Code Online (Sandbox Code Playgroud)
我想知道函数定义是什么样的.例如,返回的函数y=x^2+3如下所示:
public static int y(int x){
return x*x+3;
}
Run Code Online (Sandbox Code Playgroud)
但
return ternarySearch(f, leftThird, right, absolutePrecision)
Run Code Online (Sandbox Code Playgroud)
不适合我,我想知道该怎么做.
所以例如我有y = 3*x + 2它会是这样的吗?
interface MyFunctor { …Run Code Online (Sandbox Code Playgroud) 我遇到了一些我无法理解的奇怪行为,我想知道是否有人可以帮助我理解它.
下面的方法重现一个对象数组并在数组成员中进行一些更改
public static void adjustRow(object[] row, String column)
{
Double price ,units ,invest;
if (!(Double.TryParse(row[3].ToString(),out price)
& Double.TryParse(row[4].ToString(),out units)
& Double.TryParse(row[5].ToString(),out invest)))
return ;
switch (column)
{
case INVEST: row[4] = Math.Round(invest / price,2); break;
case UNITS:
case PRICE: row[5] = Math.Round(units * price,2); break;
}
}
Run Code Online (Sandbox Code Playgroud)
以下方法调用上述方法:
void editGrids_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridView gridView = sender as DataGridView;
DataTable source= null;
if (gridView != null)source = gridView.DataSource as DataTable;
if (source != null)
Systm.adjustRow(source.Rows[e.RowIndex].ItemArray,gridView.Columns [e.ColumnIndex].HeaderText);
}
Run Code Online (Sandbox Code Playgroud)
我期望输入数组中的值也会在第一个方法范围之外更改,但实际结果是值是否保持相同的任何解释? …
void function(int a, int b, int c) {
char buffer1[5];
char buffer2[10];
}
Run Code Online (Sandbox Code Playgroud)
我们必须记住,内存只能以字大小的倍数来处理.在我们的例子中,一个字是4个字节,或32位.所以我们的5字节缓冲区实际上需要占用8个字节(2个字)的内存,而我们的10字节缓冲区将占用12个字节(3个字)的内存.这就是SP被减去20的原因.
为什么它不是ceil((5 + 10)/ 4)*4 = 16?
Helo,有3个文件,CustomerClient.java,CustomerServer.java和Customer.java
问题:在CustomerServer.java文件中,当我在以下行编译CustomerServer.java时出现错误: System.out.println(a [k] .getName());
错误:
init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\TLNA\My Documents\NetBeansProjects\Server\build\classes
C:\Documents and Settings\TLNA\My Documents\NetBeansProjects\Server\src\CustomerServer.java:44: cannot find symbol
symbol : method getName()
location: class Customer
System.out.println(a[k].getName());
1 error
BUILD FAILED (total time: 0 seconds)
Run Code Online (Sandbox Code Playgroud)
CustomerClient.java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class CustomerClient extends JApplet {
private JTextField jtfName = new JTextField(32);
private JTextField jtfSeatNo …Run Code Online (Sandbox Code Playgroud) 保持简单,我有一个服务器和客户端.一旦给出问题,服务器就会逐个发送问题,并向客户端发送答案.
那么,你会说这个应用程序是实时的吗?
我想在没有wincap库的情况下嗅探网络数据包,请给我一些提示或方向,以便我能够实现.