java基本代碼
『壹』 java求程序代碼
按照你的要求編寫的Java程序如下
import java.util.Scanner;
public class S{
public static void main(String[] args){
int n=5;
int[] a=new int[n];
System.out.print("請輸入"+n+"個正整數:");
Scanner sc=new Scanner(System.in);
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
System.out.print("其中的偶數為:");
for(int i=0;i<n;i++){
if(a[i]%2==0){
System.out.print(a[i]+" ");
}
}
}
}
『貳』 給段最簡單的java代碼 讓我新手看一下
最簡單的java代碼肯定就是這個了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
「hello world」就是應該是所有學java的新手看的第一個代碼了。如果是零基礎的新手朋友們可以來我們的java實驗班試聽,有免費的試聽課程幫助學習java必備基礎知識,有助教老師為零基礎的人提供個人學習方案,學習完成後有考評團進行專業測試,幫助測評學員是否適合繼續學習java,15天內免費幫助來報名體驗實驗班的新手快速入門java,更好的學習java!
『叄』 java課程設計題目及代碼是什麼
java課程設計題目及代碼分別是:
1、題目:計算器。設計內容是設計一個圖形界面(GUI)的計算器應用程序,完成簡單的算術運算。
設計要求是設計的計算器應用程序可以完成家法、減法、乘法、除法和取余運算。且有小數點、正負號、求倒數、退格和清零功能。
2、代碼:
數字按鈕NumberButton類如下:
import java.awt.
import java.awt.event.
import javax.swing.
public class NumberButton extends Button.
{
int number.
public NumberButton(int number).
{
super(""+number).
this.number=number.
setForeground(Color.blue).
}
public int getNumber().
{
return number;
}
}
其它java課程設計題目及代碼是:
題目:華容道。編寫一個按鈕的子類,使用該子類創建的對象代表華容道中的人物。通過焦點事件控制人物顏色,當人物獲得焦點時顏色為藍色,當失去焦點時顏色為灰色。
通過鍵盤事件和滑鼠事件來實現曹操、關羽等人物的移動。當人物上發生滑鼠事件或鍵盤事件時,如果滑鼠指針的位置是在人物的下方(也就是組件的下半部分)或按下鍵盤的「↓「鍵,該人物向下移動。向左、向右和向上的移動原理類似。
代碼是:
String name[]={"曹操","關羽","張","劉","馬","許","兵","兵","兵","兵"}.
for(int i=0;i<name.length;i++).
{
person[i]=new Person(i,name[i]).
person[i].addKeyListener(this).
person[i].addMouseListener(this).
// person[i].addFocusListener(new Person).
add(person[i]).
}
person[0].setBounds(104,54,100,100).
person[1].setBounds(104,154,100,50).
person[2].setBounds(54,154,50,100).
person[3].setBounds(204,154,50,100).
person[4].setBounds(54,54,50,100).
person[5].setBounds(204,54,50,100);
person[6].setBounds(54,254,50,50);
person[7].setBounds(204,254,50,50);
person[8].setBounds(104,204,50,50);
person[9].setBounds(154,204,50,50);
『肆』 我需要一段最簡單的java代碼程序
public class HelloWorld{
public static void main(String[] args){
System.out.println("hello world!");
}
}
『伍』 Java100行以上源代碼,至少五個class以及一個interface,可以簡單點
下面是一個可能的Java源代碼,它包含了一個介面租沖薯(Shape)和五個類(Circle, Rectangle, Triangle, Square 和 Main)。它的功能是計算不同形狀的面積和周長。
//定義一個介面Shape,有兩判指個抽象方法:getArea()和getPerimeter()interface Shape { double getArea(); double getPerimeter();
}//定義一個類Circle,實現Shape介面class Circle implements Shape { //定義一個私有屬性radius,表示圓的半徑
private double radius; //定義一個公有構造方法,用於初始化radius
public Circle(double radius) { this.radius = radius;
} //實現getArea()方法,返回圓的面積
public double getArea() { return Math.PI * radius * radius;
} //實現getPerimeter()方法,返回圓的周長
public double getPerimeter() { return Math.PI * radius * 2;
}
}//定義一個類Rectangle,實現Shape介面class Rectangle implements Shape { //定義兩個私有屬性width和height,表示矩形的寬度和高度
private double width; private double height; //定義一個公有構造方法,用於初始化width和height
public Rectangle(double width, double height) { this.width = width; this.height = height;
} //實現getArea()方法,返回矩形的面積
public double getArea() { return width * height;
} //實現getPerimeter()方法,返回矩形的周長
public double getPerimeter() { return (width + height) *2;
}
}//定義一個類Triangle,實現Shape介面class Triangle implements Shape { //定義三個私有屬性a,b,c表示三角形的三條邊長
private double a; private double b; private double c; //定義一個公有構造方法,用於初始化a,b,c,並檢查是否滿足三角形條件(任意兩邊之和大於第三邊)
public Triangle(double a, double b, double c) throws Exception{ if (a + b > c && a + c > b && b + c > a) {
this.a = a; this.b = b;
this.c = c;
} else {
throw new Exception("Invalid triangle");
}
} //實現getArea()方法,返回三角形的面積(使用海倫公式)
public double getArea() { //計算半周長p
double p = (a + b + c) /2; //計算並返回面積s(使用Math.sqrt()函數求平方根)
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
} //實現getPerimeter()方法,返回三角形的周長
public double getPerimeter(){ return a + b + c;
}
}//定義一個類Square,繼承Rectangle類,並重寫構造方法和toString()方法class Square extends Rectangle { //重寫構造方法,在調用父類構造方法時傳入相弊者同的參數side作為width和height
public Square(double side){ super(side, side);
} //重寫toString()方法,在原來基礎上加上"Square:"前綴,並只顯示side屬性而不顯示width和height屬性(使用String.format()函數格式化字元串)
@Override
public String toString(){ return String.format("Square: side=%.2f", super.width); /* 或者直接使用super.getPerimeter()/4作為side */
/* return String.format("Square: side=%.2f", super.getPerimeter()/4); */
/* 注意:不能直接訪問super.side屬性,