最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

Java oop 題與代碼2:一表中打印九九乘法表,兩變量交換,水仙花,計(jì)算器,理代碼方法

2019-12-10 15:42 作者:詩(shī)書(shū)畫(huà)唱  | 我要投稿

1.使用方法的形式打印99乘法表

package b;


public class d1 {


public static void main(String[] args) {

// TODO Auto-generated method stub

d1.dayin();


}

public static void dayin(){

for(int j=1;j<=9;j++) {


? ? ?for(int i=1;i<=j;i++){

? ? System.out.print(j+"*"+i+"="+i*j+" ");

? ? ? }

? ? ?

? ? ? System.out.println();? ? ? ? ?


}


? ? ?}?

}




2.使用方法的形式實(shí)現(xiàn)兩個(gè)變量的交換(擴(kuò)展,不使用第三變量實(shí)現(xiàn)交換)


package b;


public class d1 {


public static void main(String[] args) {

// TODO Auto-generated method stub

d1.dayin();


}

public static void dayin(){

int a ,b;



a = 1;


b=2;


a=a+b;


b=a-b;


a=a-b;





System.out. println("a= "+a);


System.out. println("b= "+b);


? ? ?}?

}


3.提示用戶輸入一個(gè)數(shù)字,判斷是否是水仙花數(shù)


package b;


import java.util.Scanner;


public class d1 {


public static void main(String[] args) {

// TODO Auto-generated method stub

d1.dayin();


}


public static void dayin(){

System.out.println("判斷是否為水仙花數(shù)?請(qǐng)輸入一個(gè)小于1000的三位數(shù)");






Scanner s=new Scanner(System.in);//聲明Scanner接收的類(lèi)


int a= s.nextInt();//接收用戶輸入整形的數(shù)據(jù)




double x;


double y;


double z;


x=(a/100)*(a/100)*(a/100);


y=(a/10%10)*(a/10%10)*(a/10%10);


z=(a%10)*(a%10)*(a%10);






if (x+y+z==a){System.out.println("是水仙花數(shù)");}




else{System.out.println("不是水仙花數(shù)");


}


? ? ?}

}


4.使用方法的形式打印100-999之間的水仙花數(shù)

package b;


public class d1 {


public static void main(String[] args) {

// TODO Auto-generated method stub

d1.dayin();


}


public static void dayin() {

System.out.println("100-999之間的水仙花數(shù)是\n");


int i, j, k, n;


n = 100;


while (n < 1000)


{

i = n / 100;


j = (n - i * 100) / 10;


k = n % 10;


if (i * i * i + j * j * j + k * k * k == n)


System.out.println(n);


n++;


}


}

}


5.使用方法制作簡(jiǎn)單計(jì)算器(僅可以進(jìn)行加減乘除)


一次性版:

package b;


import java.util.Scanner;


public class d1 {


public static void main(String[] args) {

// TODO Auto-generated method stub

d1.dayin();


}


public static void dayin() {

System.out.println("請(qǐng)輸入一個(gè)算式:(比如2 * 4的格式,記得每輸入一個(gè)數(shù)據(jù)就要按一個(gè)空格,之后按Enter鍵就可以得到結(jié)果)");

Scanner sc= new Scanner(System.in);

int a1=sc.nextInt();

String a2=sc.next();

int a3=sc.nextInt();

if(a2.equals("+")) {

int s=a1 + a3;

System.out.println("reseult="+s);

} else if(a2.equals("-")) {

int s=a1-a3;

System.out.println("reseult="+s);

} else if(a2.equals("*")) {

int s=a1*a3;

System.out.println("reseult="+s);

}else {

System.out.println("輸入符號(hào)有誤,請(qǐng)重新輸入。");

}

sc.close();//這個(gè)函數(shù)是用于將Scanner sc= new Scanner(System.in);系統(tǒng)關(guān)閉(個(gè)人理解,若發(fā)現(xiàn)錯(cuò)了,則我會(huì)修改)


}

}

循環(huán)計(jì)算版:

package b;


import java.util.Scanner;


public class d1 {


public static void main(String[] args) {

// TODO Auto-generated method stub

d1.dayin();


}


public static void dayin() {

Scanner sc= new Scanner(System.in);

while(true){

System.out.println("請(qǐng)輸入第一個(gè)數(shù):");

int a1=sc.nextInt();

System.out.println("請(qǐng)輸入運(yùn)算符號(hào):");

String a2=sc.next();

System.out.println("請(qǐng)輸入第二個(gè)數(shù):");

int a3=sc.nextInt();

if(a2.equals("+")) {

int s=a1 + a3;

System.out.println("結(jié)果="+s);

} else if(a2.equals("-")) {

int s=a1-a3;

System.out.println("結(jié)果="+s);

} else if(a2.equals("*")) {

int s=a1*a3;

System.out.println("結(jié)果="+s);

}else {

System.out.println("輸入符號(hào)有誤,請(qǐng)重新輸入。");

}

}


}

}


理代碼方法:

若代碼沒(méi)錯(cuò),但是報(bào)錯(cuò)了,則可能是代碼太亂?,? 點(diǎn)鼠標(biāo)的右鍵,source ,format 則所有代碼都格式化了,不亂,美觀。



Java oop 題與代碼2:一表中打印九九乘法表,兩變量交換,水仙花,計(jì)算器,理代碼方法的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
佛山市| 合川市| 海安县| 宝丰县| 汉阴县| 来安县| 柳林县| 庆云县| 永靖县| 喀喇沁旗| 藁城市| 甘南县| 开化县| 彰化市| 闵行区| 秦皇岛市| 桂平市| 肇源县| 南昌市| 汝城县| 荆门市| 永寿县| 泸州市| 安徽省| 内黄县| 柘荣县| 黔东| 桑植县| 兴隆县| 拜泉县| 平利县| 满城县| 河池市| 凌海市| 七台河市| 阿克| 盐源县| 南华县| 仁寿县| 海城市| 云南省|