=90"/> =90">
search

Java loop

Feb 22, 2017 am 10:03 AM

1. Sequence structure

2. Selection structure

//import java.util.Scanner;

public class ifelse {
	
	public static void main(String [] args){
	
//		Scanner input = new Scanner(System.in);
	
		/**
		System.out.println("请输入成绩:");
		 
		int score = input.nextInt();
		
		if(score>=90){
			
			System.out.print("A级");
			
		}
		else if(score>=80){
			
			System.out.print("B级");
			
		}
		
		else {
			
			System.out.print("C级");
			
		}
		
		//运动会
		 * System.out.println();
		double time = input.nextInt();
		
		String gender = input.next();//=null;
		
		if(time<10){
			
			if(gender=="男"){//("男".equals(gender))
				
				System.out.print("");
				
			}else if(gender.equals("")){
				
				System.out.print("");
				
			}

		}else{
			System.out.print("淘汰");
		}
		
		//根据用户输入星期、气温、天气进行判断
		
		System.out.print("请输入今天星期几:");
		
		int week = input.nextInt();
		
		if(0<week&week<=7){
			if(week==6||week==7){
			System.out.print("请输入今天的温度:");
			double temperature = input.nextInt();
		    if(temperature>=30){
		   
				System.out.print("今天去游泳。");
				
			}else{
				
				System.out.print("今天去爬山。");
				
			}
			
		}else{
			System.out.print("请输入今天的天气:");
			String day = input.next();
			
			if(day.equals("晴")){
				
				System.out.print("今天谈业务。");
				
			}else{
				System.out.print("今天上网查资料。");
			}
		}
			
		}else{
			System.out.print("一周只有七天,请输入1-7之间的数字。");
		}
					
		//switch 判断
		int score = input.nextInt();
		switch(score/10){
		
			case 10:
				System.out.print("A级");
			break;
		    default:
		    	System.out.print("E级");
		}
		
		
		//根据用户输入年份、月份进行判断
		System.out.print("请输入年份:");
		int year = input.nextInt();
		System.out.print("请输入月份:");
		int month = input.nextInt();
		switch(month){
			case 2:
				if(year%4==0&year%100!=0&year%400==0){
				System.out.print("28days");
				}else{
				System.out.print("29days");
				}break;
			case 4:
			case 6:
			case 9:
			case 11:
				System.out.print("30days");
				break;
			default:
				System.out.print("31days");
		}
		
		//100求和
		int i = 1;
		int sum = 0 ;
		while(i<=100){
			sum = sum + i ;
			i++;
		}
		System.out.println("sum is :"+sum);
	//**/
		/*
		int i=3;
				
		while(i>=0){
			System.out.println("请输入用户名:");
			String user = input.next();
			System.out.println("请输入密码:");
			int password = input.nextInt();
			if("zhxj".equals(user)&&password==123456){
				System.out.print("欢迎进入系统!");
				break;
			}else{
				System.out.print("输入错误,您还有"+i+"次机会!");
			}
			i--;
		}*/
		//System.out.print("请输入0-9之间的数字:");
		/*System.out.print("请输入一个随机数字:");
		int result = (int)(Math.random()*10);
		int num = input.nextInt();
		while(result!=num){
				
			if(result<num){
				System.out.println("不好意思答错了!");
				System.out.println("您猜大了!");
			}else if(result>num){
				System.out.println("不好意思答错了!");
				System.out.println("您猜小了!");
			}
			System.out.println("请重新输入一个随机数字:");	
			num = input.nextInt();
				
			}
				System.out.println("恭喜您答对了!");
		
	/*
	 * public static void main(String[] args) {
	
		//System.out.print("请输入0-9之间的数字:");
		//Scanner input = new Scanner(System.in);
		int i = 1;
		int sum =0 ;
		do{
			sum = sum + i;
			i++;
		}while(i <= 100);
		System.out.print("Sum is:" + sum);
	}
	 */
	
	/*
	 * int i;
		
		do{
			System.out.println("************欢迎光临QQ登陆页面***********");
			System.out.println("1、注册");
			System.out.println("2、登录");
			System.out.println("3、退出");
			System.out.println("您的输入是:");
			i = input.nextInt();
			
		}while(i!=3);
		
		System.out.println("************再见!***********");
		
		
		int sum = 0;
		for(int i = 1;i <= 100;i ++){
			
			sum += i;
		}
		System.out.println("SUM=" + sum);
	 */
				
	/*System.out.print("请输入学生的姓名:");
		
		String name = input.next();
		int sum = 0;	
		for(int i = 1; i<=5;i++){
			System.out.println("请输入第"+i+"门的成绩:");
			int score = input.nextInt();
			
			sum = sum + score;
		}
			System.out.println(name+"的平均分是:"+sum/5);
			
			
				String y ;
				do{
					System.out.print("请输入学生的姓名:");
					String name = input.next();
					int sum = 0;
					for(int i = 1; i<=5;i++){
						System.out.println("请输入5门功课中第"+i+"门的成绩:");
						int score = input.nextInt();
						sum = sum + score;
					}
						System.out.println(name+"的平均分是:"+sum/5);
						System.out.print("继续输入吗?(y/n)");
						y = input.next();
										
				}while("y".equals(y));
				
				System.out.print("成绩录入结束!");		
				
				
				
				for(int i=1;i<=10;i++){
			
			if(i % 4 == 0){
				break;
				}
			System.out.print(i);
		}
		System.out.print("循环结束。");
				*/
		//百元百鸡
		for(int n=0;n<100/5;n++){//公鸡购买数量;
			
	       for(int m=0;m<100/3;m++){//母鸡购买数量;
			
		    	int l = 100-n-m;//购买小鸡的数量;
		    		
		    		if((n*5+m*3+l/3)==100&&(l%3==0)){
		    			System.out.println(n+","+m+","+l);	
		   		    			
		    	}
		    } 
		}
	}		
}



3. Loop structure
for loop
One hundred yuan buys 100 chickens

public class ifelse {
	
	public static void main(String [] args){
	
	for(int n=0;n<100/5;n++){ //公鸡购买数量;
			
	       for(int m=0;m<100/3;m++){ //母鸡购买数量;
			
		    	int l = 100-n-m; //购买小鸡的数量;
		    		
		    		if((n*5+m*3+l/3)==100&&(l%3==0)){
		    		
		    			System.out.println(n+","+m+","+l);	
		   		    			
		    	}
		    } 
		}
	}		
}

Result verification:

0,25,75
4,18,78
8,11,81
12,4,84

Example 2:

//Find all prime numbers in 3-100: prime numbers can only be divisible by 1 and themselves

public class sum {

	public static void main(String [] args){
	
		//Scanner input = new Scanner(System.in);
		
		//求3-100中的所有素数:素数只能被1和自己整除的数字
		
		for(int b=2;b<100;b++){
		
			for(int a=2;a<=b;a++){
			
				if(b%a==0){
				
					if(a==b){
					
						System.out.print(b+" ");
					}
					
					break;
				}
			}
		}
	}
}

Result verification :

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97



Parameter passing example:

public class calc {

	public void calc1(int num){
		
		 num = num + 1;
		
	}
	
	public void calc2(students stu){
		
		stu.setAge(stu.getAge()+1);
		
	}
	
	
}



##

public class calc {

	public void calc1(int num){
		
		 num = num + 1;
		
	}
	
	public void calc2(students stu){
		
		stu.setAge(stu.getAge()+1);
		
	}
	
	
}

Object array:


public class students{
		//students类的属性
		private String name;
		
		private int age;
		
		private String gender;
		//students类的构造方法
		public students(){
			//空方法,默认值
		}
		public students(String name,int age,String gender){
			//students类的构造方法
			this.name = name ;
			this.age = age ;
			this.gender = gender ;
			
		}
		//students的get方法获取属性值
		public String getName(){
			
			return this.name;
			
		}
		
		public int getAge(){
						
			return this.age;
			
		}
		
		public String getGender(){
			
			return this.gender;
			
		}
		//students的set方法对属性进行赋值
		public void setName(String name){
			
			this.name = name ;
		}
		public void setAge(int age){
		//对age进行范围圈定和判断
			if(age>45 || age<15){
				
				this.age=18;
				
			}else{
				
				this.age = age ;
			}
			
		}
		public void setGender(String gender){
			
			this.gender = gender ;
		}
	}



public class studentsDemo {

	public static void main(String[] args) {
		//实例化对象
		students st = new students() ;
						
//		st.name="小明";
//		st.age=12;
//		st.gender="男";
		
//		System.out.println(st.name+"\n"+st.age+"\n"+st.gender+"\n");
		
	//	st.setName("小虎");
	//	st.setAge(14);
	//	st.setGender("男");
		
		
	//	System.out.println(st.getName()+"\n"+st.getAge()+"\n"+st.getGender()+"\n");
		//1、定义对象数组
		students[] arrs = new students [3];
		//2、实例化对象
		students s1 = new students("张三",15,"男");
		students s2 = new students("李四",20,"女");
		students s3 = new students("王五",31,"男");
		//3、将对象放入对象数组
		arrs[0] = s1;
		arrs[1] = s2;
		arrs[2] = s3;
		//students[]arrs={s1,s2,s3};
		//students[]arrs={new students("张三",15,"男"),
		                //new students("李四",20,"女"),
	                 	//new students("王五",31,"男")};
		//4、遍历输出对象数组里的对象
		for(students i : arrs){
			
			System.out.println(i.getName()+"\t"+i.getAge()+"\t"+i.getGender()+"\t");
		}
			System.out.println("\n");
		for(int i = 0; i < arrs.length; i++){
			
			System.out.println(arrs[i].getName()+"\t"+arrs[i].getAge()+"\t"+arrs[i].getGender()+"\t");
		}
	}

}



## The above is the content of Java loop. For more related content, please pay attention to PHP Chinese Net (www.php.cn)!

Java loop
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools