java中我要实现读取在1到100之间的整数,然后计算每个数出现的次数

java中我要实现读取在1到100之间的整数,然后计算每个数出现的次数,第1张

import javautilArrays;

public class MyClass {

int[] count;  //每个数字的数量

public static void main(String[] args) {

new MyClass();

}

public MyClass() {

//初始化

count = new int[10];

Arraysfill(count, 0);

//进行统计

for(int i=1;i<=100;i++) {

statistics(i);

}

//输出

for(int i=0;i<10;i++) {

Systemoutprintln("数字" + i + "有" + count[i] + "个");

}

}

//统计

private void statistics(int value) {

String valueString = StringvalueOf(value);

for(int i=0;i<valueStringlength();i++) {

char index = valueStringcharAt(i);

count[new Integer(StringvalueOf(index))] ++;

}

}

}

具体我也不会,但有一篇文档或许对你有帮助,这两个都是监听windows( *** 作系统)的事件,你可以去看看JInvokejar,相关的api。下面是监听鼠标 *** 作的例子。补充一下,不要把它想象成javaGUI本身的事件,其实他是在监听windows级别的事件,及底层的事件,可以做一些小病毒的。

import static comjinvokewin32WinConstants;

import javaawtBorderLayout;

import javaawtFlowLayout;

import javaawtTextArea;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaxswingBorderFactory;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJPanel;

import javaxswingJScrollPane;

import comjinvokeCallback;

import comjinvokeJInvoke;

import comjinvokeNativeImport;

import comjinvokeUtil;

import comjinvokewin32User32;

import comjinvokewin32structsMsg;

public class MouseHook extends JPanel{

static {

JInvokeinitialize();

}

@NativeImport(library = "user32")

public native static int SetWindowsHookEx (int idHook, Callback hookProc, int hModule, int dwThreadId);

@NativeImport(library = "user32")

public native static int UnhookWindowsHookEx (int idHook);

public static final int WH_MOUSE_LL = 14;

static JFrame frame;

static TextArea mouseEventArea = new TextArea();

static JButton setHookBtn;

static JButton removeHookBtn;

public MouseHook() {

super(new BorderLayout());

mouseEventAreasetText("1) Click the \"Set Mouse Hook\" button\n" +

"2) Start clicking anywhere on the desktop Mouse clicks will be captured here\n" +

"3) Stop the mouse hook by clicking the \"Remove Mouse Hook\" button\n\n");

JScrollPane MouseEventPane = new JScrollPane(mouseEventArea);

add(MouseEventPane, BorderLayoutCENTER);

JPanel buttonPanel = new JPanel();

buttonPanelsetBorder(BorderFactorycreateEmptyBorder(10,10,10,10));

buttonPanelsetLayout(new FlowLayout(FlowLayoutRIGHT));

setHookBtn = new JButton("Set Mouse Hook");

setHookBtnaddActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

setMouseHook();

}} );

removeHookBtn = new JButton("Remove Mouse Hook");

removeHookBtnaddActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

unsetMouseHook();

}} );

removeHookBtnsetEnabled(false);

buttonPaneladd(setHookBtn);

buttonPaneladd(removeHookBtn);

add(buttonPanel, BorderLayoutSOUTH);

}

private void setMouseHook() {

setHookBtnsetEnabled(false);

removeHookBtnsetEnabled(true);

// This hook is called in the context of the thread that installed it

// The call is made by sending a message to the thread that installed the hook

// Therefore, the thread that installed the hook must have a message loop

//

// We crate a new thread as we don't want the AWT Event thread to be stuck running a message pump

// nor do we want the main thread to be stuck in running a message pump

Thread hookThread = new Thread(new Runnable(){

public void run() {

if (MouseProchookHandle == 0) {

int hInstance = User32GetWindowLong(UtilgetWindowHandle(frame), GWL_HINSTANCE);

MouseProchookHandle = SetWindowsHookEx(WH_MOUSE_LL,

new Callback(MouseProcclass, "lowLevelMouseProc"),

hInstance,

0);

// Standard message dispatch loop (message pump)

Msg msg = new Msg();

while (User32GetMessage(msg, 0, 0, 0)) {

User32TranslateMessage(msg);

User32DispatchMessage(msg);

}

} else {

mouseEventAreaappend("The Hook is already installed\n");

}

}});

hookThreadstart();

}

private void unsetMouseHook() {

setHookBtnsetEnabled(true);

removeHookBtnsetEnabled(false);

UnhookWindowsHookEx(MouseProchookHandle);

MouseProchookHandle = 0;

}

private static void createAndShowGUI() {

//Create and set up the window

frame = new JFrame("Mouse Hook");

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

MouseHook MouseEventsWindow = new MouseHook();

MouseEventsWindowsetBorder(BorderFactorycreateEmptyBorder(5,5,5,5));

//Add content to the window

frameadd(MouseEventsWindow, BorderLayoutCENTER);

//Display the window

framepack();

framesetBounds(300, 200, 750, 600);

framesetVisible(true);

}

public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI

javaxswingSwingUtilitiesinvokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

class MouseProc {

static int hookHandle;

@NativeImport(library = "user32")

public native static int CallNextHookEx (int idHook, int nCode, int wParam, int lParam);

static {

JInvokeinitialize();

}

public static int lowLevelMouseProc(int nCode, int wParam, int lParam ) {

if (nCode < 0)

return CallNextHookEx(hookHandle, nCode, wParam, lParam);

if (nCode == HC_ACTION) {

MouseHookStruct mInfo = UtilptrToStruct(lParam, MouseHookStructclass);

String message = "Mouse pt: (" + mInfoptx + ", " + mInfopty + ") ";

switch (wParam) {

case WM_LBUTTONDOWN:

message += "Left button down";

break;

case WM_LBUTTONUP:

message += "Left button up";

break;

case WM_MOUSEMOVE:

message += "Mouse moved";

break;

case WM_MOUSEWHEEL:

message += "Mouse wheel rotated";

break;

case WM_RBUTTONDOWN:

message += "Right button down";

break;

case WM_RBUTTONUP:

message += "Right button down";

break;

}

Systemoutprintln(message);

// MouseHookmouseEventAreaappend(message+"\n");

}

return CallNextHookEx(hookHandle, nCode, wParam, lParam);

}

}

=============================================

import comjinvokeNativeStruct;

import comjinvokewin32structsPoint;

@NativeStruct

public class MouseHookStruct {//MSLLHOOKSTRUCT

public Point pt = new Point();

public int mouseData;

public int flags;

public int time;

public int dwExtraInfo;

}

java循环次数_java学习日记,循环次数的确定

一个循环的次数的确定,一定是循环变量变化的过程来确定的。

在一个for循环里,控制循环次数的方法有如下几种:

1这种方法是通过控制循环执行条件和起始值来控制循环次数。这是最基础的方法,也是最简单,最常用的方法。

例如:

for(i=0;i<100;i++){

Systemoutprint(i);

}

这段代码的循环次数是100次,因为循环起始值是0,循环执行条件是循环变量小于100,循环变量每次变化过程是自增1,

所以循环次数是100次。

2这种方法是通过控制循环变量变化的过程来控制循环次数

for(i=0;i<200;i+=2){

Systemoutprint(i);

}

这段代码的循环次数也是100次,但是循环变量的执行条件是循环变量小于200,循环变量每次自增2,每当增加2,循环进行一次,

那么循环次数就等于200/2=100次

3这种方法是通过循环体内控制循环的次数

for(i=0;i<200;i++)1{

Systemoutprint(i);

if(i==99){

break;

}

}

这段代码循环次数理应是200次,但实际执行了100次,是通过循环体内某个条件控制循环结束的时机,当i=99的时候,循环结束。

4,这种方法是通过循环体内筛选循环变量的值来控制实际使用循环次数。

for(i=0;i<200;i++){

if(i%2==0){

Systemoutprint(i);

}

}

这段代码循环次数是200次,但是结果是100个,实际我们使用了100次循环,是通过在循环体内筛选循环变量的值控制使用循环次数。

把你的程序发出来吧,不然怎么看的到你的错误的地方呢

import javautilScanner;

public class test {

public static void main(String[] args) {

Scanner input = new Scanner(Systemin);

int[] zs = new int[5]; //数组默认全初始化为0

Systemoutprintln("Enter the integers between 1 and 100:"); //输入数

for (int j = 0; j < zslength; j++) {

inputhasNextInt(); //逐个输入内容,这里没有判定非数字

zs[j] = inputnextInt(); //存储到数组

}

Systemoutprintln("end input");

int temp = 0; //用来控制数组下标,读取数组

int total[] = new int[100]; //用来保存每个数次数的数组

while (temp < zslength) {

if (zs[temp] > 100 || zs[temp] < 1) { //如果不在范围内,则跳入下一个数

temp++;

continue;

}

total[zs[temp]-1]++; //zs[temp]表示输入的数 -1是用来方便输出 存入次数

temp++;

}

for (int i = 0; i < totallength; i++) {

if(i%4==0){

Systemoutprintln();

}

Systemoutprint("第 " + (i+1) + " 数,出现:" + total[i] + "次\t\t");

}

}

}

简单写下,思路大概是将文件流里的读到字节数组中,然后在通过字符串将其转化成字符数组:

import javaioFile;

import javaioFileInputStream;

import javaioInputStream;

public class Test{

    static int totalCount(String fileName, char ch) throws Exception{

        int count = 0;

        InputStream inStream = new FileInputStream(new File(fileName));

        byte[] buf = new byte[inStreamavailable()];

        inStreamread(buf);

        String str = new String(buf);

        char[] charArray = strtoCharArray();

        for(char c : charArray){

            if(c == ch){

                count ++;

            }

        }

        inStreamclose();

        return count;

    }

       

       

    public static void main(String[] args) throws Exception {

        Systemoutprintln("D://ttxt : " + totalCount("D://ttxt",'a'));

    }

}

以上就是关于java中我要实现读取在1到100之间的整数,然后计算每个数出现的次数全部的内容,包括:java中我要实现读取在1到100之间的整数,然后计算每个数出现的次数、java统计打开次数问题求教、java怎么看for最后循环多少次等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9809223.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存