程序一
package 变量的声明和赋值;
public class UseVariables {
public static void main(String[] args) {
// TODO 自动生成的方法存根
final int PRICE = 30;
long l = 1234l;
int num,total;
float r,v,h = 3.5f;
boolean truth = true;
boolean false1;
char c;
c = 'a';
num = 10;
total = num*PRICE;
r = 2.5f;
v = 3.14159f*r*r*h;
false1 = 6>7;
String s = "I am a student";
System.out.println("final int PRICE="+PRICE);
System.out.println("longl="+l);
System.out.println("int num="+num+"\ntotal="+total);
System.out.println("boolean truth="+truth);
System.out.println("boolean false1="+false1);
System.out.println("char c="+c);
System.out.println("float r="+r);
System.out.println("flout v="+v);
System.out.println("String s="+s);
}
// TODO 自动生成的方法存根
final int PRICE = 30;
long l = 1234l;
int num,total;
float r,v,h = 3.5f;
boolean truth = true;
boolean false1;
char c;
c = 'a';
num = 10;
total = num*PRICE;
r = 2.5f;
v = 3.14159f*r*r*h;
false1 = 6>7;
String s = "I am a student";
System.out.println("final int PRICE="+PRICE);
System.out.println("longl="+l);
System.out.println("int num="+num+"\ntotal="+total);
System.out.println("boolean truth="+truth);
System.out.println("boolean false1="+false1);
System.out.println("char c="+c);
System.out.println("float r="+r);
System.out.println("flout v="+v);
System.out.println("String s="+s);
}
}
程序运行结果
final int PRICE=30
longl=1234
int num=10
total=300
boolean truth=true
boolean false1=false
char c=a
float r=2.5
flout v=68.72228
String s=I am a student
longl=1234
int num=10
total=300
boolean truth=true
boolean false1=false
char c=a
float r=2.5
flout v=68.72228
String s=I am a student
程序二
package 关系运算符的运用;
import javax.swing.JOptionPane;
public class RelationOperator {
import javax.swing.JOptionPane;
public class RelationOperator {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String input1;
String input2;
int a,b;
boolean ee;
input1 = JOptionPane.showInputDialog("输入第一个数");
input2 = JOptionPane.showInputDialog("输入第二个数");
a = Integer.parseInt(input1);
b = Integer.parseInt(input2);
ee=(a!=b);
String s1 = a +">"+ b +"="+(a>b)+"\n";
s1 += a +"<"+ b +"="+(a<b)+"\n";
s1 += a +">+"+ b +"="+(a>=b)+"\n";
s1 += a +"<="+ b +"="+(a<=b)+"\n";
s1 += a +"!="+ b +"="+ee+"\n";
JOptionPane.showMessageDialog(null,s1,"比较运算结果",JOptionPane.PLAIN_MESSAGE);
}
// TODO 自动生成的方法存根
String input1;
String input2;
int a,b;
boolean ee;
input1 = JOptionPane.showInputDialog("输入第一个数");
input2 = JOptionPane.showInputDialog("输入第二个数");
a = Integer.parseInt(input1);
b = Integer.parseInt(input2);
ee=(a!=b);
String s1 = a +">"+ b +"="+(a>b)+"\n";
s1 += a +"<"+ b +"="+(a<b)+"\n";
s1 += a +">+"+ b +"="+(a>=b)+"\n";
s1 += a +"<="+ b +"="+(a<=b)+"\n";
s1 += a +"!="+ b +"="+ee+"\n";
JOptionPane.showMessageDialog(null,s1,"比较运算结果",JOptionPane.PLAIN_MESSAGE);
}
}
程序三
package 画图;
public class drawpicture {
public static void main(String[] args) {
// TODO 自动生成的方法存根
int i,j;
for(i=0;i<4;i++){
for(j=0;j<i;j++)
System.out.print(" ");
for(j=0;j<7-2*i;j++)
System.out.print("* ");
System.out.print("\n");
}
}
}
// TODO 自动生成的方法存根
int i,j;
for(i=0;i<4;i++){
for(j=0;j<i;j++)
System.out.print(" ");
for(j=0;j<7-2*i;j++)
System.out.print("* ");
System.out.print("\n");
}
}
}
运行结果
* * * * * * *
* * * * *
* * *
*
* * * * *
* * *
*
程序四
package 画图2;
public class drawpicture2 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
int i,j;
for(i=0;i<4;i++){
for(j=0;j<4-i;j++)
System.out.print(" ");
for(j=0;j<=2*i;j++)
System.out.print("* ");
System.out.print("\n");
}
for(i=0;i<3;i++){
for(j=0;j<i;j++)
System.out.print(" ");
for(j=0;j<9-2*i;j++)
System.out.print("* ");
System.out.print("\n");
}
}
}
// TODO 自动生成的方法存根
int i,j;
for(i=0;i<4;i++){
for(j=0;j<4-i;j++)
System.out.print(" ");
for(j=0;j<=2*i;j++)
System.out.print("* ");
System.out.print("\n");
}
for(i=0;i<3;i++){
for(j=0;j<i;j++)
System.out.print(" ");
for(j=0;j<9-2*i;j++)
System.out.print("* ");
System.out.print("\n");
}
}
}
运行结果
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
程序五
package 算术运算符的应用;
import javax.swing.JOptionPane;
public class ArithmeticOperator {
import javax.swing.JOptionPane;
public class ArithmeticOperator {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String input1;
String input2;
int a,b;
int plus;
input1 = JOptionPane.showInputDialog("输入第一个数");
input2 = JOptionPane.showInputDialog("输入第二个数");
a = Integer.parseInt(input1);
b = Integer.parseInt(input2);
plus = a + b;
String s1 = a + "+" + b + "=" +plus+"\n";
s1 += a + "*"+ b + "=" +(a * b)+"\n";
s1 += a + "/"+ b + "=" +(a/b)+"\n";
s1 += a + "%"+ b + "=" +(a%b)+"\n";
JOptionPane.showMessageDialog(null,s1,"算术运算结果",JOptionPane.PLAIN_MESSAGE);
}
// TODO 自动生成的方法存根
String input1;
String input2;
int a,b;
int plus;
input1 = JOptionPane.showInputDialog("输入第一个数");
input2 = JOptionPane.showInputDialog("输入第二个数");
a = Integer.parseInt(input1);
b = Integer.parseInt(input2);
plus = a + b;
String s1 = a + "+" + b + "=" +plus+"\n";
s1 += a + "*"+ b + "=" +(a * b)+"\n";
s1 += a + "/"+ b + "=" +(a/b)+"\n";
s1 += a + "%"+ b + "=" +(a%b)+"\n";
JOptionPane.showMessageDialog(null,s1,"算术运算结果",JOptionPane.PLAIN_MESSAGE);
}
}
程序六
package 三种循环语句;
public class Loop {
public static void main(String[] args) {
// TODO 自动生成的方法存根
int i,j=0;
int m=10,n=0;
int x=10,y=0;
System.out.print("for循环结构\n");
for(i=10;i<=50;i++){
if(i%3==0){
j++;
System.out.printf("%d ",i);
if(j%5==0)
System.out.printf("\n");
}
}
System.out.print("\nwhile循环结构\n");
while(m<=50){
if(m%3==0){
n++;
System.out.printf("%d ",m);
if(n%5==0)
System.out.printf("\n");
}
m++;
}
System.out.print("\ndo-while循环结构\n");
do{
if(x%3==0){
y++;
System.out.printf("%d ",x);
if(y%5==0)
System.out.printf("\n");
}
x++;
}while(x<=50);
}
}
// TODO 自动生成的方法存根
int i,j=0;
int m=10,n=0;
int x=10,y=0;
System.out.print("for循环结构\n");
for(i=10;i<=50;i++){
if(i%3==0){
j++;
System.out.printf("%d ",i);
if(j%5==0)
System.out.printf("\n");
}
}
System.out.print("\nwhile循环结构\n");
while(m<=50){
if(m%3==0){
n++;
System.out.printf("%d ",m);
if(n%5==0)
System.out.printf("\n");
}
m++;
}
System.out.print("\ndo-while循环结构\n");
do{
if(x%3==0){
y++;
System.out.printf("%d ",x);
if(y%5==0)
System.out.printf("\n");
}
x++;
}while(x<=50);
}
}
运行结果
for循环结构
12 15 18 21 24
27 30 33 36 39
42 45 48
while循环结构
12 15 18 21 24
27 30 33 36 39
42 45 48
do-while循环结构
12 15 18 21 24
27 30 33 36 39
42 45 48
12 15 18 21 24
27 30 33 36 39
42 45 48
while循环结构
12 15 18 21 24
27 30 33 36 39
42 45 48
do-while循环结构
12 15 18 21 24
27 30 33 36 39
42 45 48