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

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

如何停止打印不同索引處的字符串?

如何停止打印不同索引處的字符串?

qq_遁去的一_1 2023-09-20 15:25:17
作為一個入門項目,我正在編寫一個寫出 pi 數(shù)字的代碼。該代碼提示輸入 int,并打印 pi 的小數(shù)位數(shù),這使用預(yù)先確定的字符串。問題是每個單獨的數(shù)字都在單獨的行上,如下所示:3.14159該代碼使用了一個for循環(huán),該循環(huán)在掃描儀給出的索引處結(jié)束。它一次打印一個字符。我正在努力尋找一種方法讓代碼分別打印 pi 的十位數(shù)字子字符串,每個子字符串都在自己的行上,這意味著 26 位數(shù)字將像這樣打?。? .14159265358979323846264338這是兩組,每組十個,第三組包含其余六個。我正在努力將字符串打印分成十組,并且仍然在給定索引處停止打印,即使該索引不是十的倍數(shù)。另外,舊代碼中的for循環(huán)用于數(shù)字計數(shù)器,所以我想保留它。將“/n”放入字符串中以形成空格會中斷計數(shù)系統(tǒng),因為這些字符也會被計數(shù)和打印,從而使該選項無效。到目前為止我還沒有任何其他想法來解決這個問題。如果您知道以這種方式打印的方法,請告訴我。我的代碼如下,請隨意復(fù)制它并進行實驗,如果您決定在某個地方使用它,請相信我。import java.util.Scanner;public class PiWriter {    public static void main(String[] args) {        final String pi = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";        Scanner theScanner = new Scanner(System.in);        System.out.println("How many decimal places of pi do you want to see?\nEnter an integer less than or equal to 100");        int stopAt = theScanner.nextInt();        char currentChar;        int num0 = 0;        int num1 = 0;        int num2 = 0;        int num3 = 0;        int num4 = 0;        int num5 = 0;        int num6 = 0;        int num7 = 0;        int num8 = 0;        int num9 = 0;        //Causes the code to ignore the "3." at the start.        stopAt = stopAt + 2;        for (int loopNum = 0; loopNum < stopAt; loopNum++) {            System.out.println(pi.charAt(loopNum));            currentChar = pi.charAt(loopNum);            //This counts how many times a digit occurs.            switch (currentChar) {                case '0':                    num0++;                    break;                case '1':                    num1++;                    break;                case '2':                    num2++;                    break;                case '3':                    num3++;                case '4':                    num4++;                    break;                case '5':                    num5++;                    break;                case '6':
查看完整描述

3 回答

?
Qyouu

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

嘗試使用System.out.print而不是System.out.println.



查看完整回答
反對 回復(fù) 2023-09-20
?
阿晨1998

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

您可以通過做一些數(shù)學運算、幾個基本循環(huán)以及使用printvs來避免計算邏輯println。首先,我將定義此處使用的變量:


//constants

int DIGITS_PER_LINE = 10; //your number of digits per line

int DIGITS_OFFSET = 2; //always 2, because 0 = '3' and 1 = '.'

String pi = /* your PI string */;


//input

int decimals = 42; //for example, from the user

然后,使用循環(huán)打印結(jié)果:


//We can calculate the number of lines we need from the start

int lines = decimals / DIGITS_PER_LINE; //4, to start

lines += (decimals % DIGITS_PER_LINE > 0) ? 1 : 0; //5, add a line for remainders

System.out.print("3");

System.out.println(decimals > 0 ? "." : "");

for (int line = 0; line < lines; line++) {

    int offset = line * DIGITS_PER_LINE; //line -> result, 0 -> 0, 1 -> 10, 2 -> 20, etc

    int digitsLeft = decimals - offset; //0 -> 42, 1 -> 32, ... , 4 -> 2

    int toPrint = digitsLeft % DIGITS_PER_LINE; //up to the requested digit amount

    for (int i = 0; i < toPrint; i++) {

        System.out.print(pi.charAt(offset + i));

    }

    System.out.println();

}

請記住,代碼中的整數(shù)算術(shù)不會舍入;49/10is 4, and 49%10is 9(?;蛴鄶?shù)運算符)。


該語法boolean ? "something" : "another"稱為三元語句,如果它們令人困惑,我可以在沒有它們的情況下重寫它。您需要處理用戶輸入等,但永遠不要害怕解決問題。從一個可以以可變形式打印輸入的 x 個數(shù)字的程序開始,然后處理輸入。您可以將類似的內(nèi)容放入方法中printDigitsPI(int decimals)。上面使用的大多數(shù)變量都是常量,它們不會改變,也不需要傳遞。


作為最后一條建議,如果您發(fā)現(xiàn)自己將num1, num2, num3... 寫為變量,那么很可能有更好的方法來解決它。


查看完整回答
反對 回復(fù) 2023-09-20
?
絕地無雙

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

下面使用 Math.PI 創(chuàng)建 Double PI 的字符串表示形式。然后,我們使用 String#indexOf 確定該字符串中小數(shù)點的位置。然后,我們使用 Scanner 請求我們希望看到的小數(shù)位數(shù)。假設(shè)輸入是一個非負整數(shù),我們嘗試使用初始索引“.”查找字符串 PI 的子字符串。以及剩余的數(shù)字。



    public static void main(String[] args) {

        requestInput();

    }


    private static void requestInput() {

        String pi = Double.toString(Math.PI);


        int decimalOffset = pi.indexOf(".") + 1;


        try (Scanner scanner = new Scanner(System.in)) {

            System.out.println("Enter the number of decimals of PI you would like to see: ");

            int decimals = scanner.nextInt();


            if (decimals < 0 || decimals > pi.length() + decimalOffset) {

                requestInput();

                return;

            }

            String result = pi.substring(0, decimals + decimalOffset);


            System.out.println(String.format("The result of PI with %s numbers is %s", decimals, result));

        }

    }


查看完整回答
反對 回復(fù) 2023-09-20
  • 3 回答
  • 0 關(guān)注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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