为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

qq_遁去的一_1 2023-09-20 15:25:17
作为一个入门项目,我正在编写一个写出 pi 数字的代码。该代码提示输入 int,并打印 pi 的小数位数,这使用预先确定的字符串。问题是每个单独的数字都在单独的行上,如下所示:3.14159该代码使用了一个for循环,该循环在扫描仪给出的索引处结束。它一次打印一个字符。我正在努力寻找一种方法让代码分别打印 pi 的十位数字子字符串,每个子字符串都在自己的行上,这意味着 26 位数字将像这样打印:3 .14159265358979323846264338这是两组,每组十个,第三组包含其余六个。我正在努力将字符串打印分成十组,并且仍然在给定索引处停止打印,即使该索引不是十的倍数。另外,旧代码中的for循环用于数字计数器,所以我想保留它。将“/n”放入字符串中以形成空格会中断计数系统,因为这些字符也会被计数和打印,从而使该选项无效。到目前为止我还没有任何其他想法来解决这个问题。如果您知道以这种方式打印的方法,请告诉我。我的代码如下,请随意复制它并进行实验,如果您决定在某个地方使用它,请相信我。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条经验 获得超11个赞

尝试使用System.out.print而不是System.out.println.



查看完整回答
反对 回复 2023-09-20
?
阿晨1998

TA贡献2037条经验 获得超6个赞

您可以通过做一些数学运算、几个基本循环以及使用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

然后,使用循环打印结果:


//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();

}

请记住,代码中的整数算术不会舍入;49/10is 4, and 49%10is 9(模或余数运算符)。


该语法boolean ? "something" : "another"称为三元语句,如果它们令人困惑,我可以在没有它们的情况下重写它。您需要处理用户输入等,但永远不要害怕解决问题。从一个可以以可变形式打印输入的 x 个数字的程序开始,然后处理输入。您可以将类似的内容放入方法中printDigitsPI(int decimals)。上面使用的大多数变量都是常量,它们不会改变,也不需要传递。


作为最后一条建议,如果您发现自己将num1, num2, num3... 写为变量,那么很可能有更好的方法来解决它。


查看完整回答
反对 回复 2023-09-20
?
绝地无双

TA贡献1946条经验 获得超4个赞

下面使用 Math.PI 创建 Double PI 的字符串表示形式。然后,我们使用 String#indexOf 确定该字符串中小数点的位置。然后,我们使用 Scanner 请求我们希望看到的小数位数。假设输入是一个非负整数,我们尝试使用初始索引“.”查找字符串 PI 的子字符串。以及剩余的数字。



    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));

        }

    }


查看完整回答
反对 回复 2023-09-20
  • 3 回答
  • 0 关注
  • 76 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信