非固定次數循環while 和 do-whilespa
任務一: 計算成績和人數。input
由用戶輸入多個學員成績,當輸入-1時結束循環,輸出一共輸入多少人,和輸入的這些學員的總分數,不能把-1加進去。it
public class C1 { io
public static void main(String[] args) { class
int a=0,i=1;循環
int sum = 0;next
Scanner input=new Scanner(System.in);static
while (i <= 100)while
{co
System.out.println("請輸"+i+"爲成績");
a=input.nextInt();
if(a==-1)break;
else
sum+=a;
i++;
}
System.out.println((i-1)+"位同窗成績,總成績位:"+sum);
}
}
任務二:計算可樂瓶數
50瓶可樂,每喝三瓶給你一瓶,請問最後喝了多少瓶.
public class C1 {
public static void main(String[] args) {
int a=0,i=1;
int sum = 0;
while (i<= 50)
{
if(i%3==0) sum+=2;
else
sum+=1;
i ++;
}
System.out.println((i-1)+"位同窗成績,總成績位:"+sum);
}
}
任務三:
判斷一個數是不是素數。
public class C1 {
public class C1 {
public static void main(String[] args) {
int a=0,i=2;
int x=1;
Scanner input=new Scanner(System.in);
System.out.println("請輸數");
a=input.nextInt();
while(i<=a/2){
if(a%i==0){x+=1;
System.out.println("不是素數");}
i++;
break;
}
if(x==1)
System.out.println("是素數");
}
}
任務四:
輸入一批整數,使用循環求出最大值與最小值,輸入0時結束
public static void main(String[] args) {
// TODO Auto-generated method stuba
Scanner sc = new Scanner(System.in);
System.out.println("請輸入整數");
int max;
int min;
int num;
min=max=num = sc.nextInt();
while (num != 0) {
if (num < min)
min = num;
if (num > max)
max = num;
System.out.println("請繼續輸入,輸入0結束");
num = sc.nextInt();
}
System.out.println("最大值max=" + max + " 最小值min=" + min);
}
選作題:
輸入年月日,輸出此日期是一年中的第幾天,判斷平年和閏年的狀況。
使用知識點:switch、循環。
運行如圖:
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
for(int i=1;i<=3;i++){
System.out.println("請輸入年");
int a=input.nextInt();
System.out.println("請輸入月");
int b=input.nextInt();
System.out.println("請輸入日");
int c=input.nextInt();
if(a%4==0&&a%100!=0||a%400==0){
switch(b){
case 1:System.out.println("今天是今年的第"+c);break;
case 3:System.out.println("今天是今年的第"+(31+29+c));break;
case 5:System.out.println("今天是今年的第"+(31*2+30+29+c));break;
case 7:System.out.println("今天是今年的第"+(31*3+30*2+29+c));break;
case 8:System.out.println("今天是今年的第"+(31*4+30*2+29+c));break;
case 10:System.out.println("今天是今年的第"+(31*5+30*3+29+c));break; case 12:System.out.println("今天是今年的第"+(31*6+30*4+29+c));break;
case 4:System.out.println("今天是今年的第"+(31*2+29+c));break;
case 6:System.out.println("今天是今年的第"+(31*3+29+30+c));break;
case 9:System.out.println("今天是今年的第"+(31*5+30*2+29+c));break;
case 11:System.out.println("今天是今年的第"+(31*6+30*3+29+c));break;
default :System.out.println("今天是今年的第"+(31+c));break;
}
}
else{
switch(b){
case 1:System.out.println("今天是今年的第"+c);break;
case 3:System.out.println("今天是今年的第"+(31+28+c));break;
case 5:System.out.println("今天是今年的第"+(31*2+30+28+c));break;
case 7:System.out.println("今天是今年的第"+(31*3+30*2+28+c));break;
case 8:System.out.println("今天是今年的第"+(31*4+30*2+28+c));break;
case 10:System.out.println("今天是今年的第"+(31*5+30*3+28+c));break;
case 12:System.out.println("今天是今年的第"+(31*6+30*4+28+c));break;
case 4:System.out.println("今天是今年的第"+(31*2+28+c));break;
case 6:System.out.println("今天是今年的第"+(31*3+28+30+c));break;
case 9:System.out.println("今天是今年的第"+(31*5+30*2+28+c));break;
case 11:System.out.println("今天是今年的第"+(31*6+30*3+28+c));break;
default :System.out.println("今天是今年的第"+(31+c));break;
}
}
}