導航:首頁 > 編程語言 > JAVA編程

JAVA編程

發布時間:2022-01-23 20:57:01

java編程 詳細

Student 類:
public class Student {
public String name;
private String sex;
private int height;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

}

HeightAvg 類:
import java.util.ArrayList;
import java.util.List;

public class HeightAvg {
public static int getAvg(List<Student> studentList) {
int avgValue = 0;
int sumValue = 0;
int countNum = 0;
countNum = studentList.size();
for (int i = 0; i < countNum; i++) {
sumValue = sumValue + studentList.get(i).getHeight();
}

if (countNum == 0) {
avgValue = 0;
} else {
avgValue = sumValue / countNum;
}
return avgValue;
}

public static void main(String[] args) {
List<Student> studentList = new ArrayList<Student>();

Student student1 = new Student();
student1.setHeight(10);

Student student2 = new Student();
student2.setHeight(20);

Student student3 = new Student();
student3.setHeight(30);

Student student4 = new Student();
student4.setHeight(40);

studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
studentList.add(student4);

int avgValue =getAvg(studentList);
System.out.println(avgValue);
}
}

② JAVA編程

packageorg.yitianlulongji;

/**
*JAVA編程編寫一個JAVA程序,輸出1~100之間所有即可以被3整除,又可以被7整除的數,並計算它們的和。
*
*@author
*
*/
publicclassTest{
publicstaticvoidmain(String[]args){
getSums();
//getSums1();
}

publicstaticvoidgetSums(){
intsums=0;
for(inti=1;i<=100;i++){//1~100之間
if(i%3==0&&i%7==0){//即可以被3整除,又可以被7整除的數
//System.out.println(i);//輸出符合的內容
sums+=i;//sums=sums+i;//計算它們的和
}
}
System.out.print(sums);

}

publicstaticvoidgetSums1(){

intsums=0;
intconstant=21;//常量21,因為3和7的最小公倍數為21
intc=constant;
while(true){
//System.out.println("sums="+sums+",c="+c);
sums+=c;
c=c+constant;
if(c>100){
break;
}

}
System.out.print(sums);
}
}

③ Java編程

這個問題最好不要深究,你大可認為java中的類就好像,我們坐的就叫椅子一樣,沒什麼好研究的,在《JAVA編程思想》中作者都不作出解釋,不過他引用了別人的話「類是具有相同特性和行為的對象集合」,說心裡話,這對於剛學JAVA的人來說,誰能真正理解,所以你根本不需要去了解他是什麼東東,只需要知道他就叫類就可以了,過多的研究既無什麼意義反而會自己讓自己苦惱,你真正要理解的應該是什麼叫做面向對象編程!!而這個是需要大量實踐+看書+視頻,絕對不是通過其他人的回答就能明白的了。
PS:在外包企業的面試中,如果有面試官問你「知不知道什麼叫類」,
你一定要說「不知道!!」。
類也可以說成為類型,作用當然是封裝數據在裡面,你所說的有很多種類,其實就是類型不一樣,各個類處理的事情不同,剛開始也沒必要理解他,只需要怎麼使用他就行了。
通常我們都會把欄位,方法等數據放在類裡面,這種是面想對象編程的概念
比如你想像一下,一個學生,要你寫一個學生類怎麼寫?
首先學生有學號,姓名,性別等等
學生還有各種方法,指示他們能做什麼
那麼
class
Student
{
private
int
number;
private
String
name;
private
String
sex;
public
void
showInfo()
{
//
}
}

④ 一個java編程

按照你的要求編寫的Java程序如下

import java.util.Scanner;

public class A{

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

String s = sc.nextLine();

for(int i=0;i<s.length();i++){

for(int j=i+1;j<s.length();j++){

if(Character.isLetter(s.charAt(i)) && s.charAt(i)==s.charAt(j)){

s=s.substring(0,j)+s.substring(j+1,s.length());

}

}

}

System.out.println(s);

}

}

源代碼

⑤ Java編程

使用同樣的方法名,但是傳遞的參數不同,根據不同的參數實現不同的算數

⑥ java編程

利用除法和取余操作分別計算數位上的值
百位上的:153/100=1
十位上的:(153%100)/10=5
個位上的:(153%100)%10=3

⑦ java編程

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextField;

public class Calucator extends JFrame {

private JTextField tf;
private JPanel panel1, panel2, panel3, panel4;
private JMenuBar myBar;
private JMenu menu1, menu2, menu3;
private JMenuItem editItem1, editItem2, help1, help2, help3;
private JRadioButtonMenuItem seeItem1, seeItem2;
private JCheckBoxMenuItem seeItem3;
private ButtonGroup bgb;
private String back;
private boolean IfResult = true, falg = false;
private String oper = "=";
private double result = 0;
private Num numActionListener;
private DecimalFormat df;

public Calucator(){
super("計算器");

df = new DecimalFormat("0.#######");

this.setLayout(new BorderLayout(10, 5));
panel1 = new JPanel(new GridLayout(1, 3, 10, 10));
panel2 = new JPanel(new GridLayout(4, 5, 5, 5));
panel3 = new JPanel(new GridLayout(5, 1, 5, 5));
panel4 = new JPanel(new BorderLayout(5, 5));

myBar = new JMenuBar();
menu1 = new JMenu("編輯");
menu2 = new JMenu("查看");
menu3 = new JMenu("幫助");

menu1.setFont(new Font("宋體", Font.PLAIN, 12));
menu2.setFont(new Font("宋體", Font.PLAIN, 12));
menu3.setFont(new Font("宋體", Font.PLAIN, 12));

editItem1 = new JMenuItem("復制");
editItem2 = new JMenuItem("粘貼");

seeItem1 = new JRadioButtonMenuItem("科學型");
seeItem2 = new JRadioButtonMenuItem("標准型");
seeItem3 = new JCheckBoxMenuItem("數字分組");

help1 = new JMenuItem("幫助主題");
help2 = new JMenuItem("關於計算器(A)");

bgb = new ButtonGroup();

menu1.add(editItem1);
menu1.add(editItem2);

menu2.add(seeItem1);
menu2.add(seeItem2);
menu2.addSeparator();
menu2.add(seeItem3);

menu3.add(help1);
menu3.add(help2);

myBar.add(menu1);
myBar.add(menu2);
myBar.add(menu3);
this.setJMenuBar(myBar);

numActionListener = new Num();

tf = new JTextField();
tf.setEditable(false);
tf.setBackground(Color.white);
tf.setHorizontalAlignment(JTextField.RIGHT);
tf.setText("0");
tf.setBorder(BorderFactory.createLoweredBevelBorder());
init();

}

private void init(){

addButton(panel1, "Backspace", new Clear(), Color.red);
addButton(panel1, "CE", null, Color.red);
addButton(panel1, "C", new Clear(), Color.red);

addButton(panel2, "7", numActionListener, Color.blue);
addButton(panel2, "8", numActionListener, Color.blue);
addButton(panel2, "9", numActionListener, Color.blue);
addButton(panel2, "/", new Signs(), Color.red);
addButton(panel2, "sqrt", new Signs(), Color.blue);

addButton(panel2, "4", numActionListener, Color.blue);
addButton(panel2, "5", numActionListener, Color.blue);
addButton(panel2, "6", numActionListener, Color.blue);
addButton(panel2, "*", new Signs(), Color.red);
addButton(panel2, "%", new Signs(), Color.blue);

addButton(panel2, "1", numActionListener, Color.blue);
addButton(panel2, "2", numActionListener, Color.blue);
addButton(panel2, "3", numActionListener, Color.blue);
addButton(panel2, "-", new Signs(), Color.red);
addButton(panel2, "1/x", new Signs(), Color.blue);

addButton(panel2, "0", numActionListener, Color.blue);
addButton(panel2, "-/+", new Clear(), Color.blue);
addButton(panel2, ".", new Dot(), Color.blue);
addButton(panel2, "+", new Signs(), Color.red);
addButton(panel2, "=", new Signs(), Color.red);

JButton btns = new JButton("calucator");
btns.setBorder(BorderFactory.createLoweredBevelBorder());
btns.setEnabled(false);
btns.setPreferredSize(new Dimension(20, 20));

panel3.add(btns);
addButton(panel3, "MC", null, Color.red);
addButton(panel3, "MR", null, Color.red);
addButton(panel3, "MS", null, Color.red);
addButton(panel3, "M+", null, Color.red);

panel4.add(panel1, BorderLayout.NORTH);
panel4.add(panel2, BorderLayout.CENTER);
this.add(tf, BorderLayout.NORTH);
this.add(panel3, BorderLayout.WEST);
this.add(panel4);

pack();
this.setResizable(false);
this.setLocation(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private void addButton(JPanel panel, String name, ActionListener action, Color color){
JButton bt = new JButton(name);
panel.add(bt);
bt.setForeground(color);
bt.addActionListener(action);
}

public static void main(String[] args) {

new Calucator().setVisible(true);

}

private void getResult (double x){
System.out.println("result"+result+" "+"x"+x);
if(oper == "+"){
result += x;
}else if(oper == "-"){
result -= x;
}else if(oper == "*"){
result *= x;
}else if(oper == "/"){
result /= x;
}else if(oper == "="){
result = x;
}
System.out.println("result"+result+" "+"x"+x);
tf.setText(df.format(result));
System.out.println(df.format(result));
}

class Signs implements ActionListener{

public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if(str.equals("sqrt")){
double i = Double.parseDouble(tf.getText());

if(i>0){
tf.setText(String.valueOf(df.format(Math.sqrt(i))));
}else{
tf.setText("負數不能開平方根");
}

}else if(str.equals("%")){
tf.setText(df.format(Double.parseDouble(tf.getText()) / 100));
}else if(str.equals("1/x")){

if(Double.parseDouble(tf.getText()) == 0){
tf.setText("除數不能為零");
}else{
tf.setText(df.format(1 / Double.parseDouble(tf.getText())));
}

}else{
if(falg){
IfResult = false;
}
if(IfResult){
oper = str;
System.out.println(oper);
}else{
getResult(Double.parseDouble(tf.getText()));
oper = str;
IfResult = true;
}
}
}
}

class Clear implements ActionListener{

public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if(str == "C"){
tf.setText("0");
IfResult = true;
result = 0;
}else if(str == "-/+"){
double i = 0 - Double.parseDouble(tf.getText().trim());
tf.setText(df.format(i));
}else if(str == "Backspace"){
if(Double.parseDouble(tf.getText()) > 0){

if(tf.getText().length() > 1){
tf.setText(tf.getText().substring(0, tf.getText().length() - 1));
}else{
tf.setText("0");
IfResult = true;
}

}else{
if(tf.getText().length() > 2){
tf.setText(tf.getText().substring(0, tf.getText().length() - 1));
}else{
tf.setText("0");
IfResult = true;
}
}
}

}
}

class Num implements ActionListener{

public void actionPerformed(ActionEvent e) {
String srt = e.getActionCommand();
if(IfResult){
tf.setText("");
IfResult = false;
}
tf.setText(tf.getText().trim() + srt);
if(tf.getText().equals("0")){
tf.setText("0");
IfResult = true;
falg = true;
}
}

}

class Dot implements ActionListener{

public void actionPerformed(ActionEvent e) {
IfResult = false;
if(tf.getText().trim().indexOf(".") == -1){
tf.setText(tf.getText() + ".");
}

}

}

}

⑧ java編程是什麼

Java是一種可以撰寫跨平台應用程序的、面向對象的程序設計語言。

簡單說,Java就是一種語言,不過是一種針對jvm的語言,Java編譯後是位元組碼,而jvm會把這些位元組碼解釋成機器碼,然後做出相應的動作。

Java是計算機和我們的溝通語言,計算機可以懂Java這門語言。當然,你學會了Java,你也會這門語言,你就可以和計算機溝通啦!

比如說,你對你女朋友說「給我倒洗腳水去!」,然後你女朋友聽到了這句話,知道自己該幹嘛了,就去給你倒洗腳水了(現實中大部分不是這樣,哈哈)。

那麼你要是和計算機溝通,想讓計算機給我們做一件事情,總不能說「喂,計算機,給我倒洗腳水去」,這個計算機還真聽不懂,那咋辦?

當然是得使用計算機能夠明白的語言與它溝通啦!那是啥,Java就是啊,你使用Java是可以和計算機溝通的,當然這里的溝通是類比我們人與人之間的說法,其實對於計算機而言,准確的應該是我們給計算機下達指令,計算機收到我們的指令去做相應的事情。

怎麼樣,啥是Java,明白那麼一丟丟了吧!

Java是一門語言,記住了吧,這個語言是用於計算機的,我們使用Java可以和計算機進行交流,我們寫一段Java代碼,計算機就知道我們想幹啥,當然前提是你寫的是正常的代碼,所以Java可以叫做計算機語言。

如果想學習這門語言,可私聊找我要學習資料哦~免費提供的喲~

⑨ java編寫程序

你這要求都說的這么混亂,還是放棄編程把

⑩ java編程是什麼

Java(計算機編程語言),深入了解Java開發?
Java語言是SUN(Stanford University Network,斯坦福大學網路公司)公司1995年推出的一門高級編程語言,起初主要應用在小型消費電子產品上,後來隨著互聯網的興起,Java語言迅速崛起(Java applet 可以在瀏覽器中運行),成為大型互聯網項目的首選語言。
從首次發布開始,Java就躍到了 Internet 編程的前沿。後續的每一個版本都進一步鞏固了這一地位。如今,Java依然是開發基於 Web 的應用程序的最佳選擇。此外,Java還是智能手機變革的推手,Android 編程採用的就是Java語言。
Java可分為3個體系,即JavaSE、JavaEE 和JavaME。下面千鋒重慶Java的小編簡單介紹下這3個體系。
1、JavaSE
JavaSE(JavaPlatform Standard Edition,Java平台標准版)以前稱為J2SE,它允許開發和部署在桌面、伺服器、嵌入式環境和實時環境中使用的Java應用程序。JavaSE包含了支持JavaWeb服務開發的類,並為JavaEE 提供基礎,如Java語言基礎、JDBC操作、I/O 操作、網路通信以及多線程等技術。圖所示為JavaSE的體系結構。

2、JavaEE
JavaEE(JavaPlatform Enterprise Edition,Java平台企業版)以前稱為J2EE。企業版本幫助開發和部署可移植、健壯、可伸縮且安全的伺服器端Java應用程序。JavaEE是在JavaSE基礎上構建的,它提供Web服務、組件模型、管理和通信API,可以用來實現企業級的面向服務體系結構(Service Oriented Architecture,SOA)和Web 2.0應用程序。
3、JavaME
JavaME(JavaPlatform Micro Edition,Java平台微型版)以前稱為 J2ME,也叫 K-JAVA。JavaME 為在移動設備和嵌入式設備(比如手機、PDA、電視機頂盒和列印機)上運行的應用程序提供一個健壯且靈活的環境。
JavaME包括靈活的用戶界面、健壯的安全模型、豐富的內置網路協議以及對可以動態下載的聯網和離線應用程序。基於JavaME 規范的應用程序 只需編寫一次就可以用於許多設備,而且可以利用每個設備的本機功能。

閱讀全文

與JAVA編程相關的資料

熱點內容
python超簡單編程 瀏覽:257
獲取命令方 瀏覽:976
怎樣製作文件夾和圖片 瀏覽:58
調研編譯寫信息 瀏覽:860
python馮諾依曼 瀏覽:417
同時安裝多個app有什麼影響 瀏覽:254
奧術殺戮命令宏 瀏覽:183
用sdes加密明文字母e 瀏覽:360
單片機原理及應用試題 瀏覽:423
易語言開啟指定文件夾 瀏覽:40
馬思純參加密室大逃脫 瀏覽:322
文件夾冬季澆築溫度 瀏覽:712
京東有返點的aPp叫什麼 瀏覽:603
如何查看u點家庭伺服器是幾兆 瀏覽:262
python應用介面怎麼接 瀏覽:67
腐蝕怎麼進不去伺服器啊 瀏覽:359
linuxcpiogz 瀏覽:631
安卓中的布局是什麼文件 瀏覽:397
dex反編譯部分代碼無法查看 瀏覽:464
linuxandroid編譯 瀏覽:603