亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

難以適當地構建具有正確條件的做而循環運行

難以適當地構建具有正確條件的做而循環運行

瀟瀟雨雨 2022-09-07 21:51:42
我正在努力正確循環我編寫的代碼以將整數轉換為羅馬數字。我嘗試實現一個 do while 循環來運行代碼,從“請輸入一個整數”開始,在我的 switch 語句之后結束,while 部分是:while(大小寫 “y” ||“Y” == true ) 任何幫助將不勝感激。我已經搜索了幾個小時的堆棧溢出的先前帖子,并且無法找到任何有用的東西。公共類項目8 {/** * Constructor for objects of class Project4 */public static void main(String[] args) {    System.out.println("Welcome to my integer  Roman numeral conversion program");    System.out.println("------------------------------------------------------");    System.out.println(" ");    Scanner in = new Scanner (System.in);    System.out.print("Enter an integer in the range 1-3999 (both inclusive): ");    int input = in.nextInt();    if (input < 0 || input > 3999){        System.out.println("Sorry, this number is outside the range.");        System.out.println("Do you want to try again? Press Y for yes and N for no: ");            String userInput = in.next();                switch (userInput) {                 case "N":                 case "n":                 System.exit(0);                 break;                 case "Y":                 case "y":                break;                }               }     else if (input > 0 && input < 3999);       { System.out.println(Conversion.Convert(input));        }          }}
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

1)您的條件是多余的。您可以使用簡單,因為輸入只能在該范圍內進行。 僅當您要檢查兩個或多個范圍時才有意義,例如if - else ifif - elseelse if


if(input > 0 && input < 3999){ 

  ...

else if (input > 4000 && input < 8000){ 

... 

else { 

...

2) 您不需要開關塊,而是在 while 條件下使用用戶輸入,因為您希望在用戶輸入為 Y/y 時繼續循環,即while(userChoice.equals("Y"))


3) 使用循環,因為您希望應用程序至少按時運行do - while


public static void main(String[] args) {


    System.out.println("Welcome to my integer  Roman numeral conversion program");

    System.out.println("------------------------------------------------------");

    System.out.println(" ");


    Scanner in = new Scanner (System.in);

    String choice;

    do{

        System.out.print("Enter an integer in the range 1-3999 (both inclusive): ");

        int input = in.nextInt();

        if(input > 0 && input < 3999){

            System.out.println(Conversion.Convert(input));

        }

        else{

            System.out.println("Sorry, this number is outside the range.");

        }

        System.out.println("Do you want to try again? Press Y for yes and N for no: ");

        choice = in.next();

    }while(choice.equals("Y") || choice.equals("y"));

}


查看完整回答
反對 回復 2022-09-07
  • 1 回答
  • 0 關注
  • 82 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號