重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
应该命名为boy.java。。。。
坚守“ 做人真诚 · 做事靠谱 · 口碑至上 · 高效敬业 ”的价值观,专业网站建设服务10余年为成都成都木屋小微创业公司专业提供企业网站制作营销网站建设商城网站建设手机网站建设小程序网站建设网站改版,从内容策划、视觉设计、底层架构、网页布局、功能开发迭代于一体的高端网站建设服务。
类名是boy,不是boy extends Applet,extends只是描述。。。。
另外,类名建议首字母大写
求素数,比较经典的,下面是代码及注释
================================================
public class Sushu {
/**
* 判断一个数是不是素数
* @param a 被判断的数
* @return 是素数返回真
*/
public boolean isSuhu(int a){
boolean isSushu=true;
//根据素数的性质判断一个数是否为素数
for(int i=2;ia;i++){
if(a%i==0){
isSushu=false;
break;
}
}
return isSushu;
}
/**
* 判断连续若干个数中那些是素数
* @param start 起始数
* @param end 终止数
*/
public void selectSushu(int start,int end){
//判断一串数中那些为素数,并将结果打印出来
for(int i=start;i=end;i++){
if(isSuhu(i)){
System.out.println(i);
}
}
}
public static void main(String []args){
//定义起始位置和终止位置
int start=1;
int end=100;
//声明变量
Sushu s=new Sushu();
//调用方法
s.selectSushu(start, end);
}
}
这是我晓得的最简单的java小程序代码了你可以看看:
package com.kenki.emp;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.SQLException;
import java.sql.*;
public class emp extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String code = request.getParameter("code");
String name = request.getParameter("name");
String pay = request.getParameter("pay");
System.out.println("empcode:" + code);
System.out.println("name:" + name);
System.out.println("pay:" + pay);
//创建驱动
new com.microsoft.jdbc.sqlserver.SQLServerDriver();
String strd =
"jdbc:microsoft:sqlserver://localhost:1433;databasename=emp_dates";
String username = "sa";
String pws = "";
try {
java.sql.Connection conn = java.sql.DriverManager.getConnection(
strd, username, pws);
String strs = "insert into emp values(?,?,?)";
java.sql.PreparedStatement pre = conn.prepareStatement(strs);
pre.setString(1, code);
pre.setString(2, name);
pre.setString(3, pay);
pre.execute();
pre.close();
conn.close();
//重定向至查询页面
out.println("成功保存!!");
response.sendRedirect("emp.html");
} catch (SQLException ss) {
ss.printStackTrace();
response.sendRedirect("/WebModule1/error.html");
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
import java.util.Scanner;
public class a {
public static void main(String[] args) {
String result=null;
double sum = 0;
int i=0;
String str = "";
do {
Scanner in = new Scanner(System.in);
System.out.print("输入姓名:");
String name=in.next();
System.out.print("输入学号:");
String num = in.next();
System.out.print("输入分数:");
double score = in.nextDouble();
i++;
sum+=score;
System.out.print("是否结束:(end/其它任意键)");
result = in.next();
str+=name+"\r"+num+"\r\t"+score+"\r";
} while (!result.equals("end"));
System.out.println("输出信息结果:"+str+"\r\n");
System.out.println("平均分:"+sum/i);
}
}
直接复制一下,就可以运行,看是不是你要的效果。(不过没有对输入的值做判断,自己可以对输入做一下判断)
这个也可以用for循环来做
我给你一个功能强大的计算器!已运行通过!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator3 extends JFrame implements ActionListener {
private boolean dotExist, operated, equaled; // 帮助运算的布尔变量
private double storedNumber; // 目前的结果
private char lastOperator; // 表示上一运算符
private JTextField operation; // 结果栏
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 运算符
private JButton[] numbers; // 数字
// 构造者
public Calculator3() {
setTitle("Calculator");
// 初始化变量
dotExist = false; // 表示当前的数是否有小数点
operated = false; // 表示任意运算符是否被按下
equaled = false; // 表示等号是否被按下
storedNumber = 0;
lastOperator = '?';
// 初始化窗口变量
operation = new JTextField("0");
operation.setEditable(false);
numbers = new JButton[10];
for (int i = 0; i 10; i++)
numbers[i] = new JButton("" + i);
dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
sqrt = new JButton("√");
equal = new JButton("=");
changePN = new JButton("±");
clear = new JButton("AC");
// 将窗口物体放入窗口
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
addComponent(layout, operation, 0, 0, 4, 1);
addComponent(layout, numbers[1], 1, 0, 1, 1);
addComponent(layout, numbers[2], 1, 1, 1, 1);
addComponent(layout, numbers[3], 1, 2, 1, 1);
addComponent(layout, numbers[4], 2, 0, 1, 1);
addComponent(layout, numbers[5], 2, 1, 1, 1);
addComponent(layout, numbers[6], 2, 2, 1, 1);
addComponent(layout, numbers[7], 3, 0, 1, 1);
addComponent(layout, numbers[8], 3, 1, 1, 1);
addComponent(layout, numbers[9], 3, 2, 1, 1);
addComponent(layout, dot, 4, 0, 1, 1);
addComponent(layout, numbers[0], 4, 1, 1, 1);
addComponent(layout, sqrt, 4, 2, 1, 1);
addComponent(layout, plus, 1, 3, 1, 1);
addComponent(layout, minus, 2, 3, 1, 1);
addComponent(layout, multi, 3, 3, 1, 1);
addComponent(layout, div, 4, 3, 1, 1);
addComponent(layout, equal, 5, 0, 2, 1);
addComponent(layout, changePN, 5, 2, 1, 1);
addComponent(layout, clear, 5, 3, 1, 1);
}
// 对按钮进行反应的方法
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
if (btn == clear) {
operation.setText("0");
dotExist = false;
storedNumber = 0;
lastOperator = '?';
} else if (btn == equal) {
operate('=');
equaled = true;
} else if (btn == plus) {
operate('+');
equaled = false;
} else if (btn == minus) {
operate('-');
equaled = false;
} else if (btn == multi) {
operate('*');
equaled = false;
} else if (btn == div) {
operate('/');
equaled = false;
} else if (btn == changePN) {
operate('p');
operate('=');
equaled = true;
} else if (btn == sqrt) {
operate('s');
operate('=');
equaled = true;
} else {
if (equaled)
storedNumber = 0;
for (int i = 0; i 10; i++)
if (btn == numbers[i]) {
if (operation.getText().equals("0"))
operation.setText("" + i);
else if (!operated)
operation.setText(operation.getText() + i);
else {
operation.setText("" + i);
operated = false;
}
}
if (btn == dot !dotExist) {
operation.setText(operation.getText() + ".");
dotExist = true;
}
}
}
// 进行运算的方法
private void operate(char operator) {
double currentNumber = Double.valueOf(operation.getText())
.doubleValue();
if (lastOperator == '?')
storedNumber = currentNumber;
else if (lastOperator == '+')
storedNumber += currentNumber;
else if (lastOperator == '-')
storedNumber -= currentNumber;
else if (lastOperator == '*')
storedNumber *= currentNumber;
else if (lastOperator == '/')
storedNumber /= currentNumber;
else if (lastOperator == 'p')
storedNumber *= -1;
else if (lastOperator == 's')
storedNumber = Math.sqrt(currentNumber);
else if (lastOperator == '=' equaled)
storedNumber = currentNumber;
operation.setText("" + storedNumber);
operated = true;
lastOperator = operator;
}
// 快捷使用GridBagLayout的方法
private void addComponent(GridBagLayout layout, Component component,
int row, int col, int width, int height) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton) component).addActionListener(this);
getContentPane().add(component);
}
// 主方法初始化并显示窗口
public static void main(String[] args) {
Calculator3 calc = new Calculator3();
calc.setSize(290, 400);
calc.setVisible(true);
}
}
代码如下:
public class HelloWorld {
public static void main(String []args) {
int a = 3, b = 7 ;
System.out.println("Hello World!");
}
public static int f(int a, int b){
return a*a + a*b + b*b;
}
}
结果如下: