本文目錄一覽:
java新手,求完整的源代碼
//都是從新手過來的,以下代碼供參考
//1.
public class BankAccount {
private static String acctnum;
private static double money;
private static void showAcct() {
System.out.println(“賬號為: ” + acctnum);
}
private static void showMoney() {
System.out.println(“餘額為: ” + money);
}
public BankAccount(String acc, double m) {
this.acctnum = acc;
this.money = m;
}
public static void main(String[] args) {
BankAccount ba = new BankAccount(“626600018888”, 5000.00);
ba.showAcct();
ba.showMoney();
}
}
//2.
public class Triangle {
private static float a;
private static float b;
private static float c;
public Triangle(float a, float b, float c) {
this.a = a;
this.b = b;
this.c = c;
}
public static boolean judgeTriangle(float a, float b, float c) {
if ((a Math.abs(b – c) a b + c)
(b Math.abs(a – c) b a + c)
(c Math.abs(a – b) c a + b))
return true;
else
return false;
}
public float getCircumference() {
return this.a + this.b + this.c;
}
}
//3.
public class TestTriangle {
public static void main(String[] args) {
Triangle t = new Triangle(5.3f,7.8f,9.3f);
if(t.judgeTriangle(5.3f,7.8f,9.3f)){
System.out.print(“能夠成三角形,周長為: “);
System.out.printf(“%9.2f”,t.getCircumference());}
else
System.out.println(“不能構成三角形”);
}
}
學Java有哪些可以練手的項目
1.各種管理系統
源碼下載(實例一):
jsp開發完整的博研圖書館後台管理系統,不使用框架開發的,太完美了
源碼下載(實例二):
javaWeb圖書館管理系統源碼mysql版本
源碼下載(實例三)
GitHub – uboger/LibraryManager: JAVA GUI 圖書館管理系統
源碼下載(實例四):
java swing開發企業人事管理系統源代碼下載
2.簡易的聊天系統
源碼下載(實例一):
java swing開發網路聊天室群聊系統,基於java多線程socket編程
源碼下載(實例二):
java swing開發大貓聊天室源碼,簡單易懂,適合java swing初學者
源碼下載(實例三):
java websocket開發簡單聊天室系統,實現群聊與一對一單人聊天案例
源碼下載(實例四):
jsp開發簡單聊天室demo-分享
3.實現通訊錄
java通訊錄實現了添加刪除和查找功能
源碼下載(二):
JAVA版通訊錄管理系統課程設計源碼
源碼下載(三):
Java Swing界面.完美設計通訊錄..有需要的下
4.坦克大戰
源碼下載(一):
俄羅斯方塊 JAVA版
源碼下載(二):
GitHub – FieldSoft-HelloClyde/Tetris-Swing: Swing編寫的俄羅斯方塊
源碼下載(三):
java swing開發的俄羅斯方塊遊戲,包含完整代碼+詳細注釋
5.五子棋
源碼下載(一):
Java實踐(十一)——五子棋
源碼下載(二):
java swing開發的五子棋小遊戲源碼
源碼下載(三):
java swing開發單機版五子棋源代碼下載
源碼下載(四):
Java五子棋演算法和代碼
6.中國象棋
源碼下載(一):Java實踐(十二)——中國象棋
7.貪吃蛇
java貪吃蛇源代碼 、 java貪吃蛇源代碼
以上是總結出來的簡單的練手項目,希望對你有幫助
求JAVA入門小程序源代碼
mport java.util.*;
public class HuiWen
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println(“please input a String:”);
String st=in.nextLine();
String s=st.toLowerCase();
int i=0;
int j=s.length()-1;
boolean t=true;
char first=s.charAt(i);
char last=s.charAt(j);
for(;ij;)
{
while(!(first=’a’first=’z’))
{
i++;
first=s.charAt(i);
}
while(!(last=’a’last=’z’))
{
j–;
last=s.charAt(i);
}
if(i=j)
break;
if(first==last)
{
i++;
j–;
first=s.charAt(i);
last=s.charAt(j);
}
else
{
t=false;
break;
}
}
if(t)
System.out.println(st+”是迴文串”);
else
System.out.println(st+”不是迴文串”);
}
}
//判斷是否為迴文字串
//什麼實用的程序,說具體點!你要計算器的程序嗎?
//下面是計算器的程序,把分拿來吧!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator
{
public static void main(String[] args)
{
CalculatorFrame frame=new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle(“Calculator”);
CalculatorPanel panel=new CalculatorPanel();
add(panel);
pack();
}
}
class CalculatorPanel extends Panel
{
public CalculatorPanel()
{
setLayout(new BorderLayout());
result=0;
lastCommand=”=”;
start=true;
flag=true;
display=new JButton(“0”);
display.setEnabled(false);
add(display,BorderLayout.NORTH);
ActionListener insert=new InsertAction();
ActionListener command=new CommandAction();
panel=new JPanel();
panel.setLayout(new GridLayout(4,5));
addButton(“7”,insert);
addButton(“8”,insert);
addButton(“9”,insert);
addButton(“/”,command);
addButton(“CE”,command);
addButton(“4”,insert);
addButton(“5”,insert);
addButton(“6”,insert);
addButton(“*”,command);
addButton(“Backspace”,command);
addButton(“1”,insert);
addButton(“2”,insert);
addButton(“3”,insert);
addButton(“-“,command);
addButton(“sqrt”,command);
addButton(“0”,insert);
addButton(“.”,insert);
addButton(“=”,command);
addButton(“+”,command);
addButton(“1/x”,command);
add(panel,BorderLayout.CENTER);
}
private void addButton(String label,ActionListener listener)
{
JButton button=new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input=event.getActionCommand();
if(startflag)
{
display.setText(“”);
start=false;
}
if(flag)
display.setText(display.getText()+input);
}
}
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command=event.getActionCommand();
if(command.equals(“CE”))
{
display.setText(“0”);
start=true;
flag=true;
command=”=”;
}
else
if(startflag)
{
if(command.equals(“-“))
{
display.setText(command);
start=false;
}
else
if((command.equals(“1/x”)||command.equals(“sqrt”))flag)
calculate(Double.parseDouble(display.getText()),command);
else
if(flag)
lastCommand=command;
}
else
{
if(command.equals(“Backspace”)flag)
{
String s=display.getText();
char[] s1=s.toCharArray();
if(s.length()=2)
{
String s2=new String(s1,0,s.length()-1);
display.setText(s2);
}
else
{
display.setText(“0”);
start=true;
}
}
else if(flag)
{
calculate(Double.parseDouble(display.getText()),command);
lastCommand=command;
start=true;
}
}
}
}
public void calculate(double x,String command)
{
if(lastCommand.equals(“+”)) result+=x;
else if(lastCommand.equals(“-“)) result-=x;
else if(lastCommand.equals(“/”))
{
if(x!=0)
result/=x;
else
{
display.setText(“除數不能為0”);
start=false;
flag=false;
return;
}
}
else if(lastCommand.equals(“*”)) result*=x;
else if(command.equals(“1/x”))
{
if(x!=0)
result=1/x;
else
{
display.setText(“除數不能為0”);
start=false;
flag=false;
return;
}
}
else if(command.equals(“sqrt”))
{
if(x=0)
result=Math.sqrt(x);
else
{
display.setText(“函數輸入無效”);
start=false;
flag=false;
return;
}
}
else if(lastCommand.equals(“=”)) result=x;
display.setText(“”+result);
}
private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
private boolean flag;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/248678.html