zoukankan      html  css  js  c++  java
  • 第2次Java作业+LSYang

    【P62 1】打印出1~10000范围中所有的“水仙花数”

     1 public class JavaA1{
     2     public static void main(String args[]){
     3         int i=0, m = 100, n = 999, a = 0, b = 0, c = 0, d = 0;
     4         for (i = m; i <= n; i++)
     5         {
     6             a = i / 100;
     7             b = (i / 10) % 10;
     8             c = i % 10;
     9             if (i==a*a*a + b*b*b + c*c*c)
    10                 System.out.print(i+"\t");
    11         }
    12     }
    13 }

    程序运行结果

     ↓ 使用方法返回

     1 public class JavaA2{
     2     public static void main(String args[]) {
     3         for (int i = 100; i <= 999; i++) {
     4             if (isNum(i)) {
     5                 System.out.print(i + " ");
     6             }
     7         }
     8     }
     9  
    10     public static boolean isNum(int n) {
    11         int a = n / 100; //百位
    12         int b = n % 100 / 10;//十位
    13         int c = n % 10;//个位
    14         return n == a * a * a + b * b * b + c * c * c;
    15     }
    16 }

    程序运行结果

    ✎ 水仙花数”是指一个3位数其各位数字立方和等于该数本身。因为是三位数,所以不用从1循环到10000,直接从100到999。

    【P62 6】求13-23+33-43+…+973-983+993-1003的值

     1 public class JavaA6{
     2     public static void main(String args[]){
     3         int i=1,sum=0;
     4         for(i=1;i<=100;i++){
     5             if(i%2!=0){
     6                 sum+=i*10+3;
     7             }
     8             else{
     9                 sum-=i*10+3;
    10             }
    11         }
    12     System.out.println("值为"+sum);
    13     }
    14 }

    程序运行结果

    【P62 10】求1~1000之间可以同时被3、 5、 7整除的数字。

    1 public class JavaA10{
    2     public static void main(String args[]){
    3         for(int i=1;i<=1000;i++){
    4             if(i%3==0&&i%5==0&&i%7==0)
    5                 System.out.println("i="+i);
    6         }
    7     }
    8 }

    程序运行结果

    【P62 11】求1!+2!+3!+…+20!的值

     1 public class JavaA11{
     2     public static void main(String args[]){
     3         int sum=0, item=1, i, j;
     4         for (i = 1; i <= 20; i++)
     5         {
     6             for (j = 1; j <= i; j++)
     7             {
     8                 item = j*j;
     9             }
    10             sum = sum + item;
    11         }
    12     System.out.println("1!+2!+3!+…+20!="+sum);
    13     }
    14 }

    程序运行结果

     【P89 4】定义一个整型数组,求出数组元素的和、最大值和最小值。

     1 public class JavaA4{
     2     public static void main(String args[]){
     3         int num[] = {67,89,87,69,90,100,75,90} ;        
     4         int max = 0 ,min = 0 ,sum = 0;                        
     5         max = min = num[0] ;                
     6         for(int i=0;i<num.length;i++){        
     7             if(num[i]>max){                
     8                 max = num[i] ;            
     9             }
    10             if(num[i]<min){                
    11                 min = num[i] ;            
    12             }
    13             sum+=num[i];
    14         }
    15         System.out.println("和:" + sum) ;
    16         System.out.println("最大值:" + max) ;
    17         System.out.println("最小值:" + min) ;
    18     }
    19 };

    程序运行结果

    【P89 8】有30个0~9之间的数字,分别统计0~9这10个数字分别出现了多少次。

     1 public class JavaA5{
     2         public static void main(String args[]){        
     3                 int score[]={3,8,7,5,7,0,0,3,3,8,7,6,8,0,1,8,2,1,2,3,6,0,1,9,6,8,9,0,3,5};      
     4                 int count[]=new int[10];      
     5                 int c=0;      
     6                    for(int a=0;a<10;a++){              
     7                         for(int b=0;b<30;b++){                  
     8                                    if(score[b]==a){
     9                                       count[c]++;   
    10                                 }
    11                            }               
    12                         c++;              
    13                 }            
    14                 System.out.println("这十个数分别出现的次数为:");              
    15                 for(int d=0;d<10;d++){              
    16                         System.out.println("数字"+d+"出现的次数为"+count[d]+",");   
    17                 }  
    18         }   
    19 } 

    程序运行结果

    ↓ 使用Java Random()函数 随机生成数字

     1 public class JavaA3{
     2         public static void main(String args[]){      
     3                 java.util.Random random=new java.util.Random();      
     4                 int score[]=new int[30];      
     5                 int count[]=new int[10];      
     6                 int c=0;      
     7                 System.out.print("随机生成的30个数为:");            
     8                 for(int i=0;i<30;i++){                     
     9                     score[i]=Math.abs(random.nextInt()%10);                  
    10                     System.out.print(score[i]+","); 
    11         }            
    12                    for(int a=0;a<10;a++){              
    13                         for(int b=0;b<30;b++){                  
    14                                    if(score[b]==a){
    15                                       count[c]++;   
    16                                 }
    17                            }               
    18                         c++;              
    19                 }            
    20                 System.out.println("这十个数分别出现的次数为:");              
    21                 for(int d=0;d<10;d++){              
    22                         System.out.println("数字"+d+"出现的次数为"+count[d]+",");   
    23                 }  
    24         }   
    25 } 

    程序运行结果

    ✎JavaRandom()函数 随机生成数字用法还不是很理解

    参考资料:http://www.cnblogs.com/ningvsban/p/3590722.html

    【P173 1】编写并测试一个代表地址的Address类,地址信息由国家、省份、城市、街道、邮编组成,并可以返回完整的地址信息。

     1 class Address{
     2     String country;
     3     String province;
     4     String city;
     5     String street;
     6     String zipcode;
     7     public void tell(){
     8         System.out.println("国家:"+country+"\n"+"省份:"+province+"\n"+"城市:"+city+"\n"+"街道:"+street+"\n"+"邮
     9 
    10 编:"+zipcode);
    11     }
    12 }
    13 public class JavaA7{
    14     public static void main(String args[])
    15     {
    16         Address per = null;
    17         per = new Address();
    18         per.country = "中国";
    19         per.province = "山东省";
    20         per.city = "青岛市";
    21         per.street = "人民路";
    22         per.zipcode = "266000";
    23         per.tell();
    24     }
    25 }

    程序运行结果

  • 相关阅读:
    增量学习中的自我训练
    半监督学习和直推学习的区别
    LeetCode: Word Break
    LeetCode: Linked List Cycle
    LeetCode: Reorder List
    LeetCode: Binary Tree Traversal
    LeetCode: LRU Cache
    LeetCode: Insertion Sort List
    LeetCode: Sort List
    LeetCode: Max Points on a Line
  • 原文地址:https://www.cnblogs.com/liusiyang1126/p/5272993.html
Copyright © 2011-2022 走看看