1.100以内的质数
public static void main(String[] args)
{
方法一:
System.out.println("质数有:");
int count=1;
for (int i=2;i<=100;i++)
{
int j;
for (j=2;j<i;j++)
{
if (i%j ==0)
break;
}
if (j>=i)
{
System.out.print(i + "\t");
if (count % 5 == 0)
{
System.out.println();
}
}
count++;
}
方法二:
int count=0;
boolean sum=true;
for (int i=2;i<=100;i++)
{
int j;
for (j=2;j<=Math.sqrt(i);j++)
{
if (i%j ==0)
{
sum=false;
break;
}
else {
sum=true;
}
}
if (sum) {
++count;
System.out.println(i);
}
}
System.out.println("总个数为:"+count);
2.求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
程序分析:关键是计算出每一项的值。
Scanner scanner = new Scanner(System.in);
System.out.println("请输入a的值");
int a = scanner.nextInt();
System.out.println("请输入总项数");
// int b = scanner.nextInt();
方法一:
// double d = 0;
// double e = 0;
// for (int i = 1; i <=b; i++) {//控制项数
// for (int j = i-1; j >=0; j--) {//得出每一项的和
// double c = a*Math.pow(10, j);
// d+=c;
// }
// }
// e+=d;
// System.out.println(e);
方法二:
// double sum = 0;
// for (int i = 1; i <= b ; i++) {
// sum+=a*((Math.pow(10, i)-1)/9);
// }
// System.out.println(sum);
方法三:
// double sum = 0;
// double d = 0;
// for (int i = 1; i <=b ; i++) {
// d=d*10+a;
// sum+=d;
// }
// System.out.println(sum);
方法四:
// int sum = 0;
// int c = a;
// for (int i = 1; i <= b; i++) {
// sum +=a;
// c = c*10;
// a+=c;
// }
// System.out.println(sum);
}
}
3.利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
// public static void main(String[] args) {
// int a;
// System.out.println("请输入本次成绩:\n");
// Scanner cj=new Scanner(System.in);
// a=cj.nextInt();
// if (a>=90)
// {
// System.out.println("A");
// }
// else if(90>a&a>=60)
// {
// System.out.println("B");
// }
// else if(a<60)
// {
// System.out.println("C");
// }
// }
4.输入三个正数,判断能否组成三角型
// public static void main(String[] args)
// {
// int a;
// int b;
// int c;
// System.out.println("请输入三个正数:");
// Scanner in=new Scanner(System.in);
// a=in.nextInt();
// b=in.nextInt();
// c=in.nextInt();
// if(a<=0||b<=0||c<=0)
// {
// System.out.println("输入的必须是正数!");
// }
// if((a+b)>c&&(a+c)>b&&(b+c)>a)
// {
// System.out.println("能够组成三角形!");
// }
// else
// {
// System.out.println("不能组成三角形!");
// }
// }
5.输入两个正整数,求最大公约数和最小公倍数
// public static void main(String[] args)
// {
// int a;
// int b;
// Scanner s=new Scanner(System.in);
// System.out.println("请输入一个数:\n");
// a=s.nextInt();
// System.out.println("请再输入一个数:\n");
// b=s.nextInt();
// if (a>0&&b>0) {
// if (a<b) {
// int m=a;
// a=b;
// b=m;
// }
// for (int i = b; ; i--) {
// if(a%i==0&&b%i==0){
// System.out.println("最大公约数为:"+i);
// System.out.println("最小公倍数为:"+a*b/i);
// break;
// }
// }
// }
// }
6.1、2、3、4 能组成多少个不重复的3位数
// public static void main(String[] args)
// {
1、2、3、4 能组成多少个不重复的3位数
// int count=0;
// for(int i=1;i<5;i++)
// {
// for (int j = 1; j < 5; j++)
// {
// for (int j2 = 1; j2 < 5; j2++)
// {
// if (i!=j&&i!=j2&&j!=j2) {
// count++;
// System.out.print(i*100+j*10+j2+" ");
// }
// }
//
// }
// }
// }
7.输入年月日,输出这是这一年的第几天
// public static void main(String[] args)
// {
// int year,month,day;
// Scanner s=new Scanner(System.in);
// System.out.println("请输入年份:");
// year=s.nextInt();
// System.out.println("请输入月份:");
// month=s.nextInt();
// System.out.println("请输入日期:");
// day=s.nextInt();
// int count=0;
// boolean b=false;
// if (year%4==0&&year%100!=0||year%400==0)
// {
// b=true;
// }
// switch (month-1) {
// case 11:
// count+=30;
// case 10:
// count+=31;
// case 9:
// count+=30;
// case 8:
// count+=31;
// case 7:
// count+=31;
// case 6:
// count+=30;
// case 5:
// count+=31;
// case 4:
// count+=30;
// case 3:
// count+=31;
// case 2:
// if(b){
// count+=29;
// }else{
// count+=28;
// }
// case 1:
// count+=31;
// }
// System.out.println("这一天是这一年的第"+(count+day)+"天");
// }
8.打印三角形
// public static void main(String[] args) {
//
// Scanner input=new Scanner(System.in);
// System.out.println("请输入数字:");
// int num=input.nextInt();
//打印三角形-----》直角(尖向上)
//分析:嵌套的for循环,外层控制行数,里层控制*个数
// for (int i = 1; i <= num; i++) {
// for (int j = 1; j <=i; j++) {
// System.out.print("*");
// }
// System.out.println("");
// }
//打印三角形-----》直角(尖向下)
// for (int i = num; i >0; i--) {
// for (int j =i; j >0; j--) {
// System.out.print("*");
// }
// System.out.println("");
// }
//打印等腰三角形(尖向上)
// for (int i=1; i<=num;i++) {
// for (int j=num;j>i;j--) {
// System.out.print(" ");
// }
// for (int j=1;j<=i*2-1;j++) {
// System.out.print("*");
// }
// System.out.println();
// }
//打印等腰三角形(尖向下)
// for(int i=1; i<=num; i++){
// for(int j=1; j<=i-1; j++){
// System.out.print(" ");
// }
// for(int j2=1; j2<= num+1-i; j2++)
// {
// System.out.print("* ");
// }
// System.out.println();
// }
//打印空心三角形
for (int i = 1; i <= num; i++) {
// for (int j = num; j > i ; j--) {
// System.out.print(" ");
// }
// for (int j = 1; j <= i*2-1; j++) {
// if(j==1||j == i*2-1||i ==num){
// System.out.print("*");
// }else{
// System.out.print(" ");
// }
// }
// System.out.println();
// }
// }
}