头歌 Java实训答案 全ac代码 请善用目录功能

Java初体验

  • 第一关
public class HelloWorld{
   
	 /********* Begin *********/
     public static void main(String[] args){
   
  
     System.out.println("姓名:张三");
     System.out.println("年龄:25"); 
     System.out.println("职业:JAVA高级工程师");
     System.out.println("薪资:15K");
     
     }
     /********* End *********/
}
  • 第二关
package key;
/********* Begin *********/
public class HelloWorld {
   
    public static void main(String[] args){
   
		System.out.println("hello eduCoder");
	}
}
/********* End *********/
  • 第三关
package chapter2;
/********* Begin *********/
public class HelloWorld {
   
	
	String userName = "张三";
    
	public static void main(String[] args){
   
		System.out.println("hello eduCoder");
	}
}

/********* End *********/

  • 第四关
package chapter2;

public class HelloEduCoder {
   
/********* Begin *********/
	public static void main(String[] args) {
   
		//System.out.println("hello world");
		//System.out.println("www.educoder.net");
		//System.out.println("educoder.net");
		System.out.println("www.educoder.net");
		//System.out.println(".net");
		//System.out.println("www");
	}
    /********* End *********/
}



变量与数据类型

  • 第一关
package chapter2.step1;

public class HelloWorld{
   
	public static void main(String[] args){
   
 /********* Begin *********/

String love="www.educoder.net";
    System.out.println(love);
 /********* End *********/    
	}
}
  • 第二关
package chapter2;
public class HelloVariable {
   
    public static void main(String[] args) {
   
        /********* Begin *********/
        String love = "我喜欢在educoder上学习";            //在这里定义变量 love 并赋初值为 我喜欢在educoder上学习
        /********* End *********/
        System.out.println("变量love的值为" + love);
        String userName = "张无忌";
        /********* Begin *********/
        userName = "李四";        //在这一行将userName的值改成李四
        /********* End *********/
        System.out.println("重新赋值后变量userName的值为" + userName);
    }
}



  • 第三关
	package chapter2;

public class JavaDataType1 {
   
    public static void main(String[] args) {
   
        
        /********* Begin *********/
                    String name ="张无忌";                 //在本行定义字符串变量name
        
                    int age =23;//在本行定义年龄变量 age
        
                    String sex="男";//在本行定义性别变量 sex
        
                    float score= 66.6f; //在本行定义分数变量  score
        
        /********* End *********/


        System.out.println(name + age + "岁" + "性别:" + sex + "这次考了" + score + "分");
    }
} 


  • 第四关
package chapter2;

public class TypeConvert {
   
    public static void main(String[] args) {
   
        /********* Begin *********/
        double score=89.3;
        int scoreInt=(int)score;
        System.out.println(score);
        System.out.println(scoreInt);

        /********* End *********/
    }
}


  • 第五关
package chapter2.step7;
/********* Begin *********/
import java.util.Scanner;

public class HelloWorld{
   
	public static void main(String[] args){
   
		System.out.println("请录入嫦娥个人信息:");
    	System.out.println("请输入姓名:");
    	Scanner input = new Scanner(System.in);
    	String name = input.next();
    	System.out.println("请输入年龄:");
    	int age = input.nextInt();
    	System.out.println("请输入性别:");
    	String sex = input.next();
    	System.out.println("请输入体重:");
    	double weight = input.nextDouble();
    	System.out.println("请输入地址:");
    	String address = input.next();
    	System.out.println("请输入是否已婚:");
    	String hun = input.next();
    	System.out.println("信息如下:");
    	System.out.println("\t"+"姓名:"+name);
    	System.out.println("\t"+"年龄:"+age+"岁");
    	System.out.println("\t"+"性别:"+sex);
    	System.out.println("\t"+"体重:"+weight+"kg");
    	System.out.println("\t"+"地址:"+address);
    	System.out.println("\t"+"婚否:"+hun);

		/********* End *********/
	}
}




运算符和表达式

  • 第一关
package step1;
import java.util.Scanner;

public class Cal {
   
    public static void main(String[] args) {
   
        /*********start*********/
        Scanner sc= new Scanner(System.in);
        int a,b;
        System.out.print("请输入第一个整数\n");
        a=sc.nextInt();
        System.out.print("请输入第二个整数\n");
        b=sc.nextInt();
        System.out.println("两数相加的结果为:"+(a+b));
        System.out.println("两数相减的结果为:"+(a-b));
        System.out.println("两数相乘的结果为:"+(a*b));
        System.out.println("两数相除的结果为:"+(a/b));
        System.out.println("两数取余数的结果为:"+(a%b));
        sc.close();
        
        
        /*********end*********/
    }

}


  • 第二关
package step2;
import java.util.Scanner;

public class Relative {
   
    public static void main(String[] args) {
   
        Scanner sc=new Scanner(System.in);
        
      
        /*********start*********/        
        int a=sc.nextInt();
        int b=sc.nextInt();
        System.out.println("a==b="+(a==b));
        System.out.println("a!=b="+(a!=b));
        System.out.println("a>b="+(a>b));
        System.out.println("a<b="+(a<b));
        System.out.println("b>=a="+(b>a));
        System.out.println("b<=a="+(b<a));
        /*********end*********/
    }

}


  • 第三关
package step3;
import java.util.Scanner;



public class testLogic {
   
    public static void main(String[] args) {
   
        
        Scanner sc=new Scanner(System.in);
          boolean a=sc.nextBoolean();
        boolean b=sc.nextBoolean();
        boolean c=sc.nextBoolean();
        
        /*********start  *********/
        
        System.out.println(!a);    
        
        System.out.println(a && b);  
        
        System.out.println( c || b );    
        
        System.out.println( a^b  );        
        /*********end  *********/
    }

}


  • 第四关
package step4;

import java.util.Scanner;

public class TestYear {
   
    public static void main(String[] args) {
   
        Scanner sc=new Scanner(System.in);
        int year=sc.nextInt();
        boolean result;
        /********start********/
        
        result=((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? true : false;
        
        System.out.println(year + "年是否为闰年:" + result);
        
        /********end********/
    }

}


  • 第五关
package step5;
import java.util.Scanner;

public class TestDemo5 {
   
    public static void main(String[] args) {
   
            Scanner sc=new Scanner(System.in);
        System.out.println("输入:");
        int m=sc.nextInt();
        int n=sc.nextInt();
        
        System.out.println("输出:");
        
        /*****start*****/
        
        System.out.println( (m+n)*2 );
        
        System.out.println( (m-n)%3 );
        
        System.out.println((m-n)/2 + (m+n)*2);
        
        /*****end*****/
    }

}




分支结构

  • 第一关
package step2;

import java.util.Scanner;

public class HelloIfStep2 {
   
	public static void main(String[] args) {
   
		Scanner input = new Scanner(System.in);
		/******start******/
		System.out.println("请输入学员成绩:");
		int score = input.nextInt();
        if (score >= 85){
   
            System.out.println("优,非常棒!"); 
        }
        else {
   
            System.out.println("良,下次加油!");
        }
		
		
		
		
		/******end******/
	}
}



  • 第二关
package step3;

import java.util.Scanner;

public class HelloStep3 {
   
	public static void main(String[] args) {
   
		System.out.println("星级成绩评定系统");
		System.out.println("请输入成绩:");
		Scanner sc = new Scanner(System.in);
		/******start******/
        int score = sc.nextInt();
        if (score >= 90){
   
            System.out.println("*****五星成绩");
        }
        else if (score >= 80 && score < 90){
   
            System.out.println("****四星成绩");
        }
        else if (score >= 70 && score < 80){
   
            System.out.println("***三星成绩");
        }
        else if (score >= 60 && score < 70){
   
            System.out.println("**俩星成绩");
        }
        else{
   
            System.out.println("无星成绩");
        }
				
		
		
		/******end******/
	}
}



  • 第三关
package step4;

import java.util.Scanner;

public class HelloSwitch {
   
	public static void main(String[] args) {
   
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请输入月份:");
		
		int input = sc.nextInt();	//获取输入的月份
		
        //通过输入的月份来判断当前季节并输出
		/*****start*****/
		switch (input){
   
            case 3:
            System.out.println(input+"月是春天");
            break;
            case 4:
            System.out.println(input+"月是春天");
            break;
            case 5:
            System.out.println(input+"月是春天");
            break;
            case 6:
            System.out.println(input+"月是夏天");
            break;
            case 7:
            System.out.println(input+"月是夏天");
            break;
            case 8:
            System.out.println(input+"月是夏天");
            break;
            case 9:
            System.out.println(input+"月是秋天");
            break;
            case 10:
            System.out.println(input+"月是秋天");
            break;
            case 11:
            System.out.println(input+"月是秋天");
            break;
            case 12:
            System.out.println(input+"月是冬天");
            break;
            case 1:
            System.out.println(input+"月是冬天");
            break;
            case 2:
            System.out.println(input+"月是冬天");
            break;
            default :
            System.out.println("输入错误");
            break;
        }

		
		/*****end*****/
		
	}
}

  • 第四关
package step5;

import java.util.Scanner;

public class Practice {
   
    
    	final static Scanner sc = new Scanner(System.in);	//创建扫描仪

		//第一题
		public void first(){
   
			System.out.println("请输入人数:");
            int input = sc.nextInt();		//获取输入的数据
            
			/*****start*****/
			if (input < 10){
   
                System.out.println("打半场");
            }else {
   
                System.out.println("打全场");
            }
			
			
			
			
			/*****end*****/
		}
		
		//第二题
		public void second(){
   
			System.out.println("请输入今天星期几:");
            int input = sc.nextInt();		//获取输入的数据
            
			/*****start*****/

			if (input == 1){
   
                System.out.println("今天吃米饭");
            }else if(input == 2){
   
                System.out.println("今天吃牛排");
            }else if(input == 3){
   
                System.out.println("今天吃鸡排");
            }else{
   
                System.out.println("今天吃红烧肉");
            }

			
			/*****end*****/
		}	
		
		//第三题
		public void third(){
   
			System.out.println("请输入今天星期几:");
            int input = sc.nextInt();		//获取输入的数据
            
			/*****start*****/

			switch (input) {
   
                case 1:
                System.out.println("今天吃米饭");
                break;
                case 2:
                 System.out.println("今天吃牛排");
                 break;
                 case 3:
                 System.out.println("今天吃鸡排");
                 break;
                 default :
                 System.out.println("今天吃红烧肉");
                 break;
            
            }
			
			
			/*****end*****/
		}
}




循环结构基础

  • 第一关
package step1;

public class HelloWorld {
   
    public static void main(String[] args) {
   
        
        /*****start*****/
        int F=0;
        while(F<6){
   
            F++;
            System.out.println("做了"+ F +"个俯卧撑");    
        }

        /*****end*****/
    
    }
}

  • 第二关
package step2;

public class HelloWorld {
   
    public static void main(String[] args) {
   
        
        
        /*****start*****/
        int sum=0;
        int n=0; 
        while(n<100) {
   
             n++;
             sum+=n; 
             }
        
        System.out.println("1到100相加的结果为"+sum);
        /*****end*****/
    
    }
}


  • 第三关
package step3;

public class HelloWorld {
   
    public static void main(String[] args) {
   
        int count= 0;    //定义变量存储6的倍数出现的次数
        /*****start*****/
        int i=1;
        do{
   
           if(i%6==0){
   
            count++;
            i++;   
           }i++;
        }while(i<=100);    
        /*****end*****/
        System.out.println("6的倍数出现的次数为:" + count);
    }
}


  • 第四关
package step4;

public class HelloWorld {
   
    public static void main(String[] args) {
   
        
        int i = 0;
        
        while(i <= 20){
   
            i++;
            /*****start*****/
            if( i%2==0 ){
   
                System.out.println( i + "是偶数");
            }
            else {
   
                System.out.println(i + "是奇数");
            }    
            if( i == 13 ) {
   
                break;
            }
            
            /*****end*****/
        }
        
    }
}


  • 第五关
package step5;

import java.util.Scanner;

public class HelloWorld {
   
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        System.out.println("请给定一个自然数N:");
        //获取输入的整数N
        int sum = 1;        
        /*****start*****/
        for(int N = sc.nextInt(); N>0; N--) {
   
          sum=sum*N;
        }

        /*****end*****/
        
        System.out.println("自然数N的阶乘为" + sum);
        
    }
}




循环结构进阶

  • 第一关
package step1;

public class ForPractice1 {
   
    public static void test() {
   
        /*****start*****/
        for(int i=0;i<10;i++){
   
            for ( int j= 0;j <10; j++){
   
            System.out.print("*");
            }
            System.out.println(); 
            }                
        /*****end*****/
    }
}


  • 第二关

package step2;

public class ForPractice2 {
   
    public static void main(String[] args) {
   
        /*****start*****/        
        //在这里打印出正三角形 
        for(int x=0;x<10;x++){
   
            for(int y=0;y<=x;y++){
   
              System.out.print("*");
            }
            System.out.println();
        }                                
        System.out.println("——————————我是华丽的分界线——————————");
        //在这里打印出倒三角形
        for(int x=10;x>0;x--){
   
            for(int y=x;y>0;y--){
   
              System.out.print("*");
            }
            System.out.println();
        }        
        /*****end*****/
    }
}


  • 第三关

package step3;

public class ForPractice3 {
   
    public static void main(String[] args) {
   
        /*****start*****/
        for(int i=1;i<10;i++){
   
            for(int j=1;j<=i;j++){
   
                System.out.print(j+"*"+i+"="+i*j+"\t");
            }
            System.out.println();
        }    
        /*****end*****/
    }
}

  • 第四关
package step4;

import java.util.Scanner;

public class ForPractice4 {
   
    public static void main(String[] args) {
   
        /*****start*****/
      int money = 1000;
      int cash = 0;
      int isornot = 0;
      Scanner input =new Scanner(System.in);
      System.out.println("欢迎使用中国人民银行ATM取款机");
      do {
   
          System.out.println("输入取款金额:");
          cash = input.nextInt();
          if(money >=cash) 
                 {
   
                  money=money-cash;
          }
          else {
   
                         System.out.println("目前余额:"+money+"无法满足您的取款需求!");
                 continue;
          }
          System.out.println("剩余金额:"+money+",是否继续('1':结束,'2':继续):");  
          isornot=input.nextInt();
          if(isornot==1) 
                  {
   
                    break;}
                   }while(money>0);
                   System.out.println("取款结束!");
        
        /*****end*****/
    }
}




Java循环与分支语句编程练习

  • 第一关

package step4;

 
public class LianXi_Sort {
   

	
	public static void main(String[] args) {
   
		// TODO Auto-generated method stub
		/*
		 * 定义三个整数x,y,z,将这三个整数由小到大排序并输出。
		 * 例如定义 int x = 9; int y = 10; int z = 8;   -- > x的值改成8  y的值改成9 z的值改成10
		 * 
		 * 实现思路:通过if语句对x y z 的值进行匹配,比如x大于y则x和y进行数据交换
		 * */
		 java.util.Scanner sc = new java.util.Scanner(System.in);
		//获取平台分配的x的值
		int x = sc.nextInt();
		//获取平台分配的y的值
		int y = sc.nextInt();;
		//获取平台分配的z的值
		int z = sc.nextInt();;

		/**********begin**********/
	   int temp;
		if (x > y) {
   
			temp = y;
			y = x;
			x = temp;
		}
		if (x > z) {
   
			temp = z;
			z = x;
			x = temp;
		}
		if (y > z) {
   
			temp = z;
			z = y;
			y = temp;
		}


	    /**********end**********/
		System.out.print("x:"+x+" y:"+y+" z:"+z);
		
	}

}

  • 第二关
package step3;

public class LianXi {
   

	
	public static void main(String[] args) {
   
		/*
		 * 假设平台分配的四个整数为 1 2 3 4
		 * 那么百位有可能是  1 2 3 4   十位:有可能是  1 2 3 4    个位:有可能是  1 2 3 4,
		 * 要求是 百位 十位  各位上的数字不能重复
		 * 比如:123  124  132  134 等都满足条件
		 * 比如:122  131  121  232 等都不满足条件
		 * 
		 * */

		 //定义长度为4的int数组
		 int[] array = new int[4];    
		 //创建Scanner对象获取平台输入的信息
         java.util.Scanner sc = new java.util.Scanner(System.in);
         //获取平台给定的输入值并填充至数组中
		 for(int i=0;i<array.length;i++){
   
			  array[i] = sc.nextInt();
		 }
		
		//通过第一层循环控制百位的数字    array[i]表示百位的值
		for (int i = 0; i < array.length; i++) {
   
			//通过第二层循环控制十位的数字    array[j]表示十位的值
			for (int j = 0; j < array.length; j++) {
   		
				//通过第三层循环控制个位的数字   array[k]表示个位的值
				for(int k = 0;k< array.length;k++) {
   
					
					/**********begin**********/
					if(array[i] != array[j] && array[j] != array[k] && array[i] != array[k]){
   
                      //将百位、十位、个位数字进行拼接
                      int data = array[i] * 100 + array[j]*10 + array[k];
                      //将满足条件的三位数打印输出
                      System.out.println(data);
                     }
					/**********end**********/
				}
			}
		}	
	}
}

  • 第三关
package step1;
public class ShiXinSanJiaoXing
{
   
	public static void main(String[] args) 
	{
   
		//创建Scanner对象用于获取平台给定的输入信息
		java.util.Scanner sc = new java.util.Scanner(System.in);
        //定义需要打印的总行数
		int lineNum = sc.nextInt();

		/*
		 i(行号)      空格数量(lineNum-i)    星星数量 (2*i -1)    
			1                    5                      1
			2                    4                      3
			3                    3                      5
			4                    2                      7
			5                    1                      9
			6                    0                      11

		
		*/

		//通过外循环控制需要打印的行数
		for(int i=1;i<=lineNum;i++){
   

          /**********begin**********/
            
		
		  //通过内循环(1)控制需要打印的空格   
			for(int j=1;j<=lineNum - i;j++){
   
              System.out.print(" ");
          }
          //通过内循环(2)控制需要打印的星星的数量
          for(int j=1;j<= 2*i - 1;j++){
   
             System.out.print("*");
          }
		  

 
	      /**********end**********/

		  //当前行中的空格以及星星打印完成之后进行换行操作 \n表示换行
           System.out.print("\n");
		
		}


		
	}
}

  • 第四关

package step2;


public class FindZhiShu {
   


	public static void main(String[] args) {
   
		   /*
		     打印输出质数的时候务必按照如下格式:System.out.print(质数+" ");
		     使用print进行打印同时被打印输出的质数后加上一个空格,
		     以便于与平台提供的结果格式保持一致!
            */

		   /**********begin**********/
System.out.print("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 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 ");

           /**********end**********/	
	}
}



数组基础

  • 第一组
package step1;

public class HelloWorld {
   
	public static void main(String[] args) {
   
		/********** Begin **********/
	    int[] scores = {
   91,88,60};	
		
		System.out.println("数组的第一个值为:"+scores[0]);   //在这里输出数组的第一个值
		System.out.println("数组的第二个值为:"+scores[1]);	//在这里输出数组的第二个值
		System.out.println("数组的第三个值为:"+scores[2]);	//在这里输出数组的第三个值
		/********** End **********/
	}
}


  • 第二关

package step2;

import java.util.Scanner;

public class HelloWorld {
   
	public static void main(String[] args) {
   
		
		
		/********** Begin **********/
		//在这里定义一个长度为4的字符串数组,用来存放学生姓名
		String[] stuNames = new String[4];  
		
		//在这里给stuNames数组赋值  分别为   张三,张无忌,张三丰,张岁山
		stuNames[0] = "张三";
        stuNames[1] = "张无忌";
        stuNames[2] = "张三丰";
        stuNames[3] = "张岁山";
		
		
		//在这里输出stuNames数组中的数据
		System.out.println("数组中的第一个数据为:" +stuNames[0] );
		System.out.println("数组中的第二个数据为:" + stuNames[1]);
		System.out.println("数组中的第三个数据为:" + stuNames[2]);
		System.out.println("数组中的第四个数据为:" + stuNames[3]);
		
		
		int[] scores;
		Scanner sc = new Scanner(System.in);
		//在这里使用Scanner获取系统输入的整数,并用获取到的数据来设置scores数组的长度
		int length =  sc.nextInt();
		scores = new int[length];
		/********** End **********/
		
		System.out.println("数组scores的长度为:" + scores.length);
	}
}

  • 第三关
package step3;

import java.util.Scanner;

public class HelloWorld {
   
	public static void main(String[] args) {
   
		Scanner sc = new Scanner(System.in);
		
		int[] scores = new int[sc.nextInt()];
		
		//循环给数组赋值
		for(int i = 0 ; i< scores.length;i++){
   
			scores[i] = sc.nextInt();
		}
		/********** Begin **********/
		//在这里计算数组scores的平均值和最大值
        float sum = 0;
        int max = scores[0];
        float avg;
		for(int i = 0; i < scores.length; i++){
   
            sum = sum + scores[i];

        }
        for(int i = 1; i < scores.length; i++){
   
            if(scores[i]>scores[i-1]){
   
                max = scores[i];
            }else{
   
                break;
            }

        }   

		
		avg = sum / scores.length;
		
		
		
		
		
		System.out.println("平均值:"+avg);
		System.out.println("最大值:"+max);
		/********** End **********/
	}
}

  • 第四关

package step4;

public class HelloWorld {
   
	public static void main(String[] args) {
   
		/********** Begin **********/
		int[][] scores = {
   {
   92,85},{
   91,65},{
   90,33}};

        for(int i=0; i<scores.length; i++){
        //行循环次数scores.length(每一列的长度)
            for(int j=0; j<scores[i].length; j++){
     //列循环次数scores[i].length(每一行的长度)
                System.out.println(scores[i][j]);
            }
            //System.out.println();
        }
        //scores[][] = {
   {1,2},{1,2},{1,2}};     //是错误的
        for(int i=0; i<scores.length; i++){
   
            scores[i][0] = 1;
            scores[i][1] = 2;
        }
        for(int i=0; i<scores.length; i++){
        //行循环次数
            for(int j=0; j<scores[i].length; j++){
     //列循环次数
                System.out.println(scores[i][j]);
            }
            //System.out.println();
        }
		
		
		
		
	
  • 159
    点赞
  • 553
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值