JAVA程式語言基礎第六章
阿新 • • 發佈:2018-12-31
第六章課後作業
上機練習1
package tz1;public class dome1 {
public static void main(String[] args) {
int sum=0;
for (int num =1; num<=99; num+=2) {
sum+=num;
System.out.println(sum);
}
}}上機練習4 驗證使用者登入資訊
package tz1;import java.util.Scanner;
/*
* 上機練習4 驗證使用者登入資訊
*/
public class dome2 {
public static void main(String[] args) {
String name ;//定義使用者名稱
String password ;//定義使用者密碼
int fas = 3;//定義輸錯的次數
Scanner input = new Scanner(System.in);
for (int i = 1; i <= 3; i++) {
System.out.println("請輸入使用者名稱:");
name = input.next(); System.out.println("請輸入密碼:");
password = input.next();
if (name.equals("jim") || password.equals("123456")) {
System.out.println("歡迎登入系統");
break;
}
if (i == 3) {
System.out.println("對不起,您3次都輸入錯誤");
break;
} else {
System.out.println("輸入錯誤!您還有" + (fas - i) + "機會"); continue;
}
}
}}
簡答題
1計算週一到週五的平均學習時間
package com.bdqn.dome;
import java.util.Scanner;
public class dome8 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double total=0;
for (int i = 0; i < 5; i++) {
System.out.println("請輸入周"+(i+1)+"的學習時間:");
double time = input.nextDouble();
total += time;
}
System.out.println("週一到週五平均學習時間是"+total/5.0+"小時");
}}
2求雞和兔子數量
package com.bdqn.dome;public class dome9 {
public static void main(String[] args) {
int chookNum, rabitNum;
for (chookNum = 0; chookNum < 35; chookNum++) {
// 雞等於0 雞小於35 雞的數量加1
// 兔子的數量等於35減去雞的數量
rabitNum = 35 - chookNum;
if (2 * chookNum + 4 * rabitNum == 94) { System.out.println("雞有" + chookNum + "只,免有" + rabitNum + "只");
} } }
}
3.FlipFlop遊戲
package com.bdqn.dome;//開發一個標題為"flipfoip"的遊戲
public class dome10 {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0 && i % 5 == 0) {// 如果能被3整除並且能被5整除
System.out.println("flipfoip");// 就輸出flipfoip
} else if (i % 5 == 0) {
System.out.println("foip");
} else if (i % 3 == 0) {
System.out.println("flip");
} else {
System.out.println(i);
}
}
}}
4.男人,女人,小孩的數量。
package com.bdqn.dome;public class dome11 {
public static void main(String[] args) {
int man, women, kids;
for (kids = 1; kids <= 50; kids++) {
for (women = 1; women < 25; women++) {
for (man = 1; man < 16; man++) {
if (3 * man + 2 * women + kids == 50) {
if (man + women + kids == 30) {
}
System.out.println("男人有" + man + "女人有" + women + "小孩有" + kids);
} }
}
}
}}
上機練習1
package tz1;public class dome1 {
public static void main(String[] args) {
int sum=0;
for (int num =1; num<=99; num+=2) {
sum+=num;
System.out.println(sum);
}
}}上機練習4 驗證使用者登入資訊
package tz1;import java.util.Scanner;
/*
* 上機練習4 驗證使用者登入資訊
*/
public class dome2 {
public static void main(String[] args) {
String name ;//定義使用者名稱
String password ;//定義使用者密碼
int fas = 3;//定義輸錯的次數
Scanner input = new Scanner(System.in);
for (int i = 1; i <= 3; i++) {
System.out.println("請輸入使用者名稱:");
name = input.next(); System.out.println("請輸入密碼:");
password = input.next();
if (name.equals("jim") || password.equals("123456")) {
System.out.println("歡迎登入系統");
break;
}
if (i == 3) {
System.out.println("對不起,您3次都輸入錯誤");
break;
} else {
System.out.println("輸入錯誤!您還有" + (fas - i) + "機會"); continue;
}
}
}}
簡答題
1計算週一到週五的平均學習時間
package com.bdqn.dome;
import java.util.Scanner;
public class dome8 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double total=0;
for (int i = 0; i < 5; i++) {
System.out.println("請輸入周"+(i+1)+"的學習時間:");
double time = input.nextDouble();
total += time;
}
System.out.println("週一到週五平均學習時間是"+total/5.0+"小時");
}}
2求雞和兔子數量
package com.bdqn.dome;public class dome9 {
public static void main(String[] args) {
int chookNum, rabitNum;
for (chookNum = 0; chookNum < 35; chookNum++) {
// 雞等於0 雞小於35 雞的數量加1
// 兔子的數量等於35減去雞的數量
rabitNum = 35 - chookNum;
if (2 * chookNum + 4 * rabitNum == 94) { System.out.println("雞有" + chookNum + "只,免有" + rabitNum + "只");
} } }
}
3.FlipFlop遊戲
package com.bdqn.dome;//開發一個標題為"flipfoip"的遊戲
public class dome10 {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0 && i % 5 == 0) {// 如果能被3整除並且能被5整除
System.out.println("flipfoip");// 就輸出flipfoip
} else if (i % 5 == 0) {
System.out.println("foip");
} else if (i % 3 == 0) {
System.out.println("flip");
} else {
System.out.println(i);
}
}
}}
4.男人,女人,小孩的數量。
package com.bdqn.dome;public class dome11 {
public static void main(String[] args) {
int man, women, kids;
for (kids = 1; kids <= 50; kids++) {
for (women = 1; women < 25; women++) {
for (man = 1; man < 16; man++) {
if (3 * man + 2 * women + kids == 50) {
if (man + women + kids == 30) {
}
System.out.println("男人有" + man + "女人有" + women + "小孩有" + kids);
} }
}
}
}}