java提取c代码 java怎样从文件里提取数据

如何用Java实现如下这段C代码,循环那里是重点scanf类是判断输入的数值是否符合格式
while(true){//把while中的判断放到了while内部
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
if(a == nullb == null)
System.out.print(a + "" + b);
else
return;
}
Java获取字符串中字母出现的个数以下是Java获取字符串中字母出现的个数的示例代码:
```java
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个字符串:");
String input = sc.nextLine();
HashMapCharacter, Integer map = new HashMap();
for (char c : input.toCharArray()) {
if (Character.isLetter(c)) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
}
for (char key : map.keySet()) {
System.out.println(key + ": " + map.get(key));
}
}
}
```
首先,使用 `Scanner` 获取用户输入的字符串并存储在 `input` 变量中 。然后使用 `HashMap` 对每个字母出现次数进行计数,其中 `for` 循环遍历输入字符串中的每个字符,`Character.isLetter(c)` 判断字符是否为字母,如果是则判断其是否已经存在于 `map` 中,若存在则将其出现次数加一,否则将其加入到 `map` 中并将出现次数置为 1 。
最后,遍历 `map` 中的元素 , 并输出每个字母及其出现次数 。
谁能给我写一个linux下的java调用C代码的例子?我执行老是出问题java
public class HelloWorld
{
static
{
System.loadLibrary("native");
//对应C语言 动态库的名字
}
private native int hello(int[]arr,int num);
public static void main (String [] args)
{
int[]arr = {1,2,3,4};
HelloWorld n = new HelloWorld();
System.out.println(n.hello(arr,arr.length));
}
}
头文件
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:HelloWorld
* Method:hello
* Signature: ([I)I
*/
JNIEXPORT jint JNICALL Java_HelloWorld_hello
(JNIEnv *, jobject, jintArray);
#ifdef __cplusplus
}
#endif
#endif
c
#include jni.h
jintjhello(JNIEnv *env,jobject obj,jintArray arr,jint num)
{
jint *carr;
jint i, sum = 0;
carr = (*env)-GetIntArrayElements(env, arr, NULL);
if (carr == NULL) {
return 0; /* exception occurred */
【java提取c代码 java怎样从文件里提取数据】 }
for (i=0; inum; i++) {
sum += carr[i];
}
(*env)-ReleaseIntArrayElements(env, arr, carr, 0);
return sum;
}
JNINativeMethod method[] =
{
"hello","([II)I",(void *)jhello,
};
jint JNI_OnLoad(JavaVM *jvm,void *reserved)
{
printf("jnionload \n");
JNIEnv *env;
jclass jcls;
if((*jvm) -GetEnv(jvm,(void **)env,JNI_VERSION_1_2))
{
return JNI_ERR;
}
jcls = (*env) - FindClass(env,"HelloWorld");
if(jcls == NULL)
{
return JNI_ERR;
}
(*env) - RegisterNatives(env,jcls,method,sizeof(method)/sizeof(JNINativeMethod));
return JNI_VERSION_1_2;
}
主要是用jni.
java使用c的一段代码检查你的dll是否正确,最开始那个地方很容易出错 , 推荐使用Viewdll查看
java如何内嵌c代码?首选java提取c代码,直接内嵌是没法编译通过java提取c代码的 。一般通过jni方式调用c或者c++java提取c代码,或者c最后生成可移植性文件java提取c代码,通过java的api直接调用命令行 。

推荐阅读