---輸出1~100


public class t0521 {
public static void main(String args[]){

int i=0;
for(i=1;i<101;i++){

System.out.println(i);
}
}
}

---1~100加總

 

public class t0521 {
public static void main(String args[]){

int i=0,sum=0;
for(i=0;i<100;i++){
sum +=i;
}
System.out.println(sum);

}
}

------規律加總

1+2-3+4+5-6+7+8-9......+31


(3n+1)
---


public class t0521 {
public static void main(String args[]){

int i,sum=0;

for( i=1;i<=31;i++){
if(i%3==1)sum +=i ;
if(i%3==2)sum +=i ;
if(i%3==0)sum -=i ;
}
System.out.println(sum);

}

}

------規律加總

11+12+13-14-15+16+17+18-19-20+......+38

---

public class t0521 {
public static void main(String args[]) {
int i, sum = 0;
for (i = 11; i <= 38; i++) {
if (i % 5 == 1)
sum += i;
if (i % 5 == 2)
sum += i;
if (i % 5 == 3)
sum += i;
if (i % 5 == 0)
sum -= 1;
if (i % 5 == 4)
sum -= i; //sum=sum-1;
}
System.out.println(sum);
}
}

---印出陣列內容

public class t0521 {
public static void main(String args[]) {

int a[]= {10,50,80,60,20};
for(int i=0;i<a.length;i++){
System.out.println(a[i]);
}

}
}

---陣列內加總

public class t0521 {
public static void main(String args[]) {
int sum=0;
int a[]= {10,50,80,60,20};
for(int i=0;i<a.length;i++){

sum=sum+a[i];

System.out.println(sum);
}

}
}
---鍵盤輸入

import java.io.*;

public class t0521 {
public static void main(String args[])throws Exception {
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));

int sum=0;
int a[]= new int [5];
for(int i=0;i<a.length;i++){
System.out.println("請輸入數值");
a[i]=Integer.parseInt(br.readLine());
}
for(int i=0;i<a.length;i++){
System.out.println(a[i]);

sum =sum +a[i];
}
System.out.println(sum);
}

}

---

 

 

 

 

------------------------------印出元素

import java.io.*;
import java.util.Scanner;

public class t0521 {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner S = new Scanner(System.in);

String str = "1,12,456,789";
String c[] = str.split(",");
for (String a : c)
System.out.println(a);
// c為陣列,a為陣列的元素

int sum = 0;
int a[] = new int[5];
for (int i = 0; i < a.length; i++) {
System.out.println("請輸入數值");
a[i] = Integer.parseInt(br.readLine());

}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);

sum = sum + a[i];
}
System.out.println(sum);
}

}


---


import java.io.*;
import java.util.Scanner;

public class t0521 {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner S = new Scanner(System.in);

String str = "1,,12,,456,,789";
String b[] = str.split(",,");
// for (int i = 0; i < a.length; i++) {System.out.println(b[i]);

for (String d:b)
System.out.println(d); //d=b[i]
// c為陣列,a為陣列的元素

int sum = 0;
int a[] = new int[5];
for (int i = 0; i < a.length; i++) {
System.out.println("請輸入數值");
a[i] = Integer.parseInt(br.readLine());

}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);

sum = sum + a[i];
}
System.out.println(sum);
}

}

 

 

---quit

import java.io.*;
import java.util.Scanner;

public class t0521 {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner S = new Scanner(System.in);


// String str = "1,,12,,456,,789";
// String b[] = str.split(",,");
// for (int i = 0; i < a.length; i++) {System.out.println(b[i]);

// for (String d:b)
// System.out.println(d); //d=b[i]
// c為陣列,a為陣列的元素

String a[] = {"週日,週一,週二,週三,週四,週五,週六"};
String sum ="" ;
for (int i = 0; i < a.length; i++) {
System.out.println("請輸入數值");
i=Integer.parseInt(br.readLine());

if(i==0)
sum="週日";
if(i==1)
sum="週一";
if(i==2)
sum="週二";
if(i==3)
sum="週三";
if(i==4)
sum="週四";
if(i==5)
sum="週五";
if(i==6)
sum="週六";

}

System.out.println(sum);}

}

 

 

 

玥君 發表在 痞客邦 PIXNET 留言(0) 人氣()