第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

使用 do-while 循環(huán)的密碼檢查器

使用 do-while 循環(huán)的密碼檢查器

Smart貓小萌 2024-01-28 20:28:13
我朋友在我們的編程活動中提出的問題是編寫一段代碼,從鍵盤讀取單詞,直到最后輸入單詞“cherry”或“CHERRY”。對于除done之外的每個單詞,報告其第一個字符是否等于其最后一個字符。對于所需的循環(huán),請使用 do-while 語句。例子輸入單詞:mart第一個字符不等于最后一個字符:mart輸入一個單詞:tart第一個字符等于最后一個字符:tart輸入一個詞:cherry程序正在終止...package liniper;import java.util.Scanner;public class Liniper {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        String pass = "cherry";        do{            System.out.println("Enter a word: ");            pass = in.nextLine();        } while("cherry".equal(pass))    } }
查看完整描述

1 回答

?
海綿寶寶撒

TA貢獻1809條經(jīng)驗 獲得超8個贊

你可以這樣做:


public static void main(String[] args) {


    final String pass = "cherry"; // Defining the pass variable

    String word; // Defining the word variable

    boolean running = true; // Defining the running variable


    Scanner scanner = new Scanner(System.in); // Making new Scanner Instance


    do {

        System.out.print("Enter a word: "); // Informing the User to enter a word

        word = scanner.nextLine(); // Retrieving the Word from Input


        char first = word.charAt(0); // Get the first character of the word by using the #charAt(int) method

        char last = word.charAt(word.length() - 1); // Get the second character by looking at the total length of the word and subtracting 1


        // Make the word lowercase and see if it matches the "pass" variable we defined

        if (word.toLowerCase().equals(pass)) {

            running = false; // Stop the loop

            System.out.println("The program is now terminating..."); // Informing the user that the program stops


            // Checking if the first character equals the last

        } else if (first == last) {

            // Informing the user that the first character is equal to the last last character

            System.out.println("The first character is equal to it's last character:" + word);

        } else {

            // Informing the user that the first character is not equal to the last last character

            System.out.println("The first character is not equal to it's last character:" + word);

        }

    } while (running);

}

我希望這可以幫助你。


查看完整回答
反對 回復(fù) 2024-01-28
  • 1 回答
  • 0 關(guān)注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號