您的位置:

简单的java代码,java初学者经典代码

本文目录一览:

一个简单的Java程序代码?

package com.zpp;public class Charge {

public static void main(String [] args) {

if(args.length ==0) {

System.out.println("parameter error!");

System.out.println("java com.zpp.Charge [int]");

return;

}

int min = Integer.parseInt(args[0]);

double money = 0.0;

if (min = 0) {

money =0.0;

System.out.println("not money");

} else if (min = 60) {

money = 2.0;

} else {

money = 2.0 + (min - 60) * 0.01;

}

System.out.println("please pay: " + money);

}

} 编译:javac -d . Charge.java运行:java com.zpp.Charge 111

求简单JAVA程序代码,能操作的,简单的计算器有计算功能+-*/就行越简单越好!!!急!!!

public static void main(String[] args) {

String st = "";

计算器 j = new 计算器();

boolean t = true;

while (t) {

System.out.print("请输入计算代码:");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

try {

st = br.readLine();

} catch (IOException ex) {

Logger.getLogger(计算器.class.getName()).log(Level.SEVERE, null, ex);

}

if (st.equals("end")) {

t = false;

} else if (!st.equals("")) {

j.处理(st);

}

}

}

public void 处理(String st) {

if (st.indexOf("+") != -1) {

加(符号处理(st, "+"));

} else if (st.indexOf("-") != -1) {

减(符号处理(st, "-"));

} else if (st.indexOf("*") != -1) {

乘(符号处理(st, "*"));

} else if (st.indexOf("/") != -1) {

除(符号处理(st, "/"));

}

}

public int[] 符号处理(String st, String s) {

int[] i = new int[2];

i[0] = Integer.parseInt(st.substring(0, st.indexOf(s)));

i[1] = Integer.parseInt(st.substring(st.indexOf(s) + 1));

return i;

}

public void 加(int x[]) {

System.out.println(x[0] + "+" + x[1] + "=" + (x[0] + x[1]));

}

public void 减(int[] x) {

System.out.println(x[0] + "-" + x[1] + "=" + (x[0] - x[1]));

}

public void 乘(int[] x) {

System.out.println(x[0] + "*" + x[1] + "=" + x[0] * x[1]);

}

public void 除(int x[]) {

if (x[1] == 0) {

System.out.print("除数不能为零");

return;

}

System.out.println(x[0] + "/" + x[1] + "=" + x[0] / x[1]);

}

可以运行:输入示例:55+62

java简单代码

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String str = "";

System.out.println("请输入指令:");

while(true){

str = sc.next();

if(str.equals("add")){

System.out.println("执行两数相加");

} else if(str.equals("sub")){

System.out.println("执行两数相减");

} else if(str.equals("help")){

System.out.println("add:两数相加  sub:两数相减");

} else {

System.out.println("请输入正确指令:");

}

}

}

你看看这段行不行

求一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像素,这

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