JAVA学习初步

程序一
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);
 }

}

程序运行结果
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

程序二
package 关系运算符的运用;
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);
 }

}

程序三
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");
  }
 }
}

运行结果
* * * * * * *
  * * * * *
    * * *
      *

程序四
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");
  }
 }
}

运行结果
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
  * * * * * * *
    * * * * *

程序五
package 算术运算符的应用;
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);
 }

}

程序六
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);
 }
}
运行结果
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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值