close
標題:

JAVA程式設計

 

此文章來自奇摩知識+如有不便請留言告知

發問:

請各位大大幫忙: 請幫設計一個Java程式, 在開始執行時螢幕會出現文字如下: (1)公斤轉換成磅 (2)磅轉換成公斤 (3)程式結束 請輸入您的選擇: 然後可以接受輸入的選擇, 並加以判斷: 如果使用者選擇1, 則要求使用者輸入公斤數, 轉換成磅後輸出; 如果使用者選擇2, 則要求使用者輸入磅數, 轉換成公斤後輸出; 如果使用者選擇3, 則結束程式; 如果使用者輸入不為1或2或3, 則提示並要求重新輸入選項; 一磅等於0.454公斤 更新: 我還有一個問題,就是在我選擇1執行轉換輸出後,要選擇2時可以再回到 "請輸入你的選擇" 的迴圈嗎?(也就是每一各選擇輸出後再回到 "請輸入你的選擇" 的迴圈) 如果可以程式要如何修改?

最佳解答:

package demo; import java.util.Scanner; public class DemoString { public static void main(String args[]) { Scanner s = new Scanner(System.in); int nextInt = 0; System.out.println("請輸入選擇"); do{ nextInt = s.nextInt(); if(nextInt==1){ System.out.println(s.nextFloat()*(1/0.454)); }else if(nextInt==2){ System.out.println(s.nextFloat()*0.454); }else if(nextInt==3){ System.out.println("結束程式"); break; }else{ System.out.println("輸入錯誤,請重新輸入"); } }while(true); } } 2008-12-11 09:05:03 補充: 在do{ System.out.println("請輸入選擇"); //搬到這裡來 nextInt = s.nextInt(); ... }while(true); 這就可以了

其他解答:6FE1C172A843305D
arrow
arrow

    yffuhxy 發表在 痞客邦 留言(0) 人氣()