我遇到问题让我的列正确排列。我所要做的就是从用户那里获取输入,然后在一列中打印字母的ASCII字符值,在下一列中打印数字的ASCII值,然后根据输入的输入打印十六进制值的列。 一切都打印得很好,除非我在输入中包含数字。当发生这种情况时,我的数字的十六进制值会标出太多次,而其他所有内容都保持完美排列。
我花了好几个小时试图在不同的地方改变\ t,我只是不知所措,想知道是否有人可以给我任何建议。 此外,我不允许使用printf进行此分配,所以我相信我被锁定使用转义字符来格式化我的打印语句。
提前感谢任何提示或建议!
// This program gets input as a string, then outputs the initial characters,
// ASCII values of the characters and numbers, and hex values for each character.
import java.util.Scanner;
public class CZUnit3Ch12 {
public static void main(String[] args) {
//***** Variable Declaration **********
Scanner stdIn = new Scanner(System.in);
String userInput;
int i;
char character;
//****** End Variable Declaration *********
//****** Begin Program *********
System.out.print("Please enter a string of any length: ");
userInput = stdIn.nextLine();
stdIn.close(); // close the scanner; not used anymore
System.out.print("Initial\t\tASCII (char) \t\tASCII (int)\t\tHex\n"); // headings for each column
for (i = 0; i<userInput.length(); i++)
{
character = userInput.charAt(i);
System.out.print(userInput.charAt(i) + "\t");
if(Character.isDigit(character))
{
System.out.print("\t\t\t");
System.out.print("\t"+Integer.toString(userInput.charAt(i)));
}
else
{
System.out.print("\t" + Integer.toString(userInput.charAt(i)) + "\t");
System.out.print("\t");
}
System.out.print("\t\t\t\t"+Integer.toHexString(character).toUpperCase());
System.out.print("\n");
} // end for
System.out.println(""); // blank line for spacing
System.out.println("Thank you for playing!");
} // end main
} // end class CZUnit3Ch12.java
答案 0 :(得分:1)
我记得在学校做过类似的任务,使用public static void main(String[] args) {
//***** Variable Declaration **********
Scanner stdIn = new Scanner(System.in);
String userInput;
int i;
char character;
boolean bIsDigit = false;
//****** End Variable Declaration *********
//****** Begin Program *********
System.out.print("Please enter a string of any length: ");
userInput = stdIn.nextLine();
stdIn.close(); // close the scanner; not used anymore
System.out.print("\n\nInitial\t\tASCII (char) \t\tASCII (int)\t\tHex\n"); // headings for each column
for (i = 0; i<userInput.length(); i++)
{
character = userInput.charAt(i);
System.out.print(userInput.charAt(i) + "\t");
if(Character.isDigit(character))
{
bIsDigit = true;
System.out.print("\t\t\t");
System.out.print("\t"+Integer.toString(userInput.charAt(i)));
}
else
{
System.out.print("\t" + Integer.toString(userInput.charAt(i)) + "\t");
System.out.print("\t");
}
if(bIsDigit)
System.out.print("\t\t\t"+Integer.toHexString(character).toUpperCase());
else
System.out.print("\t\t\t\t"+Integer.toHexString(character).toUpperCase());
System.out.print("\n");
bIsDigit = false;
} // end for
System.out.println(""); // blank line for spacing
System.out.println("Thank you for playing!");
} // end main
做得很好。
我尝试了你的代码并注意到它只会在用户输入数字时弄乱标签,因为它正在跳过一列。如果您调整标签以适应这种情况,那么事情会排成一行。
$_FILES['file']['size']