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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

我需要驗(yàn)證用戶輸入的字符串的索引是否有效

我需要驗(yàn)證用戶輸入的字符串的索引是否有效

猛跑小豬 2023-10-13 15:16:20
當(dāng)用戶輸入單詞時(shí),系統(tǒng)會(huì)提示他們輸入子字符串的第一個(gè)和第二個(gè)索引。我已經(jīng)排序了這么多,但是當(dāng)我嘗試驗(yàn)證使用下面的 if 語句輸入的索引時(shí),我收到了有關(guān)這些 if 語句的錯(cuò)誤,import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        String inputString;        int startIndex;        int endIndex;        System.out.println("Enter a string : ");        inputString = scanner.nextLine();        System.out.println("Enter the first index of the substring : ");        startIndex = scanner.nextInt();        if (int startIndex > inputSting.length) {            System.out.println("Index is not in string length, try again.");        }        System.out.println("Enter the second index of the substring : ");        endIndex = scanner.nextInt();        if (int endIndex > inputSting.length) {            System.out.println("Index is not in string length, try again.");        }         char[] ch = new char[endIndex - startIndex + 1];        inputString.getChars(startIndex, endIndex + 1, ch, 0);        System.out.println("Output : " + String.valueOf(ch));    }}
查看完整描述

2 回答

?
天涯盡頭無女友

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊

你的代碼絕對(duì)不是Java語言。

第一的:

if (int startIndex > inputSting.length) { .. }

您不能在if語句內(nèi)或比較表達(dá)式中聲明變量,而且該變量startIndex已經(jīng)存在。

第二:

inputSting變量不存在并且它肯定沒有屬性lengthinputString可能是+ string 有方法length()而不是屬性的拼寫錯(cuò)誤。


查看完整回答
反對(duì) 回復(fù) 2023-10-13
?
開滿天機(jī)

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊

更改以下內(nèi)容:


int startIndex到startIndex,


int endIndex到endIndex,


inputSting.length到inputString.length()


所以固定代碼,


 public class Main {

     public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String inputString="";

        int startIndex;

        int endIndex;


        System.out.println("Enter a string : ");

        inputString = scanner.nextLine();


        System.out.println("Enter the first index of the substring : ");

        startIndex = scanner.nextInt();


        if ( startIndex > inputString.length()) {

            System.out.println("Index is not in string length, try again.");

        }



        System.out.println("Enter the second index of the substring : ");

        endIndex = scanner.nextInt();


        if ( endIndex > inputString.length()) {

            System.out.println("Index is not in string length, try again.");

        }

        char[] ch = new char[endIndex - startIndex + 1];

        inputString.getChars(startIndex, endIndex + 1, ch, 0);

        System.out.println("Output : " + String.valueOf(ch));

    }

 }


查看完整回答
反對(duì) 回復(fù) 2023-10-13
  • 2 回答
  • 0 關(guān)注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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