package wang; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub int sum = 0; for (int i = 1; i <= 100; i++) { if (i % 10 == 3) continue; sum = sum + i; } System.out.println(sum); } }
2.使用二重循环输出以下图形
*
***
*****
*******
*****
****
***
**
*
package wang; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 1; i <=4; i++) { for (int j = 1; j <=4-i; j++) { System.out.print(" "); } for (int k = 1; k <=2*i-1; k++) { System.out.print("*"); } System.out.println(); } System.out.println(" "); for (int i = 1; i <=5; i++) { for (int j = 1; j <=6-i; j++) { System.out.print("*"); } System.out.println(); } } }
3.循环输入学生姓名,直到输入‘Q’程序结束。
package wang; import java.util.Scanner; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("请输入学生姓名"); while(true){ String name=input.nextLine(); if("Q".equals(name)){ System.out.println("退出录入程序"); break; } System.out.println("请输入学生姓名"); } } }
4.输入5个学生成绩,求和,当输入不合法的时候,提示输入错误重新输入。
package wang; import java.util.Scanner; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); int sum=0; int i = 0; while(i<=4) { System.out.println("请输入第"+(i+1)+"个学生的成绩"); int a=input.nextInt(); if(a<0||a>100) { System.out.println("输入错误,请重新输入"); } else{ i++; sum+=a; } } System.out.println("学生总成绩为"+sum); } }
5.计算 1+1/(1+2) + 1/(1+2+3) +1/(1+2+3+4)+…………+1/(1+2+....+10)
package wang; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub double sum=0; double a=0; for (int i = 1; i <=10; i++) { sum+=i; a+=1/sum; } System.out.println(a); } }
6.产生一个1-99的随机数,猜数字,若大了就提示“大了点”,若小了就提示“小了点”直到猜对为止。
(判断猜的次数,如果1-3次,提示太棒了 如果5-8次 一般般 如果8次以上 太笨了)
package wang; import java.util.Random; import java.util.Scanner; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub Random a=new Random(); int x= a.nextInt(100); Scanner input=new Scanner(System.in); int n= input.nextInt(); int ci=1; while (n!=x) { if (n>x) { System.out.println("大了点"); } else if (n<x) { System.out.println("小了点"); } n=input.nextInt(); if (n==x) { System.out.println("猜对了"); } ci++; } if(ci>0&&ci<=3){ System.out.println("太棒了"); } else if(ci>=4&&ci<=8){ System.out.println("一般般"); } if(ci>8){ System.out.println("太笨了"); } System.out.println("猜的次数为"+ci); System.out.println("要猜的数是"+x); } }
7.输入一个数,判断是不是质数(只能被1和自身整除的数是质数)
package wang; import java.util.Scanner; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入一个数"); int num=input.nextInt(); int a=0; int i=2; while(i<num){ if(num%i==0){ System.out.println(num+"不是质数"); a++; break; } i++; } if(a==0){ System.out.println(num+"是质数"); } } }
8.求裴波那契数列的第20项是什么。裴波那契数列,1,1,2,3,5,8,13,21……
package wang; public class wang { public static void main(String[] args) { // TODO Auto-generated method stub int a=1; int b=1; int c=0; for(int i=1;i<=18;i++){ c=a+b; a=b; b=c; } System.out.println("第20项为:"+c); } }