简体   繁体   English

大写Java程序

[英]Capitalization Java Program

The directions that were given were: Write a program that prompts for and accepts a line of text from the user, and then prints it out with every character in lower case except for the characters that are immediately after a space. 给出的指示是:编写一个程序,该程序提示并接受用户的一行文本,然后将其打印出来,并以小写字母显示每个字符,但紧接空格之后的字符除外。 These characters are to be capitalized. 这些字符要大写。

NOTE: don't split the string 注意:不要分割字符串

  • Accept a string from user 接受用户的字符串
  • Turn all characters to lower case 将所有字符都转换为小写
  • Go through each character individually and capitalize the characters after the spaces 逐个浏览每个字符并在空格后大写字符

My Current Code: 我当前的代码:

import java.util.Scanner;

public class Capitalize
{
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a line of text:");
    String TextLine = input.next();
    //String FirstLetter = input.next();
    String NewTextLine = " ";
    int Length = TextLine.length();
    System.out.print("The all lower case line of text is: " + TextLine);
    for(int i = 0; i < Length; i++)
    {
        char Letter = TextLine.charAt(i);
        System.out.print(Letter);
        if(Letter != ' ') 
        {
            Letter = Character.toLowerCase(TextLine.charAt(i));
        }
        else
        {
            Letter = Character.toUpperCase(TextLine.charAt(i));
        } 
        NewTextLine = NewTextLine + Letter;
        }
        System.out.print("\nThe new text line of text is: " + NewTextLine);
    }
}

The Output Is: 输出为:

Enter a line of text: aBc Def GHIJ The all lower case line of text is: aBcaBc The new text line of text is: abc 输入一行文本:aBc Def GHIJ所有小写文本行是:aBcaBc新文本行是:abc

This isn't what I want to get as the output. 这不是我想要得到的输出。 I want to take " aBc DeF GHIJ " and get the out put of all lower case " abc def ghij " and the new text line to be " Abc Def Ghij " 我想使用“ aBc DeF GHIJ”并输出所有小写字母“ abc def ghij”,新的文本行为“ Abc Def Ghij”

Can someone please help me to figure out where I went wrong with my code? 有人可以帮我弄清楚我的代码出了什么问题吗? And how to fix it. 以及如何解决。

You're comparing a char with a literal String " " : 您正在将char与文字String " "进行比较:

if(Letter != " ")
   char      String

Change the whitespace by literal character ' ' . 通过文字字符' '更改空格。

if(Letter != ' ')

From Primitive Data Types : 原始数据类型

Character and String Literals 字符和字符串文字

Always use 'single quotes' for char literals and "double quotes" for String literals. 始终对字符文字使用“单引号”,对于字符串文字始终使用“双引号”。

Change 更改

Letter != " "

To

Letter != ' '

Double quotes are for Strings and single quotes are for Characters. 双引号用于字符串,单引号用于字符。

You could also do: 您也可以这样做:

Letter != 040  // Octal
Letter != 32   // Integer
Latter != 0x20 // Hexadecimal

These are the ASCII values for Space. 这些是空格的ASCII值

It seems like you've defined Letter as a char somewhere. 好像您已经在某个地方将Letter定义为一个字符。 Using double-quotes (" ") denotes a String. 使用双引号(“”)表示字符串。 Single quotes (' ' ) denotes a char. 单引号('')表示一个字符。 So, change if(Letter != " ") to if(Letter != ' ') 因此,将if(Letter != " ")更改为if(Letter != ' ')

You are comparing a char to a string. 您正在将char与字符串进行比较。 A primitive type to an object. 对象的原始类型。

Change your code to if(Letter != ' ') 将您的代码更改为if(Letter!='')

正如已经指出的那样,您不应该使用Letter != " " ,而是可以使用Character.isSpaceChar(Letter)Charater.isWhiteSpace(Letter)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM