gpt4 book ai didi

java - 如何正确调整数字?

转载 作者:行者123 更新时间:2023-11-30 04:44:31 25 4
gpt4 key购买 nike

如何使一些数字正确对齐?我不明白。我对 Java 真的很陌生。 :-/例如,每当我尝试使此代码正确对齐时,它都会给我错误。这是我找到的一些示例代码:

import java.util.Scanner;

public class JFindAlphabete
{
static Scanner sc = new Scanner(System.in );

public static void main(String[] Theory)
{

JWaffles MyWaffles = new JWaffles();

MyWaffles.ProgramHeading();

System.out.println("Enter a string:" );
String SentenceContents = sc.nextLine( );

int SpaceCount = SentenceContents.length() - SentenceContents.replaceAll(" ", "").length( );
int VowelCount = SentenceContents.length() - SentenceContents.replaceAll("(?i)[aeiou]", "").length( );
int ConsonantCount = SentenceContents.length() - SentenceContents.replaceAll("(?i)(?=[a-z])[^aeiou]", "").length( );
int SpecialCharCount = SentenceContents.length() - SentenceContents.replaceAll("(?i)[^a-z ]", "").length( );
int WordCount = SentenceContents.trim().split("\\s+").length;




System.out.println("There are " + VowelCount + " vowels in this sentance" );
System.out.println("There are " + ConsonantCount + " consonants in this sentance" );
System.out.println("There are " + SpaceCount + " spaces in this sentance" );
System.out.println("There are " + SpecialCharCount + " special characters in this sentance");
System.out.println("There are " + WordCount + " words in this sentance" );






}
}

最佳答案

您可以使用System.outprintf方法而不是 println,并使用格式选项来右调整打印的值:

System.out.printf("There are %5d vowels in this sentance\n"     , VowelCount);
System.out.printf("There are %5d consonants in this sentance\n" , ConsonantCount);

等等。

关于java - 如何正确调整数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11387838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com