類和對象

來源:互聯網
上載者:User

標籤:else   int   調用   .sh   equal   input   顯示   str   put   

//定義姓名,年齡,從鍵盤接收,迴圈判斷姓名與年齡並輸出相應的語句
//定義Person類public class Person { String name; //姓名 int age; //年齡 /** * 用來迴圈接收使用者輸入,然後判斷年齡 */ // 顯示資訊方法 public void show(){ Scanner input=new Scanner(System.in); //掃描器從鍵盤輸入 System.out.println("請輸入姓名:"); name=input.next(); //1.進行迴圈 while(!name.equals("n")){ System.out.println("請輸入年齡:"); age=input.nextInt(); //2.進行判斷 if(age>=18&&age<=60){ System.out.println("門票20元"); }else{ System.out.println("門票免費"); } System.out.println("請輸入姓名:"); name=input.next(); } System.out.println("程式結束!"); } //對象 public static void main(String[] args) { //建立一個Person類的對象 /** * 類的執行個體化過程 */ Person person=new Person(); //調用方法 person.show(); }
}

類和對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.