- import java.util.Scanner;
- class Dou //定义一个Dou类,里面包括Dou方法接收数值,getInfo()方法计算;
- {
- private int p;
- public Dou(int p) //接收参数
- {this.p=p;
- }
- public int getInfo(){ //计算返回值;
- p=p*2;
- return p;
- }
- }
- public class DoubleNumber {
-
- public static void main(String[] args) {
- while(true)
- {
- System.out.println("请输入数字:");
- Scanner sc = new Scanner(System.in);
- int m=sc.nextInt(); //输入数字
-
- Dou num=new Dou(m); //实例化Dou对象,并计算值;
-
-
- System.out.println(num.getInfo());//打印返回值
- }
- }
-
- }
复制代码 |