java实例2


import java.util.*;
public class app{
        public static void main(String[] args){
                Scanner reader=new Scanner(System.in);
                        System.out.print("input many numbers:");
                        int sum=0;
                        int n=0;
                        while(reader.hasNextDouble()){
                                double x=reader.nextDouble();
                                sum+=x;
                                n++;
                        }
                        System.out.println("sum=: "+sum+"  n="+n);
        }
}

/// CTRL+D结束输入。。

结果:input many numbers:1 2 3 4 5 sum=: 15  n=5


一定要明确输入数字的基本类型,否则输入while()不能循环。

证明:将上例Double改为Int

import java.util.*;
public class app{
        public static void main(String[] args){
                Scanner reader=new Scanner(System.in);
                        System.out.print("input many numbers:");
                        int sum=0;
                        int n=0;
                        while(reader.hasNextInt()){
                                int x=reader.nextInt();
                                sum+=x;
                                n++;
                        }
                        System.out.println("sum=: "+sum+"  n="+n);
        }
}

结果:因为输入的数字不是int型,所以while()循环不成立,计数为0.
input many numbers:1.1 1.1 1.1
sum=: 0  n=0



——————

例子:输入n,求累加和。

import java.util.*;
public class app{
        public static void main(String[] args){
                Scanner reader=new Scanner(System.in);
                int n=0;
                int i=0;
                int sum=0;
                do{
                        System.out.print("input n=");
                        n=reader.nextInt();
                }while(n<0);    //如果输入的数为负数,那么会一直循环输出  输入语句。
                while(i<=n)
                {
                        sum=sum+i;
                        i++;
                }
                System.out.println("1+2+3+...+"+n+"="+sum);
        }
}

结果:

input n=-1
input n=-1
input n=-1
input n=0
1+2+3+...+0=0

结果:

input n=1
1+2+3+...+1=1

input n=3
1+2+3+...+3=6

————

例子 :求两个数的最大公约数。

import java.util.*;
public class app{
        public static void main(String[] args){
                Scanner reader=new Scanner(System.in);
                int a,b,k;
                do{
                System.out.print("a=");
                a=reader.nextInt();
                }while(a<=0);
                do{
                System.out.print("b=");
                b=reader.nextInt();
                }while(b<=0);
                do{
                        k=a%b;
                        a=b;
                        b=k;
                }while(k!=0);
                System.out.println("greatest common divisor="+a);
        }
}

结果:

a=0
a=0
a=12
b=-1
b=18
greatest common divisor=6

a=18
b=12
greatest common divisor=6

——————

import java.util.*;
public class app{
        public static void main(String[] args){
                Scanner reader=new Scanner(System.in);
                int n,result=1,i=1;
                do{
                        System.out.print("input a positive integer:");
                        n=reader.nextInt();
                }while(n<=0);
                while(i<=n)
                {
                        result*=i;
                        i++;
                }
                System.out.println(n+"!="+result);
        }
}

结果:

input a positive integer:4
4!=24


import java.util.*;
public class app{
        public static void main(String[] args){
                Scanner reader=new Scanner(System.in);
                int n,result=1,i=1;
                do{
                        System.out.print("input a positive integer:");
                        n=reader.nextInt();
                }while(n<=0);
                while(result<n)
                {
                        result*=i;
                        i++;
                }
                System.out.println("i="+(i-2)+" result="+result/(i-1));  //不理解
        }
}

结果:

input a positive integer:100
i=4 result=24

——————

例子:

import java.util.*;
public class app{
        public static void main(String[] args){
                Scanner reader=new Scanner(System.in);
                int s=0;
                for(int i=1;i<=10;i++)
                  s+=i;
                System.out.println(s);
                s=0;
                for(int i=10;i>=1;i--)
                  s+=i;
                System.out.println(s);
        }
}

结果:

55
55

——————

public class app{
        public static void main(String[] args){
                int max=100;
                int k=3;
                int n=1;
                System.out.print("2\t");
                do{
                        int j=3;
                        while(j<Math.sqrt(k)&&(k%j!=0))
                          j++;
                        if(j>Math.sqrt(k))
                        {
                                System.out.print(k+"\t");
                                n++;
                                if(n%5==0)
                                  System.out.println();
                        }
                        k=k+2;
                }while(k<100);
        }
}



结果:100以内的素数

2       3       5       7       11
13      17      19      23      29
31      37      41      43      47
53      59      61      67      71
73      79      83      89      97


——————



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值