Practice and considerations for Java knowledge methods

Source: Internet
Author: User

1.1 Practice of methods
1.1.1 Method exercises to get a larger of two data
1.1.2 Case Code IV

package com.itheima_01;import java.util.Scanner;/* * 需求:键盘录入两个数据,返回两个数中的较大值 * * 两个明确: * 返回值类型:int * 参数列表:int a,int b */public class MethodTest {// 返回两个数中的较大值public static int getMax(int a, int b) {if (a > b) {return a;} else {return b;}}public static void main(String[] args) {//创建对象Scanner sc = new Scanner(System.in);//接收数据System.out.println("请输入第一个数据:");int x = sc.nextInt();System.out.println("请输入第二个数据:");int y = sc.nextInt();//调用方法int max = getMax(x,y);System.out.println("max:"+max);}}

Comparison of 1.1.3 method exercises two data is equal
1.1.4 Case Code Five

package com.itheima_01;import java.util.Scanner;/* * 需求:键盘录入两个数据,比较两个数是否相等 * * 两个明确: * 返回值类型:boolean * 参数列表:int a,int b */public class MethodTest2 {//比较两个数是否相等public static boolean compare(int a,int b){if(a==b){return true;}else {return false;}}public static void main(String[] args) {//创建对象Scanner sc = new Scanner(System.in);//接收数据System.out.println("请输入第一个数据:");int a = sc.nextInt();System.out.println("请输入第二个数据:");int b = sc.nextInt();//调用方法boolean flag = compare(a,b);System.out.println("flag:"+flag);}}

1.1.5 method exercises to get a larger value from three data
1.1.6 Case Code VI

package com.itheima_01;import java.util.Scanner;/* * 需求:键盘录入三个数据,返回三个数中的最大值 * * 两个明确: * 返回值类型:int * 参数列表:int a,int b,int c */public class MethodTest3 {// 返回三个数中的最大值public static int getMax(int a, int b, int c) {if (a > b) {if (a > c) {return a;} else {return c;}} else {if (b > c) {return b;} else {return c;}}}public static void main(String[] args) {//创建对象Scanner sc = new Scanner(System.in);//接收数据System.out.println("请输入第一个数据:");int a = sc.nextInt();System.out.println("请输入第二个数据:");int b = sc.nextInt();System.out.println("请输入第三个数据:");int c = sc.nextInt();//调用方法int max = getMax(a,b,c);System.out.println("max:"+max);}}

Invocation of 1.1.7 Void-Modified method
Write a method that outputs 10 times in the console HelloWorld case.
function calls with no explicit return value:
is actually called by the Void type method
Can only be called separately
1.1.8 Case Code VII

package com.itheima_02;/* * 需求:写一个方法,在控制台输出10次HelloWorld案例。 * * 两个明确: * 返回值类型:void * 参数列表:无参数 * * 如果一个方法没有明确的返回值类型,java提供了void进行修饰。 * * void修饰的方法的调用: * A:单独调用 */public class MethodDemo {//在控制台输出10次HelloWorld案例。public static void printHelloWorld() {for(int x=1; x<=10; x++) {System.out.println("HelloWorld");}}public static void main(String[] args) {//单独调用printHelloWorld();//输出调用//System.out.println(printHelloWorld());//System.out.println(void);//赋值调用//void v = printHelloWorld();}}

1.1.9 printing data between 1 and N
1.1.10 Case Code Eight

package com.itheima_02;/* * 需求:写一个方法,传递一个整数(大于1),在控制台打印1到该数据的值。 * * 两个明确: * 返回值类型:void * 参数列表:int n */public class MethodTest {//在控制台打印1到该数据n的值public static void printNumber(int n) {for(int x=1; x<=n; x++) {System.out.println(x);}}public static void main(String[] args) {printNumber(10);System.out.println("-------------------");printNumber(100);}}

1.1.11 Print all narcissus numbers
1.1.12 Case Code IX

package com.itheima_02;/* * 写一个方法,把所有的水仙花数打印在控制台 * * 两个明确: * 返回值类型:void * 参数列表:无参数 */public class MethodTest2 {//把所有的水仙花数打印在控制台public static void printFlower() {for(int x=100; x<1000; x++) {int ge = x%10;int shi = x/10%10;int bai = x/10/10%10;if((ge*ge*ge+shi*shi*shi+bai*bai*bai) == x){System.out.println(x);}}}public static void main(String[] args) {printFlower();}}

Practice and considerations for Java knowledge methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.