网站建设资讯

NEWS

网站建设资讯

java一百行算法题代码,编程100题

求一Java代码,要简单的。大概一百多行的的。

题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?

创新互联拥有网站维护技术和项目管理团队,建立的售前、实施和售后服务体系,为客户提供定制化的网站设计制作、网站设计、网站维护、重庆服务器托管解决方案。为客户网站安全和日常运维提供整体管家式外包优质服务。我们的网站维护服务覆盖集团企业、上市公司、外企网站、商城网站建设、政府网站等各类型客户群体,为全球数千家企业提供全方位网站维护、服务器维护解决方案。

这个题目,下面我会贴出来两种代码。其实这个题目,我烦了简单计算,想搞得有趣味性一点,结果耽误了好几天时间,最后发现搞的也不好。

先第一种,为了解题而解题。

==== Main.java ====

public class Main {

private double TotalHeight = 100;

private double CurHeight = 50;

public void drop(int times) {

if ((times - 1) == 0) {

return;

}

setTotalHeight(getTotalHeight() + 2 * getCurHeight());

setCurHeight(getCurHeight() / 2);

drop(times - 1);

}

public double getTotalHeight() {

return TotalHeight;

}

public void setTotalHeight(double totalHeight) {

TotalHeight = totalHeight;

}

public double getCurHeight() {

return CurHeight;

}

public void setCurHeight(double curHeight) {

CurHeight = curHeight;

}

public static void main(String[] args) {

Main main = new Main();

main.drop(2);

System.out.println("Total height is " +

main.getTotalHeight());

System.out.println("Current height is " +

main.getCurHeight());

}

}

==== 然后是第二种 =====

==== Main.java ====

package main;

import javax.swing.JFrame;

import panel.BallPanel;

import time.Delay;

public class MainFrame extends JFrame {

public MainFrame(String title) {

super(title);

}

public static void main(String[] args) {

Delay delay = new Delay();

MainFrame frame = new MainFrame("Hello JFrame");

BallPanel ballPanel = new BallPanel();

frame.add(ballPanel);

frame.setSize(500, 500);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

delay.initDelay();

ballPanel.setDelay(delay.getDelay());

ballPanel.loopDrop(0);

}

}

==== Delay.java ====

package time;

public class Delay {

public void initDelay() {

int g = 10;

int i = 0;

double s, t, t0 = 0.0;

delay = new int[100];

for (s = 100; s 10100; s += 100) {

t = Math.sqrt(2 * s / g);

delay[i++] = (int) ((t - t0) * 100);

t0 = t;

}

}

public int[] getDelay() {

return delay;

}

private int delay[];

}

==== BallPanel.java ====

package panel;

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JPanel;

public class BallPanel extends JPanel {

public BallPanel() {

super();

}

public void paint(Graphics g) {

g.clearRect(0, 0, this.getWidth(), this.getHeight());

g.setColor(Color.BLUE);

g.fillOval(250, ballCenter, 30, 30);

}

public void loopDrop(int height) {

int i;

if (this.height == height) { // At bottom

for (i = 0; i targetHeight; i += MUL) {

ballCenter = this.height - i;

this.repaint();

try {

Thread.sleep(delay[(targetHeight - i - 1) / MUL]);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

loopDrop(this.height - i);

} else { // At top

for (i = height; i this.height; i += MUL) {

ballCenter = i;

this.repaint();

try {

Thread.sleep(delay[(i - height) / MUL]);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

targetHeightV = targetHeightV / 2;

targetHeight = targetHeightV;

if (targetHeight != 0) {

loopDrop(i);

}

}

}

public void setDelay(int delay[]) {

this.delay = delay;

}

private int extracted() {

return 100 * MUL;

}

private int targetHeight = extracted();

private int targetHeightV = extracted();

private int ballCenter = 0;

private int height = extracted();

private int delay[];

private final int MUL = 4;

}

其实所谓的第二种,是用的JFrame在JPanel上画图,直观的展示出来每次弹起来的效果。因为100像素实在太小了,所以我做了一个变量MUL,相

当于是等比例扩大的效果。问题就是弹不到10次距离就用光了,所以。。。。。权当娱乐了。。当然可以等比例在放大,例如100M看成是10000像素,这

样能多弹几次。这个程序,最后球就在那里不动了,程序不会自己退出。

求一个简单的JAVA游戏代码,100行左右,谢谢!

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Painter extends JFrame{

/**

*

*/

private static final long serialVersionUID = 8160427604782702376L;

CanvasPanel canvas = new CanvasPanel();;

public Painter() {

super("Star");

this.add(canvas);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.pack();

this.setResizable(false);

this.setLocationRelativeTo(null);

this.setVisible(true);

}

public static void main(String[] args) {

new Painter();

}

}

class CanvasPanel extends JPanel implements ActionListener{

/**

*

*/

private static final long serialVersionUID = -4642528854538741028L;

private JButton[] btn = new JButton[4];

private String[] btn_name = {"+", "-", "R", "L"};

private int center_x = 200, center_y = 200, radius = 100, degree = 0;

public CanvasPanel() {

this.setPreferredSize(new Dimension(400, 500));

this.setLayout(null);

for(int i = 0; i 4; i++) {

btn[i] = new JButton(btn_name[i]);

btn[i].setBounds(160 + i * 60, 425, 50, 50);

btn[i].addActionListener(this);

this.add(btn[i]);

}

}

@Override

public void paintComponent(Graphics g) {

super.paintComponent(g);

for(int i = 0; i 5; i++) {

g.drawLine( (int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i))),

(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i))),

(int) (center_x + radius * Math.sin(Math.toRadians(degree + 72 * i + 144))),

(int) (center_y - radius * Math.cos(Math.toRadians(degree + 72 * i + 144))));

}

}

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getActionCommand() == "+") {

if(radius 200)

radius += 2;

repaint();

} else if(e.getActionCommand() == "-") {

if(radius 0)

radius -= 2;

repaint();

} else if(e.getActionCommand() == "R") {

degree = (degree + 2) % 360;

repaint();

} else if(e.getActionCommand() == "L") {

degree = (degree - 2) % 360;

repaint();

}

}

}

Java程序,100行左右,需要运行成果图。 程序随便。 2个有追加

200多行,程序功能是输入两个数字的英文单词(20以内),输出结果,这个结果也是一个英文(就是将结果转换成英文输出)

import java.util.Scanner;

public class test{

private static String temp, tempNumA, tempNumB;

private static int geWei, shiWei, baiWei, judgeA, judgeB;

private static int number(String inputNum) {

if (inputNum.equalsIgnoreCase("zero")) {

return 0;

} else if (inputNum.equalsIgnoreCase("one")) {

return 1;

} else if (inputNum.equalsIgnoreCase("two")) {

return 2;

} else if (inputNum.equalsIgnoreCase("three")) {

return 3;

} else if (inputNum.equalsIgnoreCase("four")) {

return 4;

} else if (inputNum.equalsIgnoreCase("five")) {

return 5;

} else if (inputNum.equalsIgnoreCase("six")) {

return 6;

} else if (inputNum.equalsIgnoreCase("seven")) {

return 7;

} else if (inputNum.equalsIgnoreCase("eigth")) {

return 8;

} else if (inputNum.equalsIgnoreCase("nine")) {

return 9;

} else if (inputNum.equalsIgnoreCase("ten")) {

return 10;

} else if (inputNum.equalsIgnoreCase("eleven")) {

return 11;

} else if (inputNum.equalsIgnoreCase("twelve")) {

return 12;

} else if (inputNum.equalsIgnoreCase("thirteen")) {

return 13;

} else if (inputNum.equalsIgnoreCase("fourteen")) {

return 14;

} else if (inputNum.equalsIgnoreCase("fifteen")) {

return 15;

} else if (inputNum.equalsIgnoreCase("sixteen")) {

return 16;

} else if (inputNum.equalsIgnoreCase("seventeen")) {

return 17;

} else if (inputNum.equalsIgnoreCase("eighteen")) {

return 18;

} else if (inputNum.equalsIgnoreCase("nineteen")) {

return 19;

} else if (inputNum.equalsIgnoreCase("twenty")) {

return 20;

} else {

return -1;

}

}

private static String lessTen(int num) {

switch (num) {

case 0:

temp = "zero";

break;

case 1:

temp = "one";

break;

case 2:

temp = "two";

break;

case 3:

temp = "three";

break;

case 4:

temp = "four";

break;

case 5:

temp = "five";

break;

case 6:

temp = "six";

break;

case 7:

temp = "seven";

break;

case 8:

temp = "eight";

break;

case 9:

temp = "nine";

break;

default:

temp = "erro";

break;

}

return temp;

}

private static String moreThanTen(int num) {

if (num 20) {

switch (num) {

case 10:

temp = "ten";

break;

case 11:

temp = "eleven";

break;

case 12:

temp = "twelve";

break;

case 13:

temp = "thirteen";

break;

case 14:

temp = "fourteen";

break;

case 15:

temp = "fifteen";

break;

case 16:

temp = "sixteen";

break;

case 17:

temp = "seventeen";

break;

case 18:

temp = "eighteen";

break;

case 19:

temp = "nineteen";

break;

}

} else if (num = 20) {

switch (num) {

case 20:

temp = "twenty";

break;

case 30:

temp = "thirty";

break;

case 40:

temp = "forty";

break;

case 50:

temp = "fifty";

break;

case 60:

temp = "sixty";

break;

case 70:

temp = "seventy";

break;

case 80:

temp = "eighty";

break;

case 90:

temp = "ninety";

break;

}

}

return temp;

}

private static void outputReult(int num) {

if (num = 0 num = 9) {

System.out.println(lessTen(num));

} else if (num = 10 num = 19) {

System.out.println(moreThanTen(num));

} else if (num = 20 num = 99) {

shiWei = num / 10;

geWei = num % 10;

if (geWei == 0) {

System.out.println(moreThanTen(shiWei * 10));

} else {

System.out.println(moreThanTen(shiWei * 10) + "-"

+ lessTen(geWei));

}

} else if (num = 100 num = 400) {

baiWei = num / 100;

shiWei = num % 100 / 10;

geWei = num % 100 % 10;

if (shiWei == 0 geWei == 0) {

System.out.println(lessTen(baiWei) + " " + "hundred");

} else if (shiWei == 0 geWei != 0) {

System.out.println(lessTen(baiWei) + " " + "hundred" + " "

+ "and" + " " + lessTen(geWei));

} else if (shiWei != 0 geWei == 0) {

System.out.println(lessTen(baiWei) + " " + "hundred" + " "

+ "and" + " " + moreThanTen(shiWei * 10 + geWei));

} else {

if (shiWei == 1 geWei != 0) {

System.out.println(lessTen(baiWei) + " " + "hundred" + " "

+ "and" + " " + moreThanTen(shiWei * 10 + geWei));

} else {

System.out.println(lessTen(baiWei) + " " + "hundred" + " "

+ "and" + " " + moreThanTen(shiWei * 10) + "-"

+ lessTen(geWei));

}

}

}

}

public static void inputNumber() {

System.out.print("请输入第一个数字(数字必须小于等于20)的单词:");

tempNumA = new Scanner(System.in).next();

judgeA = number(tempNumA);

System.out.print("请输入第二个数字(数字必须小于等于20)的单词:");

tempNumB = new Scanner(System.in).next();

judgeB = number(tempNumB);

if (judgeA == -1 || judgeB == -1) {

System.out.println("你输入的数字单词不正确,请重新输入!");

System.out.println(" ");

inputNumber();

}

}

public static void main(String[] args) {

inputNumber();

System.out.print(tempNumA + " multiply " + tempNumB + " equals ");

outputReult(number(tempNumA) * number(tempNumB));

}

}

JAVA程序用netbeans写一个计算器的程序,代码大概一百多行

以前写的,很乱。仅供参考

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.util.ArrayList;

public class T implements ActionListener{

private int append = 1;

JTextField jtf = new JTextField(10);

String s1="";

String s2="";

int n=0;

ArrayListString aList1=new ArrayListString();

ArrayListString aList2=new ArrayListString();

public T(){

JFrame jf = new JFrame("计算器");

jf.add(jtf,BorderLayout.NORTH);

jf.setSize(200,100 );

JPanel jp = new JPanel();

GridLayout gl = new GridLayout(5,4);

jp.setLayout(gl);

String[] lab = {"BackS","CE","C","+",

"7","8","9","-","4","5","6","*",

"1","2","3","/","0",".","+/-","="};

for(int i=0;ilab.length;i++){

JButton jb = new JButton(lab[i]);

jp.add(jb);

jb.addActionListener(this);

}

jf.add(jp);

jf.setSize(300,300);

jtf.setEditable(false);

jf.setResizable(false);

jf.setLocation(300, 200);jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

String num1 = "0";

String operator = "+";

public void actionPerformed(ActionEvent ae) {

String comm = ae.getActionCommand();

if("0123456789".indexOf(comm)!=-1){

if(append==1){

n=0;

s2=s2+comm;

s1=s1+comm;

}

else{

aList1.clear();

append=1;

s2=s2+comm;

s1=comm;

}

jtf.setText(s1);

}else if("+-*/".indexOf(comm)!=-1){

n++;

if(n==1){

s1=s1+comm;

aList2.add(comm);

}

if(append==0)

append=1;

if(s2!=""){

aList1.add(s2);

}

s2="";

jtf.setText(s1);

}else if("=".equals(comm)){

aList1.add(s2);

double d1;

double d2;

int i=0;

while(aList2.contains("*")||aList2.contains("/")){

int flag=0;

if(aList2.size()0"*".equals(aList2.get(i))){

d1 = Double.parseDouble(aList1.get(i));

d2 = Double.parseDouble(aList1.get(i+1));

d1=d1*d2;

aList1.remove(i);

System.out.println("RE:"+aList1);

aList1.set(i,d1+"");

System.out.println("Set:"+aList1);

aList2.remove(i);

System.out.println(aList2);

flag++;

System.out.println("*"+i);

}

if(i==aList2.size())

i--;

if(aList2.size()0"/".equals(aList2.get(i))){

System.out.println("/"+i);

d1 = Double.parseDouble(aList1.get(i));

d2 = Double.parseDouble(aList1.get(i+1));

d1=d1/d2;

aList1.remove(i);

aList1.set(i,d1+"");

aList2.remove(i);

flag++;

System.out.println("/"+aList2);

}

if(flag==0)

i++;

}

while(aList2.contains("+")||aList2.contains("-")){

i=0;

if("+".equals(aList2.get(i))){

d1 = Double.parseDouble(aList1.get(i));

d2 = Double.parseDouble(aList1.get(i+1));

d1=d1+d2;

aList1.remove(i);

System.out.println("Re"+aList1);

aList1.set(i,d1+"");

System.out.println("Set:"+aList1);

aList2.remove(i);

System.out.println("alist2:"+aList2);

System.out.println(aList2.size());

}

if(aList2.size()0"-".equals(aList2.get(i))){

d1 = Double.parseDouble(aList1.get(i));

d2 = Double.parseDouble(aList1.get(i+1));

d1=d1-d2;

aList1.remove(i);

aList1.set(i,d1+"");

aList2.remove(i);

}

}

s1=s1+comm+aList1.get(aList1.size()-1);

jtf.setText(s1);

s2="";

s1=aList1.get(aList1.size()-1);

append = 0;

}else if(".".equals(comm)){

String temp=s2;

if(temp.indexOf(".")==-1){

s1=s1+comm;

jtf.setText(s1);

s2=s2+comm;

append = 1;

}

}else if("+/-".equals(comm)){

String temp = jtf.getText();

if(temp.startsWith("-")){

jtf.setText(temp.substring(1));

}else{

jtf.setText("-"+temp);

}

}else if("CE".equals(comm)||"C".equals(comm)){

jtf.setText("0");

s2="";

s1="";

append = 0;

}else if("BackS".equals(comm)){

String temp = s1;

if(temp.length()0){

if(s1.endsWith("+")||s1.endsWith("-")||s1.endsWith("*")||s1.endsWith("/")){

System.out.print("不可再移除!");

return ;

}

jtf.setText(temp.substring(0,

temp.length()-1));

s1=s1.substring(0, s1.length()-1);

s2=s2.substring(0,s2.length()-1);

}

else{

System.out.println("没有可以移除的!");

}

}

}

public static void main(String[] args) {

new T();

}

}

求个简单点的Java程序 100行左右。 需要解释。

贪吃蛇游戏 望采纳

import java.awt.Button;

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.Point;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.util.*;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

public class Snake extends JFrame implements KeyListener{

int Count=0;

Button[][] grid = new Button[20][20];

ArrayListPoint snake_list=new ArrayListPoint();

Point bean=new Point(-1,-1);//保存随机豆子【坐标】

int Direction = 1; //方向标志 1:上 2:下 3:左 4:右

//构造方法

public Snake()

{

//窗体初始化

this.setBounds(400,300,390,395);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GridLayout f=new GridLayout(20,20);

this.getContentPane().setBackground(Color.gray);

this.setLayout(f);

//初始化20*20个按钮

for(int i=0;i20;i++)

for(int j=0;j20;j++)

{

grid[i][j]=new Button();

this.add(grid[i][j]);

grid[i][j].setVisible(false);

grid[i][j].addKeyListener(this);

grid[i][j].setBackground(Color.blue);

}

//蛇体初始化

grid[10][10].setVisible(true);

grid[11][10].setVisible(true);

grid[12][10].setVisible(true);

grid[13][10].setVisible(true);

grid[14][10].setVisible(true);

//在动态数组中保存蛇体按钮坐标【行列】信息

snake_list.add(new Point(10,10));

snake_list.add(new Point(11,10));

snake_list.add(new Point(12,10));

snake_list.add(new Point(13,10));

snake_list.add(new Point(14,10));

this.rand_bean();

this.setTitle("总分:0");

this.setVisible(true);

}

//该方法随机一个豆子,且不在蛇体上,并使豆子可见

public void rand_bean(){

Random rd=new Random();

do{

bean.x=rd.nextInt(20);//行

bean.y=rd.nextInt(20);//列

}while(snake_list.contains(bean));

grid[bean.x][bean.y].setVisible(true);

grid[bean.x][bean.y].setBackground(Color.red);

}

//判断拟增蛇头是否与自身有碰撞

public boolean is_cross(Point p){

boolean Flag=false;

for(int i=0;isnake_list.size();i++){

if(p.equals(snake_list.get(i) )){

Flag=true;break;

}

}

return Flag;

}

//判断蛇即将前进位置是否有豆子,有返回true,无返回false

public boolean isHaveBean(){

boolean Flag=false;

int x=snake_list.get(0).x;

int y=snake_list.get(0).y;

Point p=null;

if(Direction==1)p=new Point(x-1,y);

if(Direction==2)p=new Point(x+1,y);

if(Direction==3)p=new Point(x,y-1);

if(Direction==4)p=new Point(x,y+1);

if(bean.equals(p))Flag=true;

return Flag;

}

//前进一格

public void snake_move(){

if(isHaveBean()==true){//////////////有豆子吃

Point p=new Point(bean.x,bean.y);//【很重要,保证吃掉的是豆子的复制对象】

snake_list.add(0,p); //吃豆子

grid[p.x][p.y].setBackground(Color.blue);

this.Count++;

this.setTitle("总分:"+Count);

this.rand_bean(); //再产生一个豆子

}else{///////////////////无豆子吃

//取原蛇头坐标

int x=snake_list.get(0).x;

int y=snake_list.get(0).y;

//根据蛇头坐标推算出拟新增蛇头坐标

Point p=null;

if(Direction==1)p=new Point(x-1,y);//计算出向上的新坐标

if(Direction==2)p=new Point(x+1,y);//计算出向下的新坐标

if(Direction==3)p=new Point(x,y-1);//计算出向左的新坐标

if(Direction==4)p=new Point(x,y+1);//计算出向右的新坐标

//若拟新增蛇头碰壁,或缠绕则游戏结束

if(p.x0||p.x19|| p.y0||p.y19||is_cross(p)==true){

JOptionPane.showMessageDialog(null, "游戏结束!");

System.exit(0);

}

//向蛇体增加新的蛇头坐标,并使新蛇头可见

snake_list.add(0,p);

grid[p.x][p.y].setVisible(true);

//删除原蛇尾坐标,使蛇尾不可见

int x1=snake_list.get(snake_list.size()-1).x;

int y1=snake_list.get(snake_list.size()-1).y;

grid[x1][y1].setVisible(false);

snake_list.remove(snake_list.size()-1);

}

}

@Override

public void keyPressed(KeyEvent e) {

if(e.getKeyCode()==KeyEvent.VK_UP Direction!=2) Direction=1;

if(e.getKeyCode()==KeyEvent.VK_DOWN Direction!=1) Direction=2;

if(e.getKeyCode()==KeyEvent.VK_LEFT Direction!=4) Direction=3;

if(e.getKeyCode()==KeyEvent.VK_RIGHT Direction!=3) Direction=4;

}

@Override

public void keyReleased(KeyEvent e) { }

@Override

public void keyTyped(KeyEvent e) { }

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

Snake win=new Snake();

while(true){

win.snake_move();

Thread.sleep(300);

}

}

}


当前题目:java一百行算法题代码,编程100题
文章URL:http://cdweb.net/article/hoigoe.html