簡單java代碼
1. 一個簡單的java程序代碼
public double GetCost(int minutes)
{
//整數時間所花的費用
int aa = minutes / 60; //未滿1小時處理
if (minutes < 60)
return 2;
//超出小時部分
int bb = minutes % 60; //其實你還有必要做一些其他處理。比如說超過30分鍾了該怎麼樣算等等...... return aa * 2 + bb * 0.01d;
}
2. 簡單JAVA代碼
輸出錯了!!!
第一個類的構造方法,請看
public tt(int b) {
b=x;
}
請問,你new出來tt這個類了,x有值了吧?
也就是說你在這里把x賦給了b,但是在第二個方法又無法用到b,也就是說,這個方法毫無用處
請將 b 和x的位置換換,同時,x為成員變數,不需要初始化給值的,是靠你傳參數的。
第二,請看
public int cc(int b) {
b=x*2;
return b;
}
你可以完全把這個方法里的傳參去掉,應為你的x是成員變數,改為
public int cc() {
return x*2;
}
然後在另外一個類里new出來tt,請看,我幫你修改了
Scanner a =new Scanner(System.in);
int b = a.nextInt();
tt cd =new tt(b);
int sd= cd.cc();
System.out.println(sb);
其實還可以改,
Scanner a =new Scanner(System.in);
tt cd =new tt(a.nextInt());
System.out.println(cd.cc());
3. 求一個簡單的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 = -464252885453878L;
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();
}
}
}
4. 求一段簡單JAVA代碼 非常簡單
importjava.util.LinkedList;
importjava.util.List;
importjava.util.Random;
publicclassGroup{
publicstaticvoidmain(String[]args){
List<Integer>l=newLinkedList<Integer>();
int[][]group=newint[4][7];
Randomr=newRandom();
for(inti=0;i<31;i++)
l.add(newInteger(i+1));
for(inti=0;i<4;i++){
for(intj=0;j<7;j++){
intk=r.nextInt(l.size());
group[i][j]=l.get(k);
l.remove(k);
}
}
for(inti=0;i<4;i++){
System.out.print("Group"+(i+1)+":");
for(intj=0;j<7;j++){
System.out.print(group[i][j]+"");
}
System.out.println();
}
//剩下分組
Stringgroups="";
for(Integer_tem:l){
groups+=_tem+"";
}
System.out.print("Group"+"5"+":"+groups);
}
}
5. JAVA的一段簡單代碼
class Square {
int length;
Square(int len) {
length = len;
}
void getPerimeter() {
System.out.println("正方形的周長為" + (4 * length));
}
}
class Rectangle extends Square {
int width;
Rectangle(int len, int wid) {
super(len);
width = wid;
}
void getPerimeter() {
System.out.println("矩形的周長為" + (2 * (length + width)));
}
}
public class Calulate{
public static void main(String args[]) {
Square sqObj = new Square(15);
sqObj.getPerimeter();
Square rectObj =new Rectangle(10,33);
rectObj.getPerimeter();
}
}
子類重寫了父類方法!!!
創建了兩個對象~rectObj.getPerimeter()列印出來是子類重寫的~
6. 求一個簡單又有趣的JAVA小游戲代碼
具體如下:
連連看的小源碼
package Lianliankan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戲按鈕數組
JButton exitButton,resetButton,newlyButton; //退出,重列,重新開始按鈕
JLabel fractionLable=new JLabel("0"); //分數標簽
JButton firstButton,secondButton; //
分別記錄兩次被選中的按鈕
int grid[][] = new int[8][7];//儲存游戲按鈕位置
static boolean pressInformation=false; //判斷是否有按鈕被選中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戲按鈕的位置坐標
int i,j,k,n;//消除方法控制
代碼(code)是程序員用開發工具所支持的語言寫出來的源文件,是一組由字元、符號或信號碼元以離散形式表示信息的明確的規則體系。
對於字元和Unicode數據的位模式的定義,此模式代表特定字母、數字或符號(例如 0x20 代表一個空格,而 0x74 代表字元「t」)。一些數據類型每個字元使用一個位元組;每個位元組可以具有 256 個不同的位模式中的一個模式。
在計算機中,字元由不同的位模式(ON 或 OFF)表示。每個位元組有 8 位,這 8 位可以有 256 種不同的 ON 和 OFF 組合模式。對於使用 1 個位元組存儲每個字元的程序,通過給每個位模式指派字元可表示最多 256 個不同的字元。2 個位元組有 16 位,這 16 位可以有 65,536 種唯一的 ON 和 OFF 組合模式。使用 2 個位元組表示每個字元的程序可表示最多 65,536 個字元。
單位元組代碼頁是字元定義,這些字元映射到每個位元組可能有的 256 種位模式中的每一種。代碼頁定義大小寫字元、數字、符號以及 !、@、#、% 等特殊字元的位模式。每種歐洲語言(如德語和西班牙語)都有各自的單位元組代碼頁。
雖然用於表示 A 到 Z 拉丁字母表字元的位模式在所有的代碼頁中都相同,但用於表示重音字元(如"é"和"á")的位模式在不同的代碼頁中卻不同。如果在運行不同代碼頁的計算機間交換數據,必須將所有字元數據由發送計算機的代碼頁轉換為接收計算機的代碼頁。如果源數據中的擴展字元在接收計算機的代碼頁中未定義,那麼數據將丟失。
如果某個資料庫為來自許多不同國家的客戶端提供服務,則很難為該資料庫選擇這樣一種代碼頁,使其包括所有客戶端計算機所需的全部擴展字元。而且,在代碼頁間不停地轉換需要花費大量的處理時間。
7. 簡單的Java代碼
Set keys = map.keySet(); //keys是map的所有鍵的集合,即之前放進去的"key1"--"key4"
for(Object key : keys){ //循環所有key
System.out.println(map.get(key)); //列印map中鍵對應的值,即"value1"--"value4"
}
8. java簡單的代碼
原來File fDir=new File("D://");是這樣的File fDir=new File(File.separator); 我改了 但是還是不行 啊。回答: import java.io.File; public class NewClass7 { public static void main(String[] args) throws Exception{ File fDir=new File("F://");//分隔符 separator String strFile="sg"+File.separator+"DT"+File.separator+"1.txt"; File f=new File(fDir,strFile); f.createNewFile();}}當然,如果按照你這段代碼,你要確保目標盤上得有sg/DT這兩個目錄,否則你就要在代碼里創建這兩個目錄了。補充: 看我修改後的代碼,必須引用java.io.File類,你原來代碼中沒有,還有,你要確保目標盤上得有sg/DT這兩個目錄。追問: 我多問一句。這段代碼 屬於J2EE 吧?回答: 不能這樣說,J2EE是JAVA體系的一部份,是一種JAVA應用開發的技術架構。你這段代碼只是一段JAVA的基礎代碼。
9. java快速排序簡單代碼
.example-btn{color:#fff;background-color:#5cb85c;border-color:#4cae4c}.example-btn:hover{color:#fff;background-color:#47a447;border-color:#398439}.example-btn:active{background-image:none}div.example{width:98%;color:#000;background-color:#f6f4f0;background-color:#d0e69c;background-color:#dcecb5;background-color:#e5eecc;margin:0 0 5px 0;padding:5px;border:1px solid #d4d4d4;background-image:-webkit-linear-gradient(#fff,#e5eecc 100px);background-image:linear-gradient(#fff,#e5eecc 100px)}div.example_code{line-height:1.4em;width:98%;background-color:#fff;padding:5px;border:1px solid #d4d4d4;font-size:110%;font-family:Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;word-break:break-all;word-wrap:break-word}div.example_result{background-color:#fff;padding:4px;border:1px solid #d4d4d4;width:98%}div.code{width:98%;border:1px solid #d4d4d4;background-color:#f6f4f0;color:#444;padding:5px;margin:0}div.code div{font-size:110%}div.code div,div.code p,div.example_code p{font-family:"courier new"}pre{margin:15px auto;font:12px/20px Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;white-space:pre-wrap;word-break:break-all;word-wrap:break-word;border:1px solid #ddd;border-left-width:4px;padding:10px 15px} 排序演算法是《數據結構與演算法》中最基本的演算法之一。排序演算法可以分為內部排序和外部排序,內部排序是數據記錄在內存中進行排序,而外部排序是因排序的數據很大,一次不能容納全部的排序記錄,在排序過程中需要訪問外存。常見的內部排序演算法有:插入排序、希爾排序、選擇排序、冒泡排序、歸並排序、快速排序、堆排序、基數排序等。以下是快速排序演算法:
快速排序是由東尼·霍爾所發展的一種排序演算法。在平均狀況下,排序 n 個項目要 Ο(nlogn) 次比較。在最壞狀況下則需要 Ο(n2) 次比較,但這種狀況並不常見。事實上,快速排序通常明顯比其他 Ο(nlogn) 演算法更快,因為它的內部循環(inner loop)可以在大部分的架構上很有效率地被實現出來。
快速排序使用分治法(Divide and conquer)策略來把一個串列(list)分為兩個子串列(sub-lists)。
快速排序又是一種分而治之思想在排序演算法上的典型應用。本質上來看,快速排序應該算是在冒泡排序基礎上的遞歸分治法。
快速排序的名字起的是簡單粗暴,因為一聽到這個名字你就知道它存在的意義,就是快,而且效率高!它是處理大數據最快的排序演算法之一了。雖然 Worst Case 的時間復雜度達到了 O(n?),但是人家就是優秀,在大多數情況下都比平均時間復雜度為 O(n logn) 的排序演算法表現要更好,可是這是為什麼呢,我也不知道。好在我的強迫症又犯了,查了 N 多資料終於在《演算法藝術與信息學競賽》上找到了滿意的答案:
快速排序的最壞運行情況是 O(n?),比如說順序數列的快排。但它的平攤期望時間是 O(nlogn),且 O(nlogn) 記號中隱含的常數因子很小,比復雜度穩定等於 O(nlogn) 的歸並排序要小很多。所以,對絕大多數順序性較弱的隨機數列而言,快速排序總是優於歸並排序。
1. 演算法步驟
從數列中挑出一個元素,稱為 "基準"(pivot);
重新排序數列,所有元素比基準值小的擺放在基準前面,所有元素比基準值大的擺在基準的後面(相同的數可以到任一邊)。在這個分區退出之後,該基準就處於數列的中間位置。這個稱為分區(partition)操作;
遞歸地(recursive)把小於基準值元素的子數列和大於基準值元素的子數列排序;
2. 動圖演示
代碼實現 JavaScript 實例 function quickSort ( arr , left , right ) {
var len = arr. length ,
partitionIndex ,
left = typeof left != 'number' ? 0 : left ,
right = typeof right != 'number' ? len - 1 : right ;
if ( left