"13423"
大箐山ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
foo(0) 時 output 值為"134"
foo(1) 詩 output 值為"13423"
因為finally無論出沒出現(xiàn)異常都會要執(zhí)行的,哪怕你寫了return。所以finally語句塊常用來“收尾”工作。
第一題:
public?static?final?void?main(String[]?args)?{
long?sum?=?0;
for?(int?i?=?1;?i?=?10;?i++)?{
sum?+=?factorial(i);
}
System.out.println(sum);
}
/**
*?計算階乘
*/
public?static?long?factorial(long?n)?{
if?(n??0)?{
throw?new?IllegalArgumentException("n??0");
}
if?(n?==?0)?{
return?1L;
}
return?n?*?factorial(n?-?1);
}
第二題:使用正則表達(dá)式判斷字符類型
import?java.util.regex.Pattern;
public?class?Test?{
//?判斷大寫字母的正則表達(dá)式
public?static?final?Pattern?P_UPPER_CASE?=?Pattern.compile("[A-Z]");
//?判斷小寫字母的正則表達(dá)式
public?static?final?Pattern?P_LOWER_CASE?=?Pattern.compile("[a-z]");
//?判斷數(shù)字的正則表達(dá)式
public?static?final?Pattern?P_NUMBER?=?Pattern.compile("[0-9]");
public?static?final?void?main(String[]?args)?{
char[]?arr?=?{'a',?'b',?'c',?'1',?'W',?'0',?'L',?'d',?'8'};
int?countUpper?=?0;
int?countLower?=?0;
int?countNumber?=?0;
for?(int?i?=?0;?i??arr.length;?i++)?{
String?tmp?=?arr[i]?+?"";
if?(P_UPPER_CASE.matcher(tmp).matches())?{
countUpper++;
}?else?if?(P_LOWER_CASE.matcher(tmp).matches())?{
countLower++;
}?else?if?(P_NUMBER.matcher(tmp).matches())?{
countNumber++;
}
}
System.out.println(String.format("大寫字母個數(shù):%d,小寫字母個數(shù):%d,數(shù)字個數(shù):%d",?countUpper,?countLower,?countNumber));
}
}
第三題:
import?java.util.Scanner;
public?class?Test?{
public?static?final?void?main(String[]?args)?throws?InterruptedException?{
Scanner?in?=?new?Scanner(System.in);
while?(true)?{
System.out.println("請輸入溫度:");
double?temperature?=?in.nextDouble();
try?{
process(temperature);
}?catch?(TemperatureOutofRangeException?e)?{
System.err.println("孵化室溫度異常,請立即檢查!");
}
Thread.sleep(1000);
}
}
public?static?void?process(double?temperature)?throws?TemperatureOutofRangeException?{
if?(temperature?=?36??temperature?=?39)?{
System.out.println("孵化狀態(tài)正常!");
}?else?{
throw?new?TemperatureOutofRangeException();
}
}
}
//?異常類
public?class?TemperatureOutofRangeException?extends?Exception?{
public?TemperatureOutofRangeException()?{
super();
}
}
import?java.util.Arrays;
public?class?Test?{
public?static?void?main(String[]?args)?{
int[]?arr={1,2?,3?,?78?,??34?,??33?,?16?,?35};
Arrays.sort(arr);
for(int?i=arr.length-1;i=0;i--){
System.out.print(arr[i]+"\t");
}
System.out.println();
int?a=0;
for(int?i=arr.length-1;i=0;i--){
if(i0a==arr[i]arr[i]-arr[i-1]==1){
continue;
}
if(i0arr[i]-arr[i-1]==1){
a=arr[i-1];
System.out.print(arr[i]+"\t");
}else{
System.out.print(arr[i]+"\t");
}
}
}
}
下面是WINDOW界面的代碼,用JAVA SWING寫的。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PlusWhile {
//主面板
private JFrame mainTop = null;
private JPanel inPanel = null; //輸入面板
private JPanel outPanel = null;//輸出面板
private JButton count = null; //按鈕
private JTextField inText = null; // 輸入文本框
private JTextField result = null;//結(jié)果文本框
private JLabel resultOut = null;//輸出提示
private JLabel remindIn = null;//輸入提示
//初始化方法
private void init() {
//組件實例化
mainTop = new JFrame("階乘計算器");
inPanel = new JPanel();
outPanel = new JPanel();
count = new JButton("計算");
inText = new JTextField(10);
result = new JTextField(10);
resultOut = new JLabel("計算的結(jié)果:");
remindIn = new JLabel("請輸入要計算的正整數(shù):");
//添加輸入組件
inPanel.add(remindIn);
inPanel.add(inText);
inPanel.add(count);
//添加輸出組件
outPanel.add(resultOut);
outPanel.add(result);
result.setEnabled(false);
//設(shè)置布局樣式
mainTop.setLayout(new BorderLayout());
//主面板添加組件
mainTop.add(inPanel, BorderLayout.NORTH);
mainTop.add(outPanel, BorderLayout.SOUTH);
//設(shè)置監(jiān)聽
count.addActionListener(new ButtonListener());
//設(shè)置屏顯位置
mainTop.setLocation(350 , 300);
mainTop.pack();
mainTop.setVisible(true);
mainTop.setAlwaysOnTop(true);
//設(shè)置關(guān)閉事件
mainTop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//監(jiān)聽類
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
int inputNum = 0;
String input = inText.getText();
//若輸入為非數(shù)字,提示錯誤
try {
inputNum = Integer.parseInt(input);
}catch(Exception e) {
inputNum = -1;
}
//輸入數(shù)值小于0,提示錯誤
if(inputNum 0) {
inText.setText("請輸入正確的數(shù)值!");
result.setText("");
}
else if(inputNum == 0) {//輸入值為0處理
result.setText("1");
}
else {//輸入值為正整數(shù),計算階乘
int temp = inputNum;
while(inputNum 1) {
temp *= --inputNum;
}
result.setText(temp + "");
}
}
}
//入口方法
public static void main(String[] args) {
new PlusWhile().init();
}
}
有什么不明白的繼續(xù)補充,呵呵。 加點兒分不。
有 4 個 java 文件,分別是 Shape.java,Square.java,Circle.java,ShapeTest.java。其內(nèi)容如下:
Shape.java
package graphics;
interface class Shape {
double getArea();
double getPerimeter();
}
Square.java:
package graphics;
public class Square implements Shape {
private double x;
private double y;
private double width;
private double height;
public Square(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public Square(double x0, double y0, double x1, double y1) {
double xmin = x0 x1 ? x1 : x0;
double ymin = y0 y1 ? y1 : y0;
this.x = xmin;
this.y = ymin;
this.width = x0 + x1 - 2*xmin;
this.height = y0 + y1 - 2*ymin;
}
@Override
public double getArea() {
return this.width * this.height;
}
@Override
public double getPerimeter() {
return 2 * (this.width + this.height);
}
}
代碼如下,望采納
public?class?PrintPrime{
public?static?void?main(String?args[]){
//設(shè)置一個計數(shù)變量count,用于統(tǒng)計一行當(dāng)中已經(jīng)輸出數(shù)字的個數(shù)
int?count?=?0;
//寫代碼時人為判斷200為非素數(shù),如果不考慮題目的嚴(yán)格要求的話,可以寫成200
for(int?i?=?100;i=200;i++){
//判斷數(shù)字是否為素數(shù),若是,則count+1并輸出數(shù)字
if(PrintPrime.IsPrime(i)){
count++;
System.out.print(i+"?");
}
//如果一行十個已經(jīng)輸出完畢,計數(shù)歸零,換行
if(count==10){
count=0;
System.out.println();
}
}
}
//判斷數(shù)字是否為素數(shù)
public?static?boolean?IsPrime(int?n){
//如果小于等于三,則大于一即為素數(shù)
if?(n?=?3)?{
return?n??1;
}
//從2循環(huán)到數(shù)字的開平方,算法優(yōu)化
for(int?i=2;i=Math.sqrt(n);i++){
if(n%i?==?0)
return?false;
}
return?true;
}
}