A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yangzhong1991 中级黑马   /  2014-10-15 16:39  /  2187 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

只能被本身和1整除的数,怎么写啊

11 个回复

倒序浏览
  1. public class Test {
  2. public static void main(String args[]) {
  3.   System.out.println(getSum(123));
  4. }

  5. public static int getSum(int num) {
  6.   int a = num % 10;
  7.   int b = (num / 10) % 10;
  8.   int c = num / 100;
  9.   return a + b + c;
  10. }
  11. }
复制代码
回复 使用道具 举报
  1. package com.ms.test;

  2. public class GetSushu {

  3.         /**
  4.          * @param args
  5.          */
  6.         public static void main(String[] args) {
  7.                 // TODO Auto-generated method stub
  8.                 for(int x=100;x<=999;x++){
  9.                         boolean flag=true;
  10.                         for(int y=2;y<x;y++){
  11.                                 if(x%y==0){
  12.                                         flag=false;
  13.                                         break;
  14.                                 }
  15.                         }
  16.                         if(flag){
  17.                                 System.out.println(x);
  18.                         }
  19.                        
  20.                 }
  21.         }

  22. }
复制代码

定义标记

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1

查看全部评分

回复 使用道具 举报
其实做这题,你要知道什么是质数就好办了,质数就是只能被自己和1整除的数。那么你将一个数拿来从1开始除,一直到它本身,如果能被除尽,则计数器加1,最后判断计数器的值就行了。

  1. class Zhishu
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 int count=0;
  6.                 for(int i=1;i<=100;i++)
  7.                 {
  8.                         count=0;
  9.                         for(int j=1;j<=i;j++)
  10.                         {
  11.                                 if(i%j==0)
  12.                                 {
  13.                                         count++;
  14.                                 }
  15.                         }
  16.                         if(count==2||count==1)
  17.                         {
  18.                                 System.out.print(i+" ");
  19.                         }

  20.                 }
  21.         }
  22. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1 赞一个!

查看全部评分

回复 使用道具 举报 1 0
哈达洋 发表于 2014-10-15 21:19
其实做这题,你要知道什么是质数就好办了,质数就是只能被自己和1整除的数。那么你将一个数拿来从1开始除, ...

活到老学到老!
回复 使用道具 举报
那个什么 合数才难理解
回复 使用道具 举报

第一个是判断某一个三位数是否是质数
  1. import java.util.Scanner;

  2. public class PrimeNum {

  3.         public static void main(String[] args) {
  4.                 Scanner sc = new Scanner(System.in);
  5.                 System.out.println("请输入一个三位数");
  6.                 int  num = sc.nextInt();
  7.                 if(num<100 || num>999)
  8.                         throw new RuntimeException("输入数据不合要求");
  9.                
  10.                 getPrime(num);
  11.         }
  12.         public static void getPrime(int num){
  13.                
  14.                 for(int x=2;x<num;x++){
  15.                         if(num%x!=0){
  16.                                 if(x==num-1){
  17.                                         System.out.println(num+"是质数");
  18.                                 }
  19.                                 continue;
  20.                         }
  21.                         else{
  22.                                 System.out.println(num+"不是质数");
  23.                                 break;
  24.                         }                                
  25.                 }               
  26.         }
  27. }
复制代码


第二个是列出所有介于100到你输入的三位数中的质数(不包含100)

  1. import java.util.Scanner;

  2. public class TotalPrimeNum {

  3.         public static void main(String[] args) {
  4.                 Scanner sc = new Scanner(System.in);
  5.                 System.out.println("请输入一个三位数");
  6.                 int  num = sc.nextInt();
  7.                 if(num<100 || num>999)
  8.                         throw new RuntimeException("输入数据不合要求");
  9.                 getPrime(num);
  10.         }
  11.         public static void getPrime(int num){
  12.                 int count = 0;//计数器记录质数个数
  13.                 W:for (int x = 101; x <= num; x++) {
  14.                         Q:for (int y = 2; y < x; y++) {
  15.                                 if(x%y!=0){
  16.                                         if(y==x-1){
  17.                                                 System.out.print(x+"\t");
  18.                                                 count++;
  19.                                                 continue W;
  20.                                         }
  21.                                         continue Q;
  22.                                 }
  23.                                 else{
  24.                                         continue W;
  25.                                 }
  26.                         }
  27.                 }
  28.                 System.out.println();
  29.                 System.out.println("101到"+num+"共有质数个数为:"+count);        
  30.         }
  31. }
复制代码

回复 使用道具 举报
韶山 高级黑马 2014-10-16 09:01:04
8#
长知识了。
回复 使用道具 举报
学习了,这么多方法
回复 使用道具 举报
用双层for循环。第一层确定100-999中的一个数,第二层再判断从1到这个数还有没有其他因数。
具体代码为:
  1. public class Test {
  2.         public static void main(String[] args){
  3.                
  4.                 //第一个for循环,检测100到999之间每一个三位数。
  5.                 for(int i=100;i<1000;i++){
  6.                        
  7.                         //定义flag。
  8.                         boolean flag=true;
  9.                        
  10.                         //第二个for循环,针对每一个i值,检测1到其本身之间有没有其它的因数,
  11.                         //如果有,将flag标记为false。
  12.                         for(int j=2;j<i;j++){
  13.                                 if(i%j==0){
  14.                                         flag=false;
  15.                                         break;
  16.                                 }
  17.                         }
  18.                        
  19.                         //如果flag为true,表明此i值除了1和其本身没有其他的因数,为质数,将其打印。
  20.                         if(flag)
  21.                                 System.out.println(i);
  22.                 }
  23.         }
  24. }
复制代码
回复 使用道具 举报
嘿~~ 发表于 2014-10-15 23:10
第一个是判断某一个三位数是否是质数

前辈,怎样发像你这样的可以复制的代码?

点评

回帖中 找到<> 复制进去  发表于 2014-10-16 18:31
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马