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

标签 java string

作为一个入门项目,我正在编写一个写出 pi 数字的代码。该代码提示输入 int,并打印 pi 的小数位数,这使用预先确定的字符串。问题是每个单独的数字都在单独的行上,如下所示:

3
.
1
4
1
5
9

该代码使用了一个for循环,该循环在扫描器给出的索引处结束。它一次打印一个字符。我正在努力寻找一种方法让代码分别打印 pi 的十位数字子字符串,每个子字符串都在自己的行上,这意味着 26 位数字将像这样打印:

3 .
1415926535
8979323846
264338

这是两组,每组十个,第三组包含其余六个。我正在努力将字符串打印分成十组,并且仍然在给定索引处停止打印,即使该索引不是十的倍数。此外,旧代码中的 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':
                    num6++;
                    break;
                case '7':
                    num7++;
                    break;
                case '8':
                    num8++;
                    break;
                case '9':
                    num9++;
                    break;
                default:
                    break;
            }
        }
        //Further ignores "3."
        num3--;
        //Corrects number for message.
        stopAt = stopAt - 2;
        System.out.println("Successfully printed first " + stopAt + " decimal places of pi.\nShow digit count?\nY/N");
        theScanner.nextLine();
        char response = theScanner.nextLine().charAt(0);
        if (response == 'y'||response == 'Y') {
            //This displays how many times each digit occurred.
            if (num0 == 1) {
                System.out.println("1 zero");
            }
            else {
                System.out.println(num0 + " zeroes");
            }
            if (num1 == 1) {
                System.out.println("1 one");
            }
            else {
                System.out.println(num1 + " ones");
            }
            if (num2 == 1) {
                System.out.println("1 two");
            }
            else {
                System.out.println(num2 + " twos");
            }
            if (num3 == 1) {
                System.out.println("1 three");
            }
            else {
                System.out.println(num3 + " threes");
            }
            if (num4 == 1) {
                System.out.println("1 four");
            }
            else {
                System.out.println(num4 + " fours");
            }
            if (num5 == 1) {
                System.out.println("1 five");
            }
            else {
                System.out.println(num5 + " fives");
            }
            if (num6 == 1) {
                System.out.println("1 six");
            }
            else {
                System.out.println(num6 + " sixes");
            }
            if (num7 == 1) {
                System.out.println("1 seven");
            }
            else {
                System.out.println(num7 + " sevens");
            }
            if (num8 == 1) {
                System.out.println("1 eight");
            }
            else {
                System.out.println(num8 + " eights");
            }
            if (num9 == 1) {
                System.out.println("1 nine");
            }
            else {
                System.out.println(num9 + " nines");
            }
        }
    }
}

最佳答案

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

关于java - 如何停止打印不同索引处的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58204761/

相关文章:

python - 用时间戳分割字符串

algorithm - 通过相似性匹配 2 个字符串列表

java - 读取 VC++ CArchive 二进制格式(或 Java 读取 (CObArray))

java - JPA 2.0 - 使用@NamedNativeQuery 的 Oracle 函数 - 错误

java - JPA多语言排序

javascript - 如何分割字符串并重复字符?

Mysql:将字符串拆分为字符和数字

java - 在JAVA中运行时使用变量中的名称运行一个类

java - 我能否保证我的 Java 应用程序将使用具有限制性策略的安全管理器执行?

Java 字符串连接和 System.out.println 不起作用