这就是我现在所做的事情。我无法弄清楚如何为大于9的数字做这件事。 你可以在String ??
中存储多行import java.io.*;
class sample1 {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number b/w one and nine");
int a = Integer.parseInt(br.readLine());
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("----------------------------------------------------------------------");
System.out.println();
System.out.println();
System.out.println();
System.out.println();
if (a < 10) {
switch (a) {
case 0:
System.out.println(" _ _");
System.out.println("| |");
System.out.println("| |");
System.out.println("| |");
System.out.println("|_ _|");
break;
case 1:
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
break;
case 2:
System.out.println(" _ _");
System.out.println(" |");
System.out.println(" _ _|");
System.out.println("| ");
System.out.println("|_ _");
break;
case 3:
System.out.println(" _ _");
System.out.println(" |");
System.out.println(" _ _|");
System.out.println(" |");
System.out.println(" _ _|");
break;
case 4:
System.out.println("|");
System.out.println("| ");
System.out.println("|_ _|");
System.out.println(" |");
System.out.println(" |");
break;
case 5:
System.out.println(" _ _");
System.out.println("|");
System.out.println("|_ _");
System.out.println(" |");
System.out.println(" _ _|");
break;
case 6:
System.out.println(" _ _");
System.out.println("|");
System.out.println("|_ _");
System.out.println("| |");
System.out.println("|_ _|");
break;
case 7:
System.out.println(" _ _");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
break;
case 8:
System.out.println(" _ _");
System.out.println("| |");
System.out.println("|_ _|");
System.out.println("| |");
System.out.println("|_ _|");
break;
case 9:
System.out.println(" _ _");
System.out.println("| |");
System.out.println("|_ _|");
System.out.println(" |");
System.out.println(" _ _|");
break;
}
}
}
}
答案 0 :(得分:1)
是的,您可以使用行分隔符变量在String中存储多行。
String seperator = System.getproperty("line.seperator");
String lines = "line1"+seperator+"line2"+seperator+"line3"; // etc.
但要打印所有整数数字,您可以通过一些简单的字符串操作来执行以下操作。
public static void printInt(int i){
String str = i+"";
for(char ch : str){
int a = Integer.parseInt(ch+"");
switch (a) {
case 0:
System.out.println(" _ _");
System.out.println("| |");
System.out.println("| |");
System.out.println("| |");
System.out.println("|_ _|");
break;
case 1:
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
break;
case 2:
System.out.println(" _ _");
System.out.println(" |");
System.out.println(" _ _|");
System.out.println("| ");
System.out.println("|_ _");
break;
case 3:
System.out.println(" _ _");
System.out.println(" |");
System.out.println(" _ _|");
System.out.println(" |");
System.out.println(" _ _|");
break;
case 4:
System.out.println("|");
System.out.println("| ");
System.out.println("|_ _|");
System.out.println(" |");
System.out.println(" |");
break;
case 5:
System.out.println(" _ _");
System.out.println("|");
System.out.println("|_ _");
System.out.println(" |");
System.out.println(" _ _|");
break;
case 6:
System.out.println(" _ _");
System.out.println("|");
System.out.println("|_ _");
System.out.println("| |");
System.out.println("|_ _|");
break;
case 7:
System.out.println(" _ _");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
break;
case 8:
System.out.println(" _ _");
System.out.println("| |");
System.out.println("|_ _|");
System.out.println("| |");
System.out.println("|_ _|");
break;
case 9:
System.out.println(" _ _");
System.out.println("| |");
System.out.println("|_ _|");
System.out.println(" |");
System.out.println(" _ _|");
break;
}
}
}
它的工作原理是将整数转换为String
,然后循环遍历每个字符,每次都将其转换为int
。
答案 1 :(得分:1)
嗯,我做了解决方案,因为我发现它很有趣,之前从未做过这样的事情。所以对于解决方案你只是:
问题是控制台总是按顺序写入,因此在进入下一行之前,您必须事先知道要打印的内容。
以下是代码:
/**
* Created by rafael on 4/18/15.
*/
import java.io.*;
public class DigitalNumber {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number b/w one and nine");
String input = br.readLine();
int a = Integer.parseInt(new StringBuilder(input).reverse().toString());
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("----------------------------------------------------------------------");
System.out.println();
System.out.println();
System.out.println();
System.out.println();
int line = 0;
while(line < 5) {
int aux = a;
while(aux > 0) {
int digit = aux % 10;
aux /= 10;
printDigit(digit,line);
System.out.print(" ");
}
++line;
System.out.println();
}
}
public static void printDigit(int number, int line) {
switch (number) {
case 0:
if(line == 0)
System.out.print(" _ _ ");
if(line == 1)
System.out.print("| |");
if(line == 2)
System.out.print("| |");
if(line == 3)
System.out.print("| |");
if(line == 4)
System.out.print("|_ _|");
break;
case 1:
if(line == 0)
System.out.print("|");
if(line == 1)
System.out.print("|");
if(line == 2)
System.out.print("|");
if(line == 3)
System.out.print("|");
if(line == 4)
System.out.print("|");
break;
case 2:
if(line == 0)
System.out.print(" _ _ ");
if(line == 1)
System.out.print(" |");
if(line == 2)
System.out.print(" _ _|");
if(line == 3)
System.out.print("| ");
if(line == 4)
System.out.print("|_ _ ");
break;
case 3:
if(line == 0)
System.out.print(" _ _ ");
if(line == 1)
System.out.print(" |");
if(line == 2)
System.out.print(" _ _|");
if(line == 3)
System.out.print(" |");
if(line == 4)
System.out.print(" _ _|");
break;
case 4:
if(line == 0)
System.out.print("| ");
if(line == 1)
System.out.print("| ");
if(line == 2)
System.out.print("|_ _|");
if(line == 3)
System.out.print(" |");
if(line == 4)
System.out.print(" |");
break;
case 5:
if(line == 0)
System.out.print(" _ _ ");
if(line == 1)
System.out.print("| ");
if(line == 2)
System.out.print("|_ _ ");
if(line == 3)
System.out.print(" |");
if(line == 4)
System.out.print(" _ _|");
break;
case 6:
if(line == 0)
System.out.print(" _ _ ");
if(line == 1)
System.out.print("| ");
if(line == 2)
System.out.print("|_ _ ");
if(line == 3)
System.out.print("| |");
if(line == 4)
System.out.print("|_ _|");
break;
case 7:
if(line == 0)
System.out.print(" _ _ ");
if(line == 1)
System.out.print(" |");
if(line == 2)
System.out.print(" |");
if(line == 3)
System.out.print(" |");
if(line == 4)
System.out.print(" |");
break;
case 8:
if(line == 0)
System.out.print(" _ _ ");
if(line == 1)
System.out.print("| |");
if(line == 2)
System.out.print("|_ _|");
if(line == 3)
System.out.print("| |");
if(line == 4)
System.out.print("|_ _|");
break;
case 9:
if(line == 0)
System.out.print(" _ _ ");
if(line == 1)
System.out.print("| |");
if(line == 2)
System.out.print("|_ _|");
if(line == 3)
System.out.print(" |");
if(line == 4)
System.out.print(" _ _|");
break;
}
}
}