码迷,mamicode.com
首页 > 编程语言 > 详细

简单的java程序

时间:2015-09-19 10:57:56      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

1.二分查找,java的输入与输出
技术分享
 1 package com;
 2 
 3 
 4 public class Firstapp{
 5     public static void main(String args[])
 6     {
 7         java.util.Scanner sc=new java.util.Scanner(System.in);
 8         int n,start,end,middle;
 9         System.out.println("请输入:");
10         n=sc.nextInt();
11         int a[]={-2,1,4,5,8,12,17,23,45,56,90,100};
12         start=0;
13         end=a.length-1;
14         middle=(start+end)/2;
15         int count=0;
16         while(n!=a[middle]){
17             if(n>a[middle]){
18                 start=middle;
19             }
20             else if(n<a[middle]){
21                 end=middle;
22             }
23                 middle=(start+end)/2;
24                 count++;
25                 if(count>a.length/2)    
26                     break;
27             }
28         if(count>a.length/2)
29             System.out.println(":"+n+"不在数组内");
30         else
31             System.out.println(":"+n+"是数组中第"+middle+"个元素");
32         
33         }
34     }
View Code

 2.java的类与对象,new对对象进行初始化

技术分享
 1 package com;
 2 class XiyouRenwu { float height,weight;
 3 String head, ear,hand,foot,mouth;
 4 void speak(String s)
 5 { 
 6    head="歪着头";
 7    System.out.println(s);
 8 }
 9 }
10 
11 public class Exmple {    
12     public static void main(String args[])
13     {XiyouRenwu zhubajie,sunwukong;
14     zhubajie=new XiyouRenwu();
15     sunwukong=new XiyouRenwu();
16     zhubajie.height=1.80f;                                //对象给自己的变量赋值。
17     zhubajie.weight=160f;      
18     zhubajie.hand="两只黑手";
19     zhubajie.foot="两只大脚"; 
20     zhubajie.head="大头"; 
21     zhubajie.ear="一双大耳朵"; 
22     zhubajie.mouth="一只大嘴"; 
23     sunwukong.height=1.62f;                                //对象给自己的变量赋值。
24     sunwukong.weight=1000f;    
25     sunwukong.hand="白嫩小手";
26     sunwukong.foot="两只绣脚"; 
27     sunwukong.head="绣发飘飘"; 
28     sunwukong.ear="一对小耳";  
29     sunwukong.mouth="樱桃小嘴";
30     System.out.println("zhubajie的身高:"+zhubajie.height);
31     System.out.println("zhubajie的头:"+zhubajie.head);
32     System.out.println("sunwukong的重量:"+sunwukong.weight);
33     System.out.println("sunwukong的头:"+sunwukong.head);
34     zhubajie.speak("俺老猪我想娶媳妇");                       //对象调用方法。
35     System.out.println("zhubajie现在的头:"+zhubajie.head);
36     sunwukong.speak("老孙我重1000斤,我想骗八戒背我");         //对象调用方法。
37     System.out.println("sunwukong现在的头:"+sunwukong.head);
38 
39     }
40 
41 }
View Code

 

简单的java程序

标签:

原文地址:http://www.cnblogs.com/to-creat/p/4820976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!