十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
實現(xiàn)代碼如下:
創(chuàng)新互聯(lián)建站是專業(yè)的晉安網(wǎng)站建設(shè)公司,晉安接單;提供成都做網(wǎng)站、網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行晉安網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!
Student類:
public class Student {
private String name;
private String sex;
private int age;
private double chinese;
private double math;
private double english;
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 getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getChinese() {
return chinese;
}
public void setChinese(double chinese) {
this.chinese = chinese;
}
public double getMath() {
return math;
}
public void setMath(double math) {
this.math = math;
}
public double getEnglish() {
return english;
}
public void setEnglish(double english) {
this.english = english;
}
}
-----------------------------------------------------------------
StudentTest類:(測試類)
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args) {
Student student = new Student();
Scanner sc = new Scanner(System.in);
System.out.println("請輸入姓名:");
student.setName(sc.next());
System.out.println("請輸入性別:");
student.setSex(sc.next());
System.out.println("請輸入年齡:");
student.setAge(sc.nextInt());
System.out.println("請輸入語文成績、數(shù)學(xué)成績、英語成績:");
student.setChinese(sc.nextDouble());
student.setMath(sc.nextDouble());
student.setEnglish(sc.nextDouble());
Double count = student.getChinese()+ student.getMath()+student.getEnglish();
System.out.println("姓名:"+student.getName()+" 性別:"+student.getSex()+" 年齡:"+student.getAge());
System.out.println("總分:"+count+" 平均分:"+count/3);
}
}
運(yùn)行結(jié)果為:
按照題目要求編寫的Circle類的Java程序如下(文件名Circle.java)
public class Circle{
private double radius;
Circle(){
radius=0;
}
Circle(double r){
radius=r;
}
double getRadius(){
return radius;
}
double getLength(){
return 2*Math.PI*radius;
}
double getArea(){
return Math.PI*radius*radius;
}
void disp(){
System.out.println("圓的半徑為"+getRadius());
System.out.println("圓的周長為"+getLength());
System.out.println("圓的面積為"+getArea());
}
}
下面是Circle類的測試類Test(文件名Test.java 要運(yùn)行需要和Circle.java放在同一包內(nèi))
public class Test{
public static void main(String[] args){
Circle c=new Circle(2.5);
c.disp();
}
}
下面是用 Java 語言畫一個紅底五角星的代碼示例:
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics;
import javax.swing.JFrame; import javax.swing.JPanel;
public class FivePointedStar {
public static void main(String[] args) {
JFrame frame = new JFrame("Five Pointed Star");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FivePointedStarPanel panel = new FivePointedStarPanel();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
class FivePointedStarPanel extends JPanel {
public FivePointedStarPanel() {
setPreferredSize(new Dimension(300, 300));
setBackground(Color.WHITE);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int xPoints[] = {150, 200, 250, 225, 175, 125, 100, 125};
int yPoints[] = {100, 50, 100, 150, 150, 100, 50, 100};
int nPoints = 8; g.setColor(Color.RED);
g.fillPolygon(xPoints, yPoints, nPoints); }
}
這段代碼中,我們創(chuàng)建了一個 JFrame 窗口,并在窗口中添加了一個 JPanel 面板。在面板中,我們重寫了 paintComponent 方法,使用 Graphics 類的 fillPolygon 方法畫出了一個五角星。我們設(shè)置了五角星的顏色為紅色,并設(shè)置了面板的背景色為白色。
運(yùn)行這段代碼后,將會彈出一個窗口,顯示一個紅底五角星。
希望這個示例能幫助你畫出一個紅底五角星。
import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;public class JEncrytion{
public static void main(String[] argv) {
try{ KeyGenerator keygenerator = KeyGenerator.getInstance("DES"); SecretKey myDesKey = keygenerator.generateKey();
Cipher desCipher; // Create the cipher
desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// Initialize the cipher for encryption
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey); //sensitive information
byte[] text = "No body can see me".getBytes();
System.out.println("Text [Byte Format] : " + text);
System.out.println("Text : " + new String(text));
// Encrypt the text
byte[] textEncrypted = desCipher.doFinal(text);
System.out.println("Text Encryted : " + textEncrypted);
// Initialize the same cipher for decryption
desCipher.init(Cipher.DECRYPT_MODE, myDesKey); // Decrypt the text
byte[] textDecrypted = desCipher.doFinal(textEncrypted);
System.out.println("Text Decryted : " + new String(textDecrypted));
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}catch(NoSuchPaddingException e){
e.printStackTrace();
}catch(InvalidKeyException e){
e.printStackTrace();
}catch(IllegalBlockSizeException e){
e.printStackTrace();
}catch(BadPaddingException e){
e.printStackTrace();
}
}
}
國際上公認(rèn)的計算π的值得最好的方法,就是在一向一個邊長為1的正方形區(qū)域里面隨機(jī)的扔一些石子,用落在扇形里面的個數(shù)和總的個數(shù)的一個比例關(guān)系,就可以近似求解出π的值。
就類似這樣,我們可以知道這個比值 = (π/4),故π = 4*rate(比值) 。
下面貼一下Java的實現(xiàn)代碼:
public class RandomPI {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(rand_pi(100000)); //改變參數(shù)值
}
public static double rand_pi(int n) {
望采納!
像這種圖形對稱的由少變多再變少可以考慮絕對值控制。
打個比方:就像在一個數(shù)軸上,越接近中心則距離越短,然后過了中心后,就會離中心越來越遠(yuǎn)。代碼如下,如果n是可輸入的,你用IO輸入流代替即可。
public static void main(String[] args){
int n = 5;//*號的行數(shù)
/*控制*號的數(shù)量,最小為1,最大為n,由絕對值計算出變化*/
int m = 1;
int space = (n + 1) / 2;//空格的最大值
int s = 1;//和上面同理
for(int i = 0; i n; i++){
//控制空格輸出
for(int j = 0; j Math.abs(space-s); j++){
System.out.print(" ");
}
s++;
//控制*號輸出
for(int k = 0; k n - Math.abs(n - m); k++){
System.out.print("*");
}
m += 2;
System.out.println();
}
}