for循環之初學者N多算法小練習
-
顯示1到100的數,每行顯示5個。
-
for (int i=1;i<=100;i++){
-
if (i%5==0){
-
System.out.print("\t"+i+"\n");
-
}else{
-
System.out.print("\t"+i);
-
}
-
}
-
-
顯示三位數中可以被8整除的數,每行顯示8個。
-
int j=0;
-
for (int i=100;i<1000;i++){
-
if (i%8==0){
-
System.out.print("\t"+i);
-
j++;
-
if (j==8){
-
j=0;
-
System.out.println();
-
}
-
}
-
}
-
-
輸入10個數,統計偶數個數及其平均數。
-
Scanner sc=new Scanner(System.in);
-
int j=0,z=0;
-
int temp=0;
-
double count1=0,count2=0;
-
System.out.println("請輸入10個數:");
-
for (int i=0;i<10;i++){
-
try {
-
System.out.print("輸入第"+(i+1)+"個數:");
-
temp=sc.nextInt();
-
}catch (Exception e){
-
System.out.println("程序錯誤");
-
break;
-
}
-
if (temp<0){
-
System.out.println("請輸入正整數!\n程序結束!");
-
break;
-
}else if (temp%2==0){
-
count1=count1+temp;
-
j++;
-
}else{
-
count2=count2+temp;
-
z++;
-
}
-
}
-
if (j==0){
-
System.out.println("共有"+j+"個奇數,平均值為:0");
-
}else{
-
System.out.println("共有"+j+"個偶數,平均值位:"+(count1/j));
-
}
-
if (z==0){
-
System.out.println("共有"+z+"個奇數,平均值為:0");
-
}else{
-
System.out.println("共有"+z+"個奇數,平均值為:"+(count2/z));
-
}
-
-
打印乘法口訣表
-
for (int i=1;i<10;i++){
-
for (int j=1;j<=i;j++){
-
System.out.print(j+"*"+i+"="+(i*j)+"\t");
-
}
-
System.out.println();
-
}
-
-
從鍵盤輸入3個數,並將它們排序。
-
Scanner sc=new Scanner(System.in);
-
int n1=sc.nextInt();
-
int n2=sc.nextInt();
-
int n3=sc.nextInt();
-
if(n1<n2){
-
int temp=n1;
-
n1=n2;
-
n2=temp;
-
}
-
if (n1<n3){
-
int temp=n1;
-
n1=n3;
-
n3=temp;
-
}
-
if (n2<n3){
-
int temp=n2;
-
n2=n3;
-
n3=temp;
-
}
-
System.out.println(n1+" "+n2+" "+n3);
-
-
輸入一個數判斷是不是質數
-
Scanner sc=new Scanner(System.in);
-
int n= sc.nextInt();
-
int j=0;
-
for (int i=1;i<=n;i++){
-
if (n%i==0){
-
j++;
-
}
-
}
-
if(j==2){
-
System.out.println("質數");
-
}else{
-
System.out.println("和數");
-
}
-
-
從三位數中找出符合"abc=a^3+b^3+c^3"條件的數。
-
for (int i=100;i<1000;i++){
-
int a=i/100;
-
int b=(i-(a*100))/10;
-
int c=i-a*100-b*10;
-
-
if ((a*a*a+b*b*b+c*c*c)==i){
-
System.out.println(i);
-
}
-
}
-
-
從三位數中找出符合 他本身等於他所有約數的和 條件的數
-
for (int i=1;i<1000;i++){
-
int count=0;
-
for (int j=1;j<i;j++){
-
if (i%j==0){
-
count=count+j;
-
}
-
}
-
if (count==i){
-
System.out.println("\n===========");
-
System.out.print("--"+i+"=1");
-
for (int z=2;z<i;z++){
-
if (i%z==0){
-
System.out.print("+"+z);
-
}
-
}
-
}
-
}
-
-
計算1到100相加的和。
-
int sum=0;
-
for (int i=1;i<=100;i++){
-
sum+=i;
-
}
-
System.out.println(sum);
-
-
計算三位數中能被7除盡數的和
-
int sum=0;
-
for(int i=100;i<1000;i++){
-
if (i%7==0){
-
sum+=i;
-
}
-
}
-
System.out.println(sum);
-
-
計算1到100相乘的積。
-
BigInteger b=new BigInteger("1");
-
//double sum=1;
-
for (int i=1;i<=100;i++){
-
//sum=sum*i;
-
b=b.multiply(new BigInteger(""+i));
-
}
-
System.out.println(b);
-
-
將三位數中的質數按每5個一行輸出。
-
int c=0;
-
for (int i=100;i<1000;i++){
-
int count=0;
-
for (int j=1;j<=i;j++){
-
if (i%j==0){
-
count=count+1;
-
}
-
}
-
if (count==2){
-
System.out.print(i+"\t");
-
c++;
-
if (c==5){
-
System.out.println();
-
c=0;
-
}
-
}
-
}
-
-
計算從1累加到和為3003時的次數
-
int sum=0;
-
for (int i=1;;i++){
-
sum+=i;
-
if (sum==3003){
-
System.out.println(i+"--"+sum);
-
break;
-
}
-
}
-