標題:
[回家作業] TQC +310 正偶數相加
[打印本頁]
作者:
周政輝
時間:
2017-2-11 11:57
標題:
[回家作業] TQC +310 正偶數相加
輸入一個數值 利用do while 來進行累加
作者:
蔡庭豪
時間:
2017-2-11 12:06
import java.util.Scanner;
public class JPA03 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
int total=0;
do {
System.out.println("請輸入n的值<n>0,且為偶數>:");
int n = keyboard.nextInt();
if(n%2==0&&n>0){
for(int i=2;i<=n;i+=2){
total+=i;
}
System.out.println("2+....+"+n+"="+total);
break;
}else{
System.out.println("請輸入n的值<n>0,且為偶數>:");
n = keyboard.nextInt();
}
}while(true);
}
}
複製代碼
作者:
蔡季樺
時間:
2017-2-15 20:41
import java.util.Scanner;
public class JPA03 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
int n;
do {
System.out.print("請輸入n的值(n>0,且為偶數):");
n = keyboard.nextInt();
} while (n<=0 || n%2==1);
System.out.printf("2+4+...+%d=%d",n,(2+n)*n/4);
}
}
複製代碼
作者:
王彥甯
時間:
2017-2-16 02:39
import java.util.Scanner;
public class JPA03 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
int n;
do
{
System.out.print("請輸入n的值(n>0 , 且為偶數):");
n = keyboard.nextInt();
if(n>0 && n%2==0)
{
int i = n/2;
int count = (2+n)*i;
int total = count/2;
System.out.printf("2+4+...+%d=%d",n,total);
break;
}
else if(n<=0 || n%2==1)
{
continue;
}
}while(true);
}
}
複製代碼
作者:
張健勳
時間:
2017-3-2 19:55
import java.util.Scanner;
public class JPA03 {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
int n=0;
do{
System.out.println("請輸入n的值 ( n>0 , 且為偶數)");
keyboard.nextInt();
}while (n<=0 || n%2==1);
System.out.println("2+4+..."+n+"="+n*(2+n)/4);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2