不是素数,反之是素数。
public class lianxi02 public static void mainString args int count 0 forint i101 ilt200 i2 boolean b false forint j2 jltMath.sqrti j ifi j 0 b false break else b true ifb true count System.out.printlni System.out.println quot素数个数是: quot count【程序 3】题目:打印出所有的 quot水仙花数 quot,所谓 quot水仙花数 quot是指一个三位数,其各位数字立方和等于该数本身。
例如:153 是一个 quot水仙花数 quot,因为 1531 的三次方+5 的三次方+3 的三次方。
public class lianxi03 public static void mainString args int b1 b2 b3 forint m101 mlt1000 m b3 m / 100 b2 m 100 / 10 b1 m 10 ifb3b3b3 b2b2b2 b1b1b1 m System.out.printlnmquot是一个水仙花数quot 【程序 4】题目:将一个正整数分解质因数。
例如:输入 90打印出 902335。
程序分析:对 n 进行分解质因数,应先找到一个最小的质数 k,然后按下述步骤完成:1如果这个质数恰等于 n,则说明分解质因数的过程已经结束,打印出即可。
2如果 n ltgt k,但 n 能被 k 整除,则应打印出 k 的值,并用 n 除以 k 的商作为新的正整数你 n重复执行第一步。
3如果 n 不能被 k 整除,则用 k1 作为 k 的值重复执行第一步。
import
java.util.public class lianxi04 public static void mainString args Scanner s new ScannerSystem.in System.out.print quot请键入一个正整数: quot int n s.nextInt int k2 System.out.printn quotquot whilek lt n ifk n System.out.printlnnbreak else if n k 0 System.out.printk quotquotn n / k else k 【程序 5】题目:利用条件运算符的嵌套来完成此题:学习成绩gt 90 分的同学用 A 表示,60-89 分之间的用 B 表示,60 分以下的用 C 表示。
import
java.util.public class lianxi05 public static void mainString args int x char grade Scanner s new ScannerSystem.in System.out.print quot请输入一个成绩: quot x s.nextInt grade x gt 90 A : x gt 60 B :C System.out.printlnquot等级为:quotgrade【程序 6】题目:输入两个正整数 m 和 n,求其最大公约数和最小公倍数。
/在循环中,只要除数不等于 0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为 0,返回较大的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。
/import
java.util.public class lianxi06 public static void mainString args int a bmScanner s new ScannerSystem.inSystem.out.print quot键入一个整数: quota s.nextIntSystem.out.print quot再键入一个整数: quotb s.nextInt deff cd new deff m cd.deffab int n a b / m System.out.printlnquot最大公约数: quot m System.out.printlnquot最小公倍数: quot nclass deffpublic int deffint x int y int t ifx lt y t x x y y t whiley 0 ifx y return x else int k x y x y y k return x【程序 7】题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
import
java.util.public class lianxi07 public static void mainString args int digital 0int character 0int other 0int blank 0 char ch null Scanner sc new ScannerSystem.in String s sc.nextLine ch s.toCharArray forint i0 iltch.length i ifch gt 0 ampamp ch lt 9 digital else ifch gt a ampamp ch lt z ch gt A ampamp ch lt Z character else ifch blank else other System.out.printlnquot数字个数: quot digital System.out.printlnquot英文字母个数: quot character System.out.printlnquot空格个数: quot blank System.out.printlnquot其他字符个数:quot other 【程序 8】题目:求 saaaaaaaaaaaa...a 的值,其中 a 是一个数字。
例如 222222222222222此时共有 5 个数相加,几个数相加有键盘控制。
import
java.util.public class lianxi08 public static void mainString args long a b 0 sum 0 Scanner s new ScannerSystem.in System.out.printquot输入数字 a 的值: quot a s.nextInt System.out.printquot输入相加的项数:quot int n s.nextInt int i 0 whilei lt n b b a sum sum b a a 10 i System.out.printlnsum【程序 9】题目:一个数如果恰好等于它的因子之和,这个数就称为 quot完数 quot。
例如 61+2+3.编程 找出 1000 以内的所有完数。
public class lianxi09 public static void mainString args System.out.printlnquot1 到 1000 的完数有: quot forint i1 ilt1000 i int t 0 forint j1 jlt i/2 j ifi j 0 t t j ift i System.out.printi quot quot 【程序 10】题目: 一球从 100 米高度自由落下, 每次落地后反跳回原高度的一半;再落下,求它在 第10 次落地时,共经过多少米?第 10 次反弹多高?public class lianxi10 public static void mainString args double h 100s 100 forint i1 ilt10 i s s h h h / 2 System.out.printlnquot经过路程:quot s System.out.printlnquot反弹高度:quot h / 2【程序 11】 有 2、 4题目: 1、 3、 四个数字, 能组成多少个互不相同且无重复数字的三位数?都是多少?public class lianxi11 public static void mainString args int count 0 forint x1 xlt5 x forint y1 ylt5 y forint z1 zlt5 z ifx y ampamp y z ampamp x z count System.out.printlnx100 y10 z System.out.printlnquot共有quot count quot个三位数quot【程序 12】题目:企业发放的奖金根据利润提成。
利润I低于或等于 10 万元时,奖金可提 10;利润高于 10 万元,低于 20 万元时,低于 10 万元的部分按 10提成,高于 10 万元的部分,可可提成 7.5;20 万到 40 万之间时,高于 20 万元的部分,可提成 5;40 万到 60 万之间时高于 40 万元的部分, 可提成 3; 万到 100 万之间时, 60 高于 60 万元的部分,可提成 1.5,高于 100 万元时,超过 100 万元的部分按 1提成,从键盘输入当月利润,求应发放奖金总数?import
java.util.public class lianxi12 public static void mainString args double x 0y 0 System.out.printquot输入当月利润(万) :quot Scanner s new ScannerSystem.in x s.nextInt ifx gt 0 ampamp x lt 10 y x 0.1 else ifx gt 10 ampamp x lt 20 y 10 0.1 x - 10 0.075 else ifx gt 20 ampamp x lt 40 y 10 0.1 10 0.075 x - 20 0.05 else ifx gt 40 ampamp x lt 60 y 10 0.1 10 0.075 20 0.05 x - 40 0.03 else ifx gt 60 ampamp x lt 100 y 20 0.175 20 0.05 20 0.03 x - 60 0.015 else ifx gt 100 y 20 0.175 40 0.08 40 0.015 x - 100 0.01 System.out.printlnquot应该提取的奖金是 quot y quot万quot【程序 13】题目:一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平方数,请问该数是多少?public class lianxi13 public static void mainString args forint x 1 xlt100000 x ifMath.sqrtx100 1 0 ifMath.sqrtx268 1 0 System.out.printlnx quot加 100 是一个完全平方数,再加 168 又是一个完全平方数quot /按题意循环应该从-100 开始(整数包括正整数、负整数、零) ,这样会多一个满足条件的数-99。
但是我看到大部分人解这道题目时都把题中的“整数”理解成正整数,我也就随大流了。
/【程序 14】题目:输入某年某月某日,判断这一天是这一年的第几天?import
java.util.public class lianxi14 public static void mainString args int year month day int days 0 int d 0 int e input fymd new input do e 0 System.out.printquot输入年:quot year fymd.input System.out.printquot输入月:quot month fymd.input System.out.printquot输入天:quot day fymd.input if year lt 0 month lt 0 month gt 12 day lt 0 day gt 31 System.out.printlnquot输入错误,请重新输入!quot e1 while e1 for int i1 i ltmonth i switch i case 1: case 3: case 5: case 7: case 8: case 10: case 12: days 31 break case 4: case 6: case 9: case 11: days 30 break case 2: if year 400 0 year 4 0 ampamp year 100 0 days 29 else days 28 break d days System.out.printlnyear quot-quot month quot-quot day quot是这年的第quot dday quot天。
quotclass inputpublic int input int value 0 Scanner s new ScannerSystem.in value s.nextInt return value【程序 15】题目:输入三个整数 xyz,请把这三个数由小到大输出。
import
java.util.public class lianxi15 public static void mainString args input fnc new input int x0 y0 z0 System.out.printquot输入第一个数字:quot x fnc.input System.out.printquot输入第二个数字:quot y fnc.input System.out.printquot输入第三个数字:quot z fnc.input ifx gt y int t x x y y t ifx gt z int t x x z z t ify gt z int t y y z z t System.out.println quot三个数字由小到大排列为: quotx quot quot y quot quot zclass inputpublic int input int value 0 Scanner s new ScannerSystem.in value s.nextInt return value【程序 16】题目:输出 99 口诀。
public class lianxi16 public static void mainString args forint i1 ilt10 i forint j1 jlti j System.out.printj quotquot i quotquot ji quot quot ifjilt10System.out.printquot quot System.out.println 【程序 17】题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。
以后每天早上都吃了前一天剩下 的一半零一个。
到第 10 天早上想再吃时, 求第一天共摘了多少。
见只剩下一个桃子了。
public class lianxi17 public static void mainString args int x 1 forint i2 ilt10 i x x12 System.out.printlnquot猴子第一天摘了 quot x quot 个桃子quot【程序 18】题目:两个乒乓球队进行比赛,各出三人。
甲队为 abc 三人,乙队为 xyz 三人。
已抽签决定比赛名单。
有人.
上一篇:
java简单聊天小程序
下一篇:
多年来只想说一句,我不怪你