本文目录一览:
Java编程(写出程序代码)
写了一个代码,代码如下,可以进行参考
public class sum {
public static void main(String[] args) {
//创建一个Scanner的对象input
Scanner input = new Scanner(System.in);
//提示用户输入数据
System.out.print("请输入一个整数");
//将输入的值赋给n
int n = input.nextInt();
//定义变量接收计算后的和
int sum = 0;
//利用循环进行求和
for (int i = 0; i = n; i++) {
sum+=i;
}
//输出最后的和
System.out.println("从0一直到"+n+"的所有整数的和是:"+sum);
}
}
求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编程代码
public class Test {
public static void main(String[] args) {
MyRectangle rec = new MyRectangle(3, 5);
MyRectangle square = new MySquare(4);
System.out.println(rec.toString());
System.out.println(square.toString());
}
}
class MyRectangle{
protected double width;
protected double length;
public MyRectangle(double length, double width){
this.width = width;
this.length = length;
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getArea(){
return this.width * this.length;
}
public String toString(){
return "长方形的长为:" + length + ", 宽: " + width + ", 面积为:" + getArea();
}
}
class MySquare extends MyRectangle{
public MySquare(double length){
super(length, length);
}
public double getArea(){
return Math.pow(super.width, 2);
}
public String toString(){
return "正方形边长为: " + super.length + ", 面积为: " + getArea();
}
}
----------测试
长方形的长为:3.0, 宽: 5.0, 面积为:15.0
正方形边长为: 4.0, 面积为: 16.0
Java编程 代码如下
"13423"
foo(0) 时 output 值为"134"
foo(1) 诗 output 值为"13423"
因为finally无论出没出现异常都会要执行的,哪怕你写了return。所以finally语句块常用来“收尾”工作。
java编程代码!请仔细看我的说明. . .
我来帮您,代码如下:
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
public class A extends JFrame {
private static final long serialVersionUID = 1L;
JProgressBar pb;
A() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setLocation(400, 100);
getContentPane().setLayout(null);
pb = new JProgressBar(); // 改动1
pb.setBounds(10, 201, 212, 40);
getContentPane().add(pb);
setTitle("滚动条测试!");
pb.setStringPainted(true);
final JLabel label = new JLabel();
label.setText("正在登陆验证,请稍候。。。");
label.setBounds(10, 179, 212, 18);
getContentPane().add(label);
final JLabel label03 = new JLabel();
label03.setBounds(49, 247, 13, 18);
getContentPane().add(label03);
final JLabel label04 = new JLabel();
label04.setBounds(68, 247, 13, 18);
getContentPane().add(label04);
final JLabel label05 = new JLabel();
label05.setBounds(86, 247, 13, 18);
getContentPane().add(label05);
final JLabel label06 = new JLabel();
label06.setBounds(105, 247, 13, 18);
getContentPane().add(label06);
final JLabel label07 = new JLabel();
label07.setBounds(124, 247, 13, 18);
getContentPane().add(label07);
final JLabel label02 = new JLabel();
label02.setBounds(30, 247, 13, 18);
getContentPane().add(label02);
final JLabel label01 = new JLabel();
label01.setBounds(10, 247, 13, 18);
getContentPane().add(label01);
final JLabel label08 = new JLabel();
label08.setBounds(143, 247, 13, 18);
getContentPane().add(label08);
final JLabel label09 = new JLabel();
label09.setBounds(162, 247, 13, 18);
getContentPane().add(label09);
final JLabel label10 = new JLabel();
label10.setBounds(177, 247, 13, 18);
getContentPane().add(label10);
JButton btn1 = new JButton("登陆");
btn1.addActionListener(new ActionListener() {
private int i;
public synchronized void actionPerformed(final ActionEvent e) {
new Thread() {
public synchronized void run() {
for (i = 0; i = 100; i++) {
pb.setValue(i);
try {
Thread.currentThread();
Thread.sleep(200);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
switch (i) {
case 0:
label01.setText("*");
break;
case 10:
label01.setText("*");
label02.setText("*");
break;
case 20:
label01.setText("*");
label02.setText("*");
label03.setText("*");
break;
case 30:
label01.setText("*");
label02.setText("*");
label03.setText("*");
label04.setText("*");
break;
case 40:
label01.setText("*");
label02.setText("*");
label03.setText("*");
label04.setText("*");
label05.setText("*");
break;
case 50:
label01.setText("*");
label02.setText("*");
label03.setText("*");
label04.setText("*");
label05.setText("*");
label06.setText("*");
break;
case 60:
label01.setText("*");
label02.setText("*");
label03.setText("*");
label04.setText("*");
label05.setText("*");
label06.setText("*");
label07.setText("*");
break;
case 70:
label01.setText("*");
label02.setText("*");
label03.setText("*");
label04.setText("*");
label05.setText("*");
label06.setText("*");
label07.setText("*");
label08.setText("*");
break;
case 80:
label01.setText("*");
label02.setText("*");
label03.setText("*");
label04.setText("*");
label05.setText("*");
label06.setText("*");
label07.setText("*");
label08.setText("*");
label09.setText("*");
break;
case 90:
label01.setText("*");
label02.setText("*");
label03.setText("*");
label04.setText("*");
label05.setText("*");
label06.setText("*");
label07.setText("*");
label08.setText("*");
label09.setText("*");
label10.setText("*");
break;
case 100:
JOptionPane.showMessageDialog((new A()
.getContentPane()), "登陆成功", "恭喜您!",
JOptionPane.WARNING_MESSAGE);
label01.setText("");
label02.setText("");
label03.setText("");
label04.setText("");
label05.setText("");
label06.setText("");
label07.setText("");
label08.setText("");
label09.setText("");
label10.setText("");
pb.setValue(0);
}
}
}
}.start();
}
});
btn1.setBounds(86, 92, 90, 39);
Container con = getContentPane();
con.add(btn1);
}
public static void main(String[] args) {
new A().setVisible(true);
}
}
求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;
}
}