JAVA类(手机充电、放音乐示例)
生活随笔
收集整理的這篇文章主要介紹了
JAVA类(手机充电、放音乐示例)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
練習練習
import java.util.Scanner;//包含包public class ex1 {public static void main(String args[])//程序入口{Phone phone = new Phone();//建一個手機phone.logo = "SONY";//弄上個牌子phone.battery.power = 100;//給電池弄上100電Scanner reader = new Scanner(System.in);//為輸入XXX做準備int command = 0;//為選擇用boolean sign = true;//循環標志System.out.printf("輸入1聽音樂\n輸入2充電\n輸入3顯示電量\n");while(sign){command = reader.nextInt();//輸入...switch (command){case 1:System.out.println("聽音樂");phone.music();//播放音樂phone.showpower();//顯示電量break;case 2:System.out.println("充電");phone.showpower();phone.inpower();//充電break;case 3:System.out.println("顯示電量");phone.showpower();break;case 4:sign = false;//結束循環,結束程序。break;default://缺省break;}}} } class Phone//電話類 {String logo;//牌子Battery battery = new Battery();//給電話加上電池Music music = new Music();//電話的音樂void music(){if(music.have())//沒有音樂是不能播放音樂的{if(battery.outpower())//播放音樂就會使用電池System.out.println("music......" + music.song);elseSystem.out.println("battery low");}elseSystem.out.println("song null");}void inpower()//電池充電{if(battery.inpower())System.out.println("電量已充滿");elseshowpower();}void showpower()//顯示電量{System.out.println("當前電量" + battery.power);} }class Battery//電池類 {String logo;short power;boolean inpower()//充電{power += 10;if(power >= 100){power = 100;return true;}else{return false;}}boolean outpower()//使用電量{if(power >= 10){power -= 10;return true;}else{power = 0;return false;}}}class Music {String song = "song1";void list()//音樂清單,【沒空去實現選擇了】{System.out.println("song1");System.out.println("song2");System.out.println("song3");System.out.println("song4");}boolean have()//是否有音樂{if(song == "null")return false;elsereturn true;}boolean down(short num)//下載音樂{switch (num){case 1:song = "song1";break;case 2:song = "song2";break;case 3:song = "song3";break;case 4:song = "song4";break;default:return false;}return true;} }總結
以上是生活随笔為你收集整理的JAVA类(手机充电、放音乐示例)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js 创建快捷方式
- 下一篇: 怎样让VB6程序只能运行一次