java的一些代码题

img


谁能告诉俺 这几个题的代码是啥 考试大概就考这种手写代码 后天就考了

其实就是百元买百鸡问题的变种,只是数字换了下

public class Main {
    public static void main(String[] args) {
        for (int nan = 0; nan <= 36; nan++)
            for (int nv = 0; nv <= 36; nv++) 
                for (int hai = 0; hai <= 36; hai += 2)
                {
                    if (nan * 4 + nv * 3 + hai / 2 == 36 && nan + nv + hai == 36)
                   System.out.println(nan + "," + nv + "," + hai);
                }
    }
}
结果
3,3,30

回文数,最简单的是转换为字符串,再颠倒,比较颠倒前后

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个整数:");
        int number = scanner.nextInt();
        boolean isPalindrome = IsHW(number);
        System.out.println("是否为回文数:" + isPalindrome);
    }

    public static boolean IsHW(int n) {
        String original = String.valueOf(n);
        String reversed = new StringBuilder(original).reverse().toString();
        return original.equals(reversed);
    }
}

后面两个可以用Swing实现

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        // 创建窗口
        JFrame frame = new JFrame("标签和文本框示例");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 150);

        // 创建面板
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());

        // 创建标签
        JLabel label = new JLabel("标签的值");
        panel.add(label);

        // 创建文本框
        JTextField textField = new JTextField(15);
        panel.add(textField);

        // 创建按钮
        JButton button = new JButton("复制标签");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String labelText = label.getText();
                textField.setText(labelText);
            }
        });
        panel.add(button);

        // 将面板添加到窗口
        frame.getContentPane().add(panel);

        // 显示窗口
        frame.setVisible(true);
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        // 创建窗口
        JFrame frame = new JFrame("按钮示例");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 150);

        // 创建面板
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());

        // 创建确定按钮
        JButton okButton = new JButton("确定");
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("OK");
            }
        });
        panel.add(okButton);

        // 创建退出按钮
        JButton exitButton = new JButton("退出");
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        panel.add(exitButton);

        // 将面板添加到窗口
        frame.getContentPane().add(panel);

        // 显示窗口
        frame.setVisible(true);
    }
}
  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/7743114
  • 这篇博客也不错, 你可以看下java在一个有序数组中查找某个值最左侧的位置(附上代码实现)
  • 同时,你还可以查看手册:java-学习Java语言 - 描述Java编程语言的基本概念和特点的课程。-类和对象》描述了如何编写创建对象的类,以及如何创建和使用对象。-课堂 中的内容
  • 除此之外, 这篇博客: java调用海康威视摄像头实现视频流实时查看以及视频保存中的 上边链接缺少的代码,根据下边的代码修改参考链接里代码的对应值: 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • class  CameraInfo
    {
     public String Ip;  
     public void setCameraIp(String ip){      // 设置IP    
            Ip = ip ; 
     }
     public String getCameraIp(){            // 取得IP   
            return Ip ;  
        }  
     
     public int  Port;  
     public void setCameraPort(int port){      // 设置Port    
            Port = port ;
     }
     public int getCameraPort(){            // 取得Port  
            return Port ;  
        }  
     
     public String UserName;  
     public void setUserName(String username){      // 设置用户名      
            UserName = username ;
     }
     public String getUserName(){            // 取得用户名  
            return UserName ;  
        }  
     
     public String  Pwd;
     public void setUserPwd(String pwd){      // 设置密码      
            Pwd = pwd ;
     }
     public String getUserPwd(){            // 取得密码   
            return Pwd ;  
        }  
     
     public NativeLong  UserId;
     public void setUserId(NativeLong userId){      // 设置用户Id    
      UserId = userId ;
     }
     public NativeLong getUserId(){            // 取得用户Id   
            return UserId ;  
        }  
     
     public NativeLong  Channel;
     public void setChannel(NativeLong channel){      // 设置通道      
      Channel = channel ;
     }
     public NativeLong getChannel(){            // 取得通道   
            return Channel ;  
        } 
     
     public String  Path;
     public String getPath( ){      // 设置路径     
      Path = "D:/TestFile/hikision/hikision";
      //Path = "/LicensePlateRecognition/ThePhoto/hikision/hikision";
      return Path;
     }
    }
    
  • 您还可以看一下 汪翠老师的java项目实战之欢乐斗地主游戏开发教程 毕业项目课程设计带源码课程中的 游戏效果展示小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:
    public class Code1_evenNumber {
        public static void main(String[] args) {
            printEvenNumbers();
        }
    
        public static void printEvenNumbers() {
            for (int i = 2; i <= 100; i += 2) {
                System.out.println(i);
            }
        }
    }
    

    输出: 2 4 6 ... 98 100

    public class Code2_factorial {
        public static void main(String[] args) {
            int n = 5;
            int result = factorial(n);
            System.out.println(result);
        }
    
        public static int factorial(int n) {
            if (n == 0 || n == 1) {
                return 1;
            } else {
                return n * factorial(n - 1);
            }
        }
    }
    

    输出: 120

    public class ListNode {
        int val;
        ListNode next;
    
        ListNode(int val) {
            this.val = val;
            this.next = null;
        }
    }
    
    public class Code3_reverseLinkedList {
        public static void main(String[] args) {
            ListNode head = new ListNode(1);
            head.next = new ListNode(2);
            head.next.next = new ListNode(3);
            head.next.next.next = new ListNode(4);
            head.next.next.next.next = new ListNode(5);
    
            ListNode reversed = reverseLinkedList(head);
            printLinkedList(reversed);
        }
    
        public static ListNode reverseLinkedList(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
    
            ListNode prev = null;
            ListNode curr = head;
            ListNode next = null;
    
            while (curr != null) {
                next = curr.next;
                curr.next = prev;
                prev = curr;
                curr = next;
            }
    
            return prev;
        }
    
        public static void printLinkedList(ListNode head) {
            ListNode curr = head;
            while (curr != null) {
                System.out.println(curr.val);
                curr = curr.next;
            }
        }
    }
    

    输出: 5 4 3 2 1