循环语句
while适合于循环结束条件已知 但是循环次数未知的情况
for 适合于循环次数已知的情况
for和while之间是可以完全互换的
while 循环
循环初始化
while(循环继续条件){
循环体
循环间距
}
while(true){//死循环
循环体
循环间距
if(循环结束条件){
break;
}
}
do-while循环
do-while 先执行 再判断
do{
循环体;//循环初始化 循环间距
}while(循环结束条件)
for循环
for( 循环初始化 ; 循环继续条件 ; 循环间距){
循环体
}
嵌套循环
打印图形
public class Test {
public static void main(String[] args) {
/*
*
**
***
****
*****
*/
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
/*
*******
******
*****
****
***
**
*
*/
for(int i=1;i<=7;i++){
// for(int j=1;j<=8-i;j++){
// System.out.print("*");
// }
for(int j=8-i;j>=1;j--){
System.out.print("*");
}
System.out.println();
}
/*
-----*
----**
---***
--****
-*****
******
*/
for(int i=1;i<=6;i++){
for(int k=1;k<=6-i;k++){
System.out.print(" ");
}
for(int j=1;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
/*
i j
* 1 1
** 2 1 2
*** 3 1 2 3
**** 4 1 2 3 4
***** 5 1 2 3 4 5
****** 6 1 2 3 4 5 6
***** 7 1 2 3 4 5 6 7
**** 8 1 2 3 4 5 6 7 8
*** 9 1 2 3 4 5 6 7 8 9
** 10 1 2 3 4 5 6 7 8 9 10
* 11 1 2 3 4 5 6 7 8 9 10 11
*/
System.out.println("=============================================");
for(int i=1;i<=11;i++){
for(int j=1;j<=i&&(i+j)<=12;j++){
System.out.print("*");
}
System.out.println();
}
/* i k
----* 1 -4
---* * 2 -3
--* * * 3 -2
-* * * * 4 -1
* * * * * 5 0
-* * * * 6 1
--* * * 7 2
---* * 8 3
----* 9 4
*/
for(int i=1;i<=9;i++){
for(int k=1;k<=Math.abs(i-5);k++){
System.out.print(" ");
}
for(int j=1;j<=i&&(i+j)<=10;j++){
System.out.print("* ");
}
System.out.println();
}
/*
*
* *
* *
* *
* *
* *
* *
* *
*
*/
for(int i=1;i<=9;i++){
for(int k=1;k<=Math.abs(i-5);k++){
System.out.print(" ");
}
for(int j=1;j<=i&&(i+j)<=10;j++){
if(j==1||j==i||j+i==10){
System.out.print("* ");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
编程题
import java.util.Scanner;
public class Demo4_1 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter an integer,the input ends if it is 0:");
double average=0;
int positiveCount=0;
int negativeCount=0;
int sum=0;
while(true){
int num=scanner.nextInt();
if(num>0){
positiveCount++;
}else if(num<0){
negativeCount++;
}else{
if(positiveCount+negativeCount==0){
System.out.println("No numbers are entered except 0");
return;
}
break;
}
sum+=num;
}
average=1.0*sum/(negativeCount+positiveCount);
System.out.println("The number of positives is "+positiveCount);
System.out.println("The number of negatives is "+negativeCount);
System.out.println("The total is "+sum);
System.out.println("The average is "+average);
}
}
import java.util.Scanner;
public class Demo4_2 {
public static void main(String[] args){
/* 第一名的成绩firstScore和名字firstName
* 第二名的成绩secondScore和名字secondName
*
*
* */
Scanner scanner=new Scanner(System.in);
int firstScore=0,secondScore=0;
String firstName="",secondName="";
System.out.print("Enter the number of students:");
int count=scanner.nextInt();
for(int i=0;i<count;i++){
System.out.print("Enter the name and score:");
String name=scanner.next();
int score=scanner.nextInt();
if(score>=firstScore){
secondScore=firstScore;
secondName=firstName;
firstScore=score;
firstName=name;
}else if(score>=secondScore){
secondScore=score;
secondName=name;
}
}
System.out.println(firstName+":"+firstScore);
System.out.println(secondName+":"+secondScore);
}
}
public class Demo4_3 {
public static void main(String[] args) {
int count=0;
for (int i=100;i<=200;i++) {
if ((i%6==0||i%5==0)&&(i%5==0^i%6==0)) {
count++;
System.out.print(i+" ");
if (count%10==0) {
System.out.println();
}
}
}
}
}
public class Demo4_4 {
public static void main(String[] args) {
int n=1;
while(n*n<=12000){
n++;
}
System.out.println(n);
n=1;
while(n*n*n<=12000){
n++;
}
System.out.println(n-1);
}
}
扫描二维码关注公众号,回复:
3913465 查看本文章
import java.util.Scanner;
public class Demo4_5 {
public static void main(String[] args) {
//120/2=60 -->2
//60/2=30 -->2
//30/2=15 -->2
//15/3=5 -->3
//5 ------>5
Scanner scanner=new Scanner(System.in);
System.out.print("Enter a number:");
int number=scanner.nextInt();
while(true){
boolean flag=false;
//120 [2,60] /2 60
//60 [2,30] /2 30
//30 [2,15] /2 15
//15 [2,7] /3 5
//5 [2,2]
for(int i=2;i<=number/2;i++){
if(number%i==0){
System.out.print(i+" ");
number/=i;
flag=true;
break;
}
}
if(!flag){
System.out.print(number);
break;
}
}
}
}
4.6
import java.util.Scanner;
public class Demo4_6 {
public static void main(String[] args) {
//ptintf 格式化输出
//%3d 3代表宽度,给前面补空格
//%-3d 给后面补空格
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the line:");
int line=scanner.nextInt();
for(int i=1;i<=line;i++){//行数
for(int k=1;k<=line-i;k++){
System.out.print(" ");
}
//-1
//-2 -1 2
for(int j=-i;j<=i;j++){
if(j!=0&&j!=1){
// System.out.print(Math.abs(j)+"\t");
System.out.printf("%-3d",Math.abs(j));
//3d "1" " 12" "123"
//-3d "1 " "12 " "123"
}
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Demo4_7 {
public static void main(String[] args) {
for(int i=1;i<=6;i++){
for(int j=1;j<=i;j++){
System.out.print(j+" ");
}
System.out.println();
}
for(int i=0;i<=6;i++){
for(int j=1;j<=6-i;j++){
System.out.print(j+" ");
}
System.out.println();
}
for(int i=1;i<=6;i++){
for(int k=1;k<=6-i;k++){
System.out.print(" ");
}
for(int j=1;j<=i;j++){
System.out.print(j+" ");
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Demo4_8 {
public static void main(String[] args) {
/*
* i=1 0
* i=2 0 1 0
* i=3 0 1 2 1 0
* i=4 0 1 2 3 2 1 0
* i=5 0 1 2 3 4 3 2 1 0
* */
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the line:");
int line=scanner.nextInt();
for(int i=1;i<=line;i++){
for(int k=1;k<=line-i;k++){
System.out.print(" ");
}
for(int j=-(i-1);j<=i-1;j++){//-3 ~ 3
if(j<=0){//j+i-1
System.out.printf("%4d",(int)Math.pow(2, j+i-1));
}else{//-j+i-1
System.out.printf("%4d",(int)Math.pow(2, -j+i-1));
}
}
System.out.println();
}
}
}
public class Demo4_9 {
public static void main(String[] args){
//i=1 2 3 4 5 6 7 49...i++ 2*i-1/2*i+1
//i=1 3 5 7 ...97 i+=2 i/(i+2)
double sum=0;
// for(int i=1;i<=49;i++){
// sum=sum+1.0*(2*i-1)/(2*i+1);
// }
// System.out.println(sum);
//
for(int i=1;i<=97;i+=2){
sum=sum+1.0*i/(i+2);
}
System.out.println(sum);
}
}
public class Demo4_10 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
//1.i=1 2 3 4 5 6 7 8 9 ....10000 2*i-1
//if(i%2==1)+1/(2*i-1) else -1/(2*i-1)
//2.flag=1.0 flag/(2*i-1) flag=-flag flag/(2*i-1) flag=-flag
//3.Math.pow(-1,i+1)/(2*i-1)
Math.pow(2,10);
double pi=0.0;
double flag=1.0;
for(int i=1;i<=10000;i++){
// //1.
// if(i%2==1){
// pi=pi+1.0/(2*i-1);
// }else{
// pi=pi-1.0/(2*i-1);
// }
// //3.
// pi=pi+Math.pow(-1, i+1)/(2*i-1);
//2.
pi=pi+flag/(2*i-1);
flag=-flag;
}
pi=4*pi;
System.out.println(pi);
}
}
public class Demo4_11 {
public static void main(String[] args) {
double e=1;
for(int i=1;i<=10000;i++){
double multi=1.0;
for(int j=1;j<=i;j++){
multi=multi*j;
}
e=e+1/multi;
}
System.out.println(e);
}
}
public class Demo4_12 {
public static void main(String[] args) {
int count=0;
for(int i=101;i<=2100;i++){
if(i%4==0&&i%100!=0||i%400==0){
System.out.print(i+" ");
count++;
if(count%10==0){
System.out.println();
}
}
}
System.out.println("\n"+count);
}
}
import java.util.Scanner;
public class Demo4_13 {
public static void main(String[] args) {
//1.用户输入年份和代表该年第一天是星期几的数字
Scanner scanner=new Scanner(System.in);
System.out.print("请输入年份和代表该年第一天第星期几的数字");
int year=scanner.nextInt();
int days=scanner.nextInt();
//2.
int m=0,y=0,k=0,j=0,h=0;
String monthStr="";
String weekStr="";
for(int month=1;month<=12;month++){
m=month;
y=year;
if(m==1||m==2){
m+=12;
y--;
}
k=y%100;
j=y/100;
h=(1+26*(month+1)/10+k+k/4+j/4+5*j)%7;
switch (month) {
case 1:
monthStr="January";
break;
case 2:
monthStr="February";
break;
case 3:
monthStr="March";
break;
case 4:
monthStr="April";
break;
case 5:
monthStr="May";
break;
case 6:
monthStr="June";
break;
case 7:
monthStr="July";
break;
case 8:
monthStr="Auguest";
break;
case 9:
monthStr="September";
break;
case 10:
monthStr="October";
break;
case 11:
monthStr="November";
break;
case 12:
monthStr="December";
break;
}
switch (h) {
case 0:
weekStr="Saturday";
break;
case 1:
weekStr="Sunday";
break;
case 2:
weekStr="Mondy";
break;
case 3:
weekStr="Tuesday";
break;
case 4:
weekStr="Wednesday";
break;
case 5:
weekStr="Thursday";
break;
case 6:
weekStr="Friday";
break;
}
System.out.println(monthStr+" 1,"+year+" is "+weekStr);
}
}
}
public class Demo4_15 {
public static void main(String[] args){
for(int i=1;i<=10000;i++){
int sum=0;
for(int j=1;j<=i/2;j++){
if(i%j==0){
sum+=j;
}
}
if(sum==i){
System.out.println(i);
}
}
}
}
import java.util.Scanner;
public class Demo4_16 {
public static void main(String[] args){
//定义两个计数器 分别记录电脑赢的次数和玩家赢的次数
//pCount cCount
Scanner scanner=new Scanner(System.in);
int pCount=0;
int cCount=0;
while(true){
System.out.print("请输入 0剪刀 1石头 2布:");
int p=scanner.nextInt();
int c=(int) (Math.random()*3);
if(p==c){
System.out.println("平局,请继续");
}
if(p==0&&c==1){
System.out.println("玩家剪刀,电脑石头,电脑赢啦!");
cCount++;
}
if(p==0&&c==2){
System.out.println("玩家剪刀,电脑布,玩家赢啦!");
pCount++;
}
if(p==1&&c==2){
System.out.println("玩家石头,电脑布,电脑赢啦!");
cCount++;
}
if(p==1&&c==0){
System.out.println("玩家石头,电脑剪刀,玩家赢啦!");
pCount++;
}
if(p==2&&c==0){
System.out.println("玩家布,电脑剪刀,电脑赢啦!");
cCount++;
}
if(p==2&&c==1){
System.out.println("玩家布,电脑石头,玩家赢啦!");
pCount++;
}
if(cCount==2||pCount==2){
break;
}
}
if(cCount==2){
System.out.println("电脑赢啦!!!");
}else{
System.out.println("玩家赢啦!!!");
}
}
}
public class Demo4_17 {
public static void main(String[] args) {
double sum=0;
for(int i=1;i<=624;i++){
sum=sum+1/(Math.sqrt(i)+Math.sqrt(i+1));
}
System.out.println(sum);
}
}
import java.util.Scanner;
public class Demo4_18 {
public static void main(String[] args) {
//1.输入一个十进制的数
Scanner scanner=new Scanner(System.in);
System.out.print("输入一个十进制的数:");
int number=scanner.nextInt();
String str="";
//2.转换成二进制
// while(true){
// str=number%2+str;
// number/=2;
// if(number==0){
// break;
// }
// }
// //3.输出
// System.out.println(str);
//八进制
// while(true){
// str=number%8+str;
// number/=8;
// if(number==0){
// break;
// }
// }
// System.out.println(str);
//十六进制
while(true){
int num=number%16;
if(num>=10){
switch (num) {
case 10:
str="A"+str;
break;
case 11:
str="B"+str;
break;
case 12:
str="C"+str;
break;
case 13:
str="D"+str;
break;
case 14:
str="E"+str;
break;
case 15:
str="F"+str;
break;
}
}else{
str=num+str;
}
number/=16;
if(number==0){
break;
}
}
System.out.println("0x"+str);
}
}
4.19
import java.util.Scanner;
public class Demo4_19 {
public static void main(String[] args) {
//1.输入几个整数
Scanner scanner=new Scanner(System.in);
System.out.print("Enter numbers:");
int max=0;
int count=0;
//2.找出最大数,计算出现次数
while(true){
int number=scanner.nextInt();
if(number==0){
break;
}
if(number>max){
max=number;
count=1;
}else if(number==max){
count++;
}
}
System.out.println("The largest number is "+max);
System.out.println("The occurrence count of the largest number is "+count);
}
}
public class Demo4_20 {
public static void main(String[] args) {
int count=0;
for(int i=1;i<7;i++){
for(int j=i+1;j<=7;j++){
System.out.println(i+" "+j);
count++;
}
}
System.out.println("count:"+count);
}
}
import java.util.Scanner;
public class Demo4_21 {
public static void main(String[] args){
//sum 数字和
//powSum 所有数字平方和
Scanner scanner=new Scanner(System.in);
double sum=0;
double powSum=0;
System.out.print("Enter 10 numbers:");
for(int i=1;i<=10;i++){
double num=scanner.nextDouble();
sum+=num;
powSum+=num*num;
}
double mean=sum/10;
double sd=Math.sqrt((powSum-sum*sum/10)/9);
System.out.println("mean ="+mean);
System.out.println("sd ="+sd);
}
}
import java.util.Scanner;
public class Demo4_22 {
public static void main(String[] args) {
//1.提示用户输入字符串
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the first string:");
String a=scanner.nextLine();
System.out.print("Enter the second string:");
String b=scanner.nextLine();
//2.显示两个字符串最长的共同前缀
int i=0;
for(;i<Math.min(a.length(), b.length());i++){
if(a.charAt(i)!=b.charAt(i)){
break;
}
}
System.out.println("["+a.substring(0, i)+"]");
}
}
4.23
import java.util.Scanner;
public class Demo4_23 {
public static void main(String[] args) {
//1.输入一个字符串
Scanner scanner=new Scanner(System.in);
System.out.print("Enter a string:");
String s=scanner.nextLine();
int count=0;
//2.判断是否大写
for(int i=0;i<s.length();i++){
if(s.charAt(i)>=65&&s.charAt(i)<=90){
count++;
}
}
//3.计数
System.out.println(count);
}
}
import java.util.Scanner;
public class Demo4_24 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter a string:");
String str=scanner.nextLine();
for(int i=0;i<str.length();i++){
if(i%2==0){
System.out.print(str.charAt(i));
}
}
}
}