java大学编程代码大全 java编程常用代码
Java编程 代码如下
"13423"
大箐山ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
foo(0) 时 output 值为"134"
foo(1) 诗 output 值为"13423"
因为finally无论出没出现异常都会要执行的,哪怕你写了return。所以finally语句块常用来“收尾”工作。
JAVA编程,帮帮忙,写一下代码
第一题:
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);
}
第二题:使用正则表达式判断字符类型
import java.util.regex.Pattern;
public class Test {
// 判断大写字母的正则表达式
public static final Pattern P_UPPER_CASE = Pattern.compile("[A-Z]");
// 判断小写字母的正则表达式
public static final Pattern P_LOWER_CASE = Pattern.compile("[a-z]");
// 判断数字的正则表达式
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("大写字母个数:%d,小写字母个数:%d,数字个数:%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("孵化状态正常!");
} else {
throw new TemperatureOutofRangeException();
}
}
}
// 异常类
public class TemperatureOutofRangeException extends Exception {
public TemperatureOutofRangeException() {
super();
}
}
求Java编程代码
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");
}
}
}
}
求JAVA编程代码…………
下面是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;//结果文本框
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("计算的结果:");
remindIn = new JLabel("请输入要计算的正整数:");
//添加输入组件
inPanel.add(remindIn);
inPanel.add(inText);
inPanel.add(count);
//添加输出组件
outPanel.add(resultOut);
outPanel.add(result);
result.setEnabled(false);
//设置布局样式
mainTop.setLayout(new BorderLayout());
//主面板添加组件
mainTop.add(inPanel, BorderLayout.NORTH);
mainTop.add(outPanel, BorderLayout.SOUTH);
//设置监听
count.addActionListener(new ButtonListener());
//设置屏显位置
mainTop.setLocation(350 , 300);
mainTop.pack();
mainTop.setVisible(true);
mainTop.setAlwaysOnTop(true);
//设置关闭事件
mainTop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//监听类
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
int inputNum = 0;
String input = inText.getText();
//若输入为非数字,提示错误
try {
inputNum = Integer.parseInt(input);
}catch(Exception e) {
inputNum = -1;
}
//输入数值小于0,提示错误
if(inputNum 0) {
inText.setText("请输入正确的数值!");
result.setText("");
}
else if(inputNum == 0) {//输入值为0处理
result.setText("1");
}
else {//输入值为正整数,计算阶乘
int temp = inputNum;
while(inputNum 1) {
temp *= --inputNum;
}
result.setText(temp + "");
}
}
}
//入口方法
public static void main(String[] args) {
new PlusWhile().init();
}
}
有什么不明白的继续补充,呵呵。 加点儿分不。
编程java 求代码
有 4 个 java 文件,分别是 Shape.java,Square.java,Circle.java,ShapeTest.java。其内容如下:
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);
}
}
求java编程的代码
代码如下,望采纳
public class PrintPrime{
public static void main(String args[]){
//设置一个计数变量count,用于统计一行当中已经输出数字的个数
int count = 0;
//写代码时人为判断200为非素数,如果不考虑题目的严格要求的话,可以写成200
for(int i = 100;i=200;i++){
//判断数字是否为素数,若是,则count+1并输出数字
if(PrintPrime.IsPrime(i)){
count++;
System.out.print(i+" ");
}
//如果一行十个已经输出完毕,计数归零,换行
if(count==10){
count=0;
System.out.println();
}
}
}
//判断数字是否为素数
public static boolean IsPrime(int n){
//如果小于等于三,则大于一即为素数
if (n = 3) {
return n 1;
}
//从2循环到数字的开平方,算法优化
for(int i=2;i=Math.sqrt(n);i++){
if(n%i == 0)
return false;
}
return true;
}
}
网站标题:java大学编程代码大全 java编程常用代码
转载来于:http://abwzjs.com/article/hppped.html