新聞中心
一個(gè)簡(jiǎn)單的Java程序代碼?
package com.zpp;public class Charge {
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括寶山網(wǎng)站建設(shè)、寶山網(wǎng)站制作、寶山網(wǎng)頁(yè)制作以及寶山網(wǎng)絡(luò)營(yíng)銷(xiāo)策劃等。多年來(lái),我們專(zhuān)注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,寶山網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶(hù)以成都為中心已經(jīng)輻射到寶山省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶(hù)的支持與信任!
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運(yùn)行:java com.zpp.Charge 111
Java程序代碼
import java.awt.*;//計(jì)算器實(shí)例
import java.awt.event.*;
public class calculator
{
public static void main(String args[])
{
MyWindow my=new MyWindow("計(jì)算器");
}
}
class MyWindow extends Frame implements ActionListener
{ StringBuffer m=new StringBuffer();
int p;
TextField tex;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,jia,jian,cheng,chu,deng,dian,qingling,kaifang;
MyWindow(String s)
{
super(s);
//StringBuffer s2=new StringBuffer();
//String s;
tex=new TextField(18);
b0=new Button(" 0 ");
b1=new Button(" 1 ");
b2=new Button(" 2 ");
b3=new Button(" 3 ");
b4=new Button(" 4 ");
b5=new Button(" 5 ");
b6=new Button(" 6 ");
b7=new Button(" 7 ");
b8=new Button(" 8 ");
b9=new Button(" 9 ");
dian=new Button(" . ");
jia=new Button(" + ");
jian=new Button(" - ");
cheng=new Button(" × ");
chu=new Button(" / ");
deng=new Button(" = ");
qingling=new Button(" 清零 ");
kaifang=new Button(" √ ");
setLayout(new FlowLayout());
add(tex);
add(b0);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(dian);
add(jia);
add(jian);
add(cheng);
add(chu);
add(kaifang);
add(qingling);
add(deng);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
jia.addActionListener(this);
jian.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
dian.addActionListener(this);
deng.addActionListener(this);
qingling.addActionListener(this);
kaifang.addActionListener(this);
setBounds(200,200,160,280);
setResizable(false);//不可改變大小
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent ee)
{ System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b0)
{
m=m.append("0");
tex.setText(String.valueOf(m));
}
if(e.getSource()==b1)
{
m=m.append("1"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b2)
{
m=m.append("2"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b3)
{
m=m.append("3"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b4)
{
m=m.append("4"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b5)
{
m=m.append("5"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b6)
{
m=m.append("6"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b7)
{
m=m.append("7"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b8)
{
m=m.append("8"); tex.setText(String.valueOf(m));
}
if(e.getSource()==b9)
{
m=m.append("9"); tex.setText(String.valueOf(m));
}
if(e.getSource()==jia)
{
m=m.append("+"); tex.setText(String.valueOf(m));
}
if(e.getSource()==jian)
{
m=m.append("-"); tex.setText(String.valueOf(m));
}
if(e.getSource()==cheng)
{
m=m.append("*"); tex.setText(String.valueOf(m));
}
if(e.getSource()==chu)
{
m=m.append("/"); tex.setText(String.valueOf(m));
}
if(e.getSource()==dian)
{
m=m.append("."); tex.setText(String.valueOf(m));
}
String mm=String.valueOf(m);
int p1=mm.indexOf("+");
int p2=mm.indexOf("-");
int p3=mm.indexOf("*");
int p4=mm.indexOf("/");
if(p1!=-1)
{
p=p1;
}
else if(p3!=-1)
{
p=p3;
}
else if(p2!=-1)
{
p=p2;
}
else if(p4!=-1)
{
p=p4;
}
if(e.getSource()==deng)
{
String m1=mm.substring(0,p);
String m2=mm.substring(p+1);
String ch=mm.substring(p,p+1);
//System.out.println(m1);
//System.out.println(m2);
//System.out.println(ch);
if(ch.equals("+"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1+n2;
String su=String.valueOf(sum);
tex.setText(su);
}
if(ch.equals("-"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1-n2;
String su=String.valueOf(sum);
tex.setText(su);
}
if(ch.equals("*"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1*n2;
String su=String.valueOf(sum);
tex.setText(su);
}
if(ch.equals("/"))
{
float n1=Float.parseFloat(m1);
float n2=Float.parseFloat(m2);
float sum=n1/n2;
String su=String.valueOf(sum);
tex.setText(su);
}
}
if(e.getSource()==qingling)
{StringBuffer kk=new StringBuffer();
m=kk;
tex.setText("0");
// System.out.println(mm);
}
if(e.getSource()==kaifang)
{
String t=tex.getText();
float num=Float.parseFloat(t);
double nub=Math.sqrt(num);
tex.setText(String.valueOf(nub));
}
}
}
Java的基本格式
基本格式:修飾符 class 類(lèi)名(程序代碼)
意義:Java中的程序代碼都必須放在一個(gè)類(lèi)中,對(duì)于類(lèi)初學(xué)者可以簡(jiǎn)單地把它理解為一個(gè)java程序;類(lèi)需要使用class作為關(guān)鍵字定義;而在class的前面可以有一些修飾符。
擴(kuò)展資料
編寫(xiě)java時(shí),特別需要注意的幾個(gè)書(shū)寫(xiě)格式常見(jiàn)的錯(cuò)誤:
1.java中的程序代碼中功能執(zhí)行語(yǔ)句的最后都必須用(;)結(jié)束。
這里需要注意的是,在程序中不要將英文的分號(hào)(;)誤寫(xiě)成中文的分號(hào)(;) ,如果寫(xiě)成了中文的分號(hào),編譯器會(huì)報(bào)告“Invalid character”(無(wú)效字符)這樣的錯(cuò)誤信息。
2.Java語(yǔ)言是嚴(yán)格區(qū)分大小寫(xiě)的。在定義類(lèi)時(shí),不能將class寫(xiě)成Class,否則編譯會(huì)報(bào)錯(cuò)。程序中定義一個(gè)computer的同時(shí),還可以定義一個(gè)Computer,computer和Computer是兩個(gè)全完不同的符號(hào),在使用的時(shí)候需要注意。
3. 在編寫(xiě)java代碼的時(shí)候?yàn)榱吮阌陂喿x,通常會(huì)使用一種良好的格式進(jìn)行排版,但這并不是必須的,我們也可以在兩個(gè)單詞或者符號(hào)之間任意換行。
參考資料:百度百科-java
簡(jiǎn)單的JAVA的程序代碼的意思?
意思是把x和y轉(zhuǎn)換成字符串輸出。
當(dāng)x=0,y=0時(shí),輸出
0 0(空格)
當(dāng)x=1,y=0時(shí),輸出
0 0 0 1(空格)
所以,最后輸出就是:
0 0 0 1 0 2 0 3 0 4(最后還有一個(gè)空格)
用java編寫(xiě)下圖的程序,詳細(xì)程序代碼
我使用eclipse寫(xiě)的這個(gè)練習(xí),首先給你截個(gè)圖,抹掉的地方不是這個(gè)題的范疇,光標(biāo)擋住的地方是ModelScore
然后,先是ModelScore代碼:
package?Models;
public?class?ModelScore?{
private?double?mathScore;
private?double?englishScore;
private?double?javaScore;
private?static?final?int?type_Math?=?0,?type_English?=?1,?type_Java?=?2;
public?ModelScore(){
}
public?int?getNumberOfField(){
return?3;
}
public?ModelScore(double?mathScore,?double?englishScore,?double?javaScore){
this.mathScore?=?mathScore;
this.englishScore?=?englishScore;
this.javaScore?=?javaScore;
}
public?double?getMathScore(){
return?this.mathScore;
}
public?double?getEnglishScore(){
return?this.englishScore;
}
public?double?getJavaScore(){
return?this.javaScore;
}
public?void?setMathScore(double?mathScore){
this.mathScore?=?mathScore;
}
public?void?setEnglishScore(double?englishScore){
this.englishScore?=?englishScore;
}
public?void?setJavaScore(double?javaScore){
this.javaScore?=?javaScore;
}
public?double?getAverageScore(){
return?(double)Math.round((this.mathScore?+?this.englishScore?+?this.javaScore)?/?3?*?100)?/?100;
}
public?String?toString(){
return?"Scores:[Math:?"?+?getMathScore()?+?
",?English:?"?+?getEnglishScore()?+?",?Java:?"?+?getJavaScore()?+?
",?AverageScore:?"?+?getAverageScore()?+?"]";
}
public?ModelScore?clone(){
ModelScore?score?=?new?ModelScore();
score.setMathScore(this.getMathScore());
score.setEnglishScore(this.getEnglishScore());
score.setJavaScore(this.getJavaScore());
return?score;
}
public?double?getScore(int?type){
if(type?==?type_Math){
return?mathScore;
}
if(type?==?type_English){
return?englishScore;
}
if(type?==?type_Java){
return?javaScore;
}
return?0;
}
}
然后是ModelStudent代碼:
package?Models;
public?class?ModelStudent?{
private?String?studentID;
private?String?studentName;
private?ModelScore?score;
public?ModelStudent(){
}
public?ModelStudent(String?studentID,?String?studentName,?ModelScore?score){
this.studentID?=?studentID;
this.studentName?=?studentName;
this.score?=?score.clone();
}
public?String?getStudentID(){
return?this.studentID;
}
public?String?getStudentName(){
return?this.studentName;
}
public?ModelScore?getStudentScore(){
return?this.score.clone();
}
public?void?setStudentID(String?studentID){
this.studentID?=?studentID;
}
public?void?setStudentName(String?studentName){
this.studentName?=?studentName;
}
public?void?setStudentScore(ModelScore?score){
this.score?=?score.clone();
}
public?double?getAverageScore(){
return?score.getAverageScore();
}
public?ModelStudent?clone(){
ModelStudent?student?=?new?ModelStudent();
student.setStudentID(studentID);
student.setStudentName(studentName);
student.setStudentScore(score.clone());
return?student;
}
public?String?toString(){
return?"Student:[?ID:?"?+?getStudentID()?+?",?Name:?"
+?getStudentName()?+?",?"?+?getStudentScore()?+?"]";
}
public?String?getNameAndEverayScore(){
return?"Name:?"?+?studentName?+?",?average?score:?"?+?getAverageScore();
}
}
最后是主函數(shù)所在的類(lèi)StudentScoreTester代碼:
import?java.util.ArrayList;
import?java.util.Scanner;
import?Models.ModelScore;
import?Models.ModelStudent;
public?class?StudentScoreTester?{
static?final?int?type_Math?=?0,?type_English?=?1,?type_Java?=?2?;
public?static?void?main(String[]?args)?throws?NumberFormatException{
Scanner?input?=?new?Scanner(System.in);
String?studentID?=?"?",?studentName,?searchedStudentName;
double?studentMathScore,?studentEnglishScore,?studentJavaScore;
ModelScore?studentScore;
ModelStudent?student;
ArrayListModelStudent?students?=?new?ArrayListModelStudent();
ArrayListModelStudent?studentsWithMaxMath,?studentsWithMinMath;
ArrayListModelStudent?studentsWithMaxEnglish,?studentsWithMinEnglish;
ArrayListModelStudent?studentsWithMaxJava,?studentsWithMinJava;
ArrayListModelStudent?searchedStudents;
double[]?averageScores;
double?averageScoreForMath,?averageScoreForEnglish,?averageScoreForJava;
double?maxScoreForMath,?maxScoreForEnglish,?maxScoreForJava;
double?minScoreForMath,?minScoreForEnglish,?minScoreForJava;
while(!studentID.equalsIgnoreCase("end")){
System.out.print("輸入若干學(xué)生的學(xué)號(hào),姓名,以及三科成績(jī)(數(shù)學(xué),英語(yǔ),Java),?以輸入end作為結(jié)束輸入:");
studentID?=?input.next();
if(studentID.equalsIgnoreCase("end")){
break;
}
studentName?=?input.next();
studentMathScore?=?Double.valueOf(input.next());
studentEnglishScore?=?Double.valueOf(input.next());
studentJavaScore?=?Double.valueOf(input.next());
studentScore?=?new?ModelScore(studentMathScore,?studentEnglishScore,?studentJavaScore);
student?=?new?ModelStudent(studentID,?studentName,?studentScore);
students.add(student);
}
System.out.println("*******************************");
System.out.println("計(jì)算出所有學(xué)生平均成績(jī),并以降序顯示......");
sortByAverageScore(students);
for(ModelStudent?everyStudent:?students){
System.out.println(everyStudent.getNameAndEverayScore());
}
System.out.println("*******************************");
System.out.println("全組每科平均成績(jī)......");
averageScores?=?calculateAverageScore(students);
averageScoreForMath?=?averageScores[type_Math];
averageScoreForEnglish?=?averageScores[type_English];
averageScoreForJava?=?averageScores[type_Java];
System.out.println("數(shù)學(xué):?"?+?averageScoreForMath);
System.out.println("英語(yǔ):?"?+?averageScoreForEnglish);
System.out.println("Java:?"?+?averageScoreForJava);
System.out.println("*******************************");
System.out.println("每科最好以及最差成績(jī)的學(xué)生......");
studentsWithMaxMath?=?findStudentWithMaxScore(students,?type_Math);
studentsWithMinMath?=?findStudentWithMinScore(students,?type_Math);
studentsWithMaxEnglish?=?findStudentWithMaxScore(students,?type_English);
studentsWithMinEnglish?=?findStudentWithMinScore(students,?type_English);
studentsWithMaxJava?=?findStudentWithMaxScore(students,?type_Java);
studentsWithMinJava?=?findStudentWithMinScore(students,?type_Java);
System.out.println("??????#########################");
System.out.println("數(shù)學(xué)最高成績(jī)......");
for(ModelStudent?studentWithMaxMath:?studentsWithMaxMath){
System.out.println(studentWithMaxMath.getStudentName()?+?",?"?+?studentWithMaxMath.getStudentScore().getScore(type_Math));
}
System.out.println("\n數(shù)學(xué)最低成績(jī)......");
for(ModelStudent?studentWithMinMath:?studentsWithMinMath){
System.out.println(studentWithMinMath.getStudentName()?+?",?"?+?studentWithMinMath.getStudentScore().getScore(type_Math));
}
System.out.println("??????#########################");
System.out.println("英語(yǔ)最高成績(jī)......");
for(ModelStudent?studentWithMaxEnglish:?studentsWithMaxEnglish){
System.out.println(studentWithMaxEnglish.getStudentName()?+?",?"?+?studentWithMaxEnglish.getStudentScore().getScore(type_English));
}
System.out.println("\n英語(yǔ)最低成績(jī)......");
for(ModelStudent?studentWithMinEnglish:?studentsWithMinEnglish){
System.out.println(studentWithMinEnglish.getStudentName()?+?",?"?+?studentWithMinEnglish.getStudentScore().getScore(type_English));
}
System.out.println("??????#########################");
System.out.println("Java最高成績(jī)......");
for(ModelStudent?studentWithMaxJava:?studentsWithMaxJava){
System.out.println(studentWithMaxJava.getStudentName()?+?",?"?+?studentWithMaxJava.getStudentScore().getScore(type_Java));
}
System.out.println("\nJava最低成績(jī)......");
for(ModelStudent?studentWithMinJava:?studentsWithMinJava){
System.out.println(studentWithMinJava.getStudentName()?+?",?"?+?studentWithMinJava.getStudentScore().getScore(type_Java));
}
System.out.println("*******************************");
System.out.print("輸入要查找的學(xué)生姓名:?");
input.nextLine();
searchedStudentName?=?input.nextLine();
searchedStudents?=?searchStudentsByName(students,?searchedStudentName);
System.out.println("查找結(jié)果是......");
if(searchedStudents.size()?!=?0){
for(ModelStudent?everyStudent:?searchedStudents){
System.out.println(everyStudent);
}
}
else{
System.out.println("查無(wú)此學(xué)生");
}
}
public?static?void?sortByAverageScore(ArrayListModelStudent?students){
for(int?i?=?0;?i??students.size()?-?1;?i++){
for(int?j?=?i;?j??students.size();?j++){
if(students.get(i).getAverageScore()??students.get(j).getAverageScore()){
ModelStudent?tempStudent?=?students.get(i);
students.set(i,?students.get(j));
students.set(j,?tempStudent);
}
}
}
}
public?static?double[]?calculateAverageScore(ArrayListModelStudent?students){
double[]?averageScore?=?new?double[students.get(0).getStudentScore().getNumberOfField()];
for(int?i?=?0;?i??students.size();?i++){
ModelStudent?currentStudent?=?students.get(i);
averageScore[type_Math]?+=?currentStudent.getStudentScore().getMathScore();
averageScore[type_English]?+=?currentStudent.getStudentScore().getEnglishScore();
averageScore[type_Java]?+=?currentStudent.getStudentScore().getJavaScore();
}
averageScore[type_Math]?=?(double)Math.round(averageScore[type_Math]/?students.size()?*?100)?/?100;
averageScore[type_English]?=?(double)Math.round(averageScore[type_English]/?students.size()?*?100)?/?100;
averageScore[type_Java]?=?(double)Math.round(averageScore[type_Java]/?students.size()?*?100)?/?100;
return?averageScore;
}
public?static?ArrayListModelStudent?findStudentWithMaxScore(ArrayListModelStudent?students,?int?type){
ArrayListModelStudent?studentsWithMaxScore?=?new?ArrayListModelStudent();
ModelStudent?studentWithMaxScore?=?students.get(0);
for(ModelStudent?everyStudent:?students){
if(studentWithMaxScore.getStudentScore().getScore(type)??everyStudent.getStudentScore().getScore(type)){
studentWithMaxScore?=?everyStudent.clone();
}
}
for(ModelStudent?everyStudent:?students){
if(studentWithMaxScore.getStudentScore().getScore(type)?==?everyStudent.getStudentScore().getScore(type)){
studentsWithMaxScore.add(everyStudent);
}
}
return?studentsWithMaxScore;
}
public?static?ArrayListModelStudent?findStudentWithMinScore(ArrayListModelStudent?students,?int?type){
ArrayListModelStudent?studentsWithMaxScore?=?new?ArrayListModelStudent();
ModelStudent?studentWithMaxScore?=?students.get(0);
for(ModelStudent?everyStudent:?students){
if(studentWithMaxScore.getStudentScore().getScore(type)??everyStudent.getStudentScore().getScore(type)){
studentWithMaxScore?=?everyStudent.clone();
}
}
for(ModelStudent?everyStudent:?students){
if(studentWithMaxScore.getStudentScore().getScore(type)?==?everyStudent.getStudentScore().getScore(type)){
studentsWithMaxScore.add(everyStudent);
}
}
return?studentsWithMaxScore;
}
public?static?ArrayListModelStudent?searchStudentsByName(ArrayListModelStudent?students,?String?studentName){
ArrayListModelStudent?resultStudents?=?new?ArrayListModelStudent();
for(ModelStudent?everyStudent:?students){
if(everyStudent.getStudentName().equals(studentName)){
resultStudents.add(everyStudent);
}
}
return?resultStudents;
}
}
網(wǎng)站名稱(chēng):java中的程序代碼中心,代碼JAVA
文章出自:http://fisionsoft.com.cn/article/hegoog.html