簡單的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像素,這

樣能多彈幾次。這個程序,最後球就在那裡不動了,程序不會自己退出。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/193498.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-01 15:02
下一篇 2024-12-01 15:02

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變數和數…

    編程 2025-04-29
  • Python簡單數學計算

    本文將從多個方面介紹Python的簡單數學計算,包括基礎運算符、函數、庫以及實際應用場景。 一、基礎運算符 Python提供了基礎的算術運算符,包括加(+)、減(-)、乘(*)、除…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • Python初學者指南:第一個Python程序安裝步驟

    在本篇指南中,我們將通過以下方式來詳細講解第一個Python程序安裝步驟: Python的安裝和環境配置 在命令行中編寫和運行第一個Python程序 使用IDE編寫和運行第一個Py…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在著手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29
  • 怎麼寫不影響Python運行的長段代碼

    在Python編程的過程中,我們不可避免地需要編寫一些長段代碼,包括函數、類、複雜的控制語句等等。在編寫這些代碼時,我們需要考慮代碼可讀性、易用性以及對Python運行性能的影響。…

    編程 2025-04-29

發表回復

登錄後才能評論