1、循環輸出1到100之間所有能被3或能被4整除的數。
package com.hz.loop02;
/**
* 1、循環輸出1到100之間所有能被3或能被4整除的數。
* @author ztw
*
*/
public class Practice01 {
public static void main(String[] args) {
for(int i=1;i<=100;i++){
//判斷下是否被3或能被4整除,是的話輸出
if(i%3==0||i%4==0){
System.out.println(i);
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
2、循環輸出200到300之間所有能被5整除,或能被2整除並且能被3整除的數。
package com.hz.loop02;
/**
* 2、循環輸出200到300之間所有能被5整除,或能被2整除並且能被3整除的數。
* @author ztw
*
*/
public class Practice02 {
public static void main(String[] args) {
//輸出200到300之間
for(int i=200;i<=300;i++){
//判斷是否能被5整除,或能被2整除並且能被3整除的數
if(i%5==0||(i%2==0&&i%3==0)){
System.out.println(i);
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
3、循環輸出1到2000中所有能4整除但不能被100整除的數,或能被400整除的數。
package com.hz.loop02;
/**
* 3、循環輸出1到2000中所有能4整除但不能被100整除的數,或能被400整除的數。
* @author ztw
*
*/
public class Practice03 {
public static void main(String[] args) {
//循環輸出1到2000
for(int i=1;i<=2000;i++){
//判斷所有能4整除但不能被100整除的數,或能被400整除的數
if((i%4==0&&1%100!=0)||i%400==0){
System.out.println(i);
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
4、計算1+2+3+……+100的結果。
package com.hz.loop02;
/**
* 4、計算1+2+3+……+100的結果。
* @author ztw
*
*/
public class Practice04 {
public static void main(String[] args) {
//定義一個結果變量初始為0
int sum =0;
//i循環+1
for(int i=1;i<=100;i++){
//1-100自加
sum+=i;
}
System.out.println("1+2+3+……+100的結果是:"+sum);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
5、計算1*2*3*……*10的結果。
package com.hz.loop02;
/**
* 5、計算1*2*3*……*10的結果。
* @author ztw
*
*/
public class Practice05 {
public static void main(String[] args) {
////定義一個結果變量初始為1
int sum = 1;
//i循環+1
for(int i=1;i<=10;i++){
//每一次循環+1相乘
sum=sum*i;
}
System.out.println("1*2*3*……*10的結果是:"+sum);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
6、計算1+1/4+1/9+….+1/(20*20)
package com.hz.loop02;
/**
* 6、計算1+1/4+1/9+....+1/(20*20)
* @author ztw
*
*/
public class Practice06 {
public static void main(String[] args) {
//定義兩個變量
int number = 1;
double sum = 0;
/*
* 循環自+1,需要注意的是分子必須?.0的模式
*/
while(number<=20){
sum +=1.0/(number*number);
number++;
}
//輸出結果
System.out.println(sum);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
7、輸入一個整數放入到變量n中,如果這個整數大於0,那么計算1+2+3+……+(n-1)+n的結果,否則輸出“輸入的數據有錯誤
package com.hz.loop02;
import java.util.Scanner;
/**
*
* 7、輸入一個整數放入到變量n中,如果這個整數大於0,
* 那么計算1+2+3+……+(n-1)+n的結果,否則輸出“輸入的數據有錯誤
* @author ztw
*
*/
public class Practice07 {
public static void main(String[] args) {
int sum = 0;
Scanner sc = new Scanner(System.in);
System.out.println("輸入一個整數:");
int n = sc.nextInt();
if(n>0){
for(int i=0;i<=n;i++){
sum+=i;
}
}else{
System.out.println("輸入的數據有錯誤!");
}
System.out.println(sum);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
8、循環輸入5個學生的成績,計算這5個學生的總分,及平均分
package com.hz.loop02;
import java.util.Scanner;
/**
* 8、循環輸入5個學生的成績,計算這5個學生的總分,及平均分
* @author ztw
*
*/
public class Practice08 {
public static void main(String[] args) {
float sum = 0;
float avg = 0;
Scanner sc = new Scanner(System.in);
/*
* 循環輸出5個學生的成績
* 求出總成績
*/
for(int i=1;i<=5;i++){
System.out.println("輸入學生的成績:");
float sroce = sc.nextFloat();
sum+=sroce;
}
//求平均成績
avg = sum/5;
System.out.println("總分:"+sum+"平均分:"+avg);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
9、首先要求用戶輸入學生的數目放入到變量n中,如果這個數大於0,那么就循環n次接收n個學生的成績,計算總分及平均分。否則輸出“學生的人數不能為負數
package com.hz.loop02;
import java.util.Scanner;
/**
* 9、首先要求用戶輸入學生的數目放入到變量n中,
* 如果這個數大於0,那么就循環n次接收n個學生的成績,
* 計算總分及平均分。否則輸出“學生的人數不能為負數
* @author ztw
*
*/
public class Practice09 {
public static void main(String[] args) {
int n = 0;
float sum=0;
Scanner sc = new Scanner(System.in);
System.out.println("輸入學生的數目:");
n = sc.nextInt();
/*
* 判斷變量n是否大於0
* 如果大於0,則進行成績輸入並求和,否則輸出”學生的人數不能為負數“
*/
if(n>0){
for(int i=1;i<=n;i++){
System.out.println("輸入學生的成績:");
float sroce = sc.nextFloat();
sum+= sroce;
}
//計算平均成績
float avg = sum/n;
System.out.println("總分:"+sum+"及平均分:"+avg);
}else{
System.out.println("學生的人數不能為負數");
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
10、循環問“老婆,你愛我嗎?”,如果回答的是“愛”,那么就結束循環,否則就繼續問。用程序描述這個故事
package com.hz.loop02;
import java.util.Scanner;
/**
* 10、循環問“老婆,你愛我嗎?”,
* 如果回答的是“愛”,那么就結束循環,
* 否則就繼續問。用程序描述這個故事
* @author ztw
*
*/
public class Practice10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
/*
* 循環問“老婆,你愛我嗎?”,
* 如果回答的是“愛”,那么就結束循環,
* 否則就繼續問。
*/
for(;;){
System.out.println("老婆,你愛我嗎?");
String choice = sc.next();
if(choice.equals("愛")){
System.out.println("循環結束");
//中斷,跳出循環
break;
}else{
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
11、循環輸入字符串,將這些輸入的字符串都連接起來,至到輸入的字符串為“Esc”就結束循環,最后顯示這個連接起來的字符串。
比如:輸入abc 輸入def 輸入Esc
就輸出abcdef
package com.hz.loop02;
import java.util.Scanner;
/**
*
*11、循環輸入字符串,將這些輸入的字符串都連接起來,至到輸入的字符串為“Esc”就結束循環,
*最后顯示這個連接起來的字符串。
*比如:輸入abc 輸入def 輸入Esc
*就輸出abcdef
* @author ztw
*
*/
public class Practice11 {
public static void main(String[] args) {
String str = "";
Scanner sc = new Scanner(System.in);
//構造一個其中不帶字符的字符串緩沖區,初始容量為 16 個字符。
StringBuffer sbuffer = new StringBuffer();
//循環輸入輸出字符
while(true){
System.out.println("輸入字符串:");
str = sc.next();
//判斷如果str等於"Esc"
if(str.equals("Esc")){
break;
}
/*
* 按順序將 str參數中的字符添加到此 StringBuffer 中,
* 並使 StringBuffer 在長度上增加該參數的長度。
*/
sbuffer.append(str);
}
//輸出這個連接起來的字符串
System.out.println("連接起來的字符串:"+sbuffer.toString());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
12、輸入年份和月份,打印該該月份的日歷,例如:輸入2011年9月,就打印2011年9月的日歷
package com.hz.loop02;
import java.util.Scanner;
/**
*
*12、輸入年份和月份,打印該該月份的日歷,例如:輸入2011年9月,就打印2011年9月的日歷
* @author ztw
*
*/
public class Practice12 {
public static void main(String[] args) {
//定義表示年和月的兩個變量
int year,month;
Scanner sc = new Scanner(System.in);
System.out.println("請輸入年份:");
year = sc.nextInt();
System.out.println("請輸入月份:");
month = sc.nextInt();
//判斷輸入月份是否合理
if(month<=12&&month>=1){
/*
* 判斷輸入的年份是否為潤年
*/
if(month==1||month==3||month==5||month==7||month==8){
for(int i=1;i<=31;i++){
System.out.print(" "+i+" ");
if(i%7==0){
System.out.println();
}
}
}else if(month==2){
/*
* 判斷輸入的年份是否為潤年
* 閏年二月29天,否則28天
*/
if((year%4==0&&year%100!=0)||year%400==0){
for(int i=1;i<=29;i++){
System.out.print(" "+i+" ");
//一行等於7,就換行
if(i%7==0){
System.out.println();
}
}
}else{
for(int i=1;i<=28;i++){
System.out.print(" "+i+" ");
if(i%7==0){
System.out.println();
}
}
}
}else{
for(int i=1;i<=30;i++){
System.out.print(" "+i+" ");
if(i%7==0){
System.out.println();
}
}
}
}else{
System.out.println("請輸入合理的月份!!!");
}
}
}
///
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class ff {
public static void main(String[] args) {
// 輸入年份和月份,打印該該月份的日歷,
// 例如:輸入2011年9月,就打印2011年9月的日歷
// 2011年9月
// 1 2 3 4 5 6 7
// 8
//
//
// 30
//
Scanner s = new Scanner(System.in);
int year, month;
System.out.print("請輸入年份:");
year = s.nextInt();
System.out.print("請輸入年份:");
month = s.nextInt();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date tmpDate=new Date();
try {
tmpDate = format.parse(year+"-"+month+"-"+"1");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(tmpDate);
int space =cal.get(Calendar.DAY_OF_WEEK);
int t;
System.out.println(year + " 年 " + month + " 月");
System.out.println("日\t一\t二\t三\t四\t五\t六");
if (month > 0 && month <= 12) {
// 31天
if (month == 1 || month == 3 || month == 5 || month == 7
|| month == 8 || month == 10 || month == 1) {
for(int j=1;j<space;j++)
{
System.out.print("\t");
}
t=space;
for (int i = 1; i <= 31; i++) {
System.out.print(i + "\t");
if (t % 7 == 0)
System.out.println();
t++;
}
} else if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
for (int i = 1; i <= 29; i++) {
}
} else {
for (int i = 1; i <= 28; i++) {
}
}
}
// 30天
else {
for (int i = 1; i <= 30; i++) {
}
}
} else {
System.out.print("月份數字不合法");
}
}
}
