挑战编程 程序设计竞赛训练手册-1.6.4 液晶显示屏(LC-Display)
public class problem_LC_Display {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/* for(int k=0;k<10;k++){
System.out.print("|");
}
System.out.println("");
for(int k=0;k<10;k++){
System.out.print(" ");
}
System.out.println("");
for(int k=0;k<10;k++){
System.out.print("-");
}
System.out.println("");*/
//以上代码验证某些字体的空格距离短,可能会导致显示偏移
String n = "0123456789";//显示的内容
int s = 3;//显示的大小
//System.out.println(n);
printNumber(n, s);
}
/*扫描一行,判断一遍数字,按数字和格式输出相应笔画,再扫描一行*/
private static void printNumber(String n, int s) {
char[] num = n.toCharArray();
for (int line = 1; line <= (2 * s + 3); line++) {// 扫描行,其中第1行,第(s+2)行,(2*s+3)行为'-'
// 且行数固定为(2 * s + 3)行
for (int i = 0; i < num.length; i++) {
judgeNum(num[i], s, line);
System.out.print(" ");//每个数字间的空隙
}
System.out.println("");
}
}
/*判断数字,并输出*/
private static void judgeNum(char num, int s, int line) {
// TODO Auto-generated method stub
int l = line;//当前扫描的行数
/*数字的详细算法见 数字2
* 另:可以另写一方法统一调用,以输出数字,下列代码为参考起见,为每个数字都写了一个搭建液晶体的算法
* 调用输出方法概述:
* 获取当前行数,再加以判断数字,
* 如在第一行,1和4输出的都是空格,而其他数字需要输出空格和"-"
* 按判断的结果和输入的变量s,控制输出宽度和输出字符。
* 依次类推……*/
switch (num) { //选择判断数字
case '0': { // 0-0
if ((l == 1) || (l == (2 * s + 3))) { // |0|
for (int width = 1; width <= s + 2; width++) { // 000
if ((width == 1) || (width == s + 2)) { // |0|
System.out.print(" "); // 0-0
} else {
System.out.print("-");
}
}
} else if ((l == s + 2)) {
for (int width = 1; width <= s+2; width++) {
System.out.print(" ");
}
} else {
for (int width = 1; width <= s + 2; width++) {
if ((width == 1) || (width == s + 2)) {
System.out.print("|");
} else {
System.out.print(" ");
}
}
}
}
break;
case '1': { // 000
if ((l == 1) || (l == s + 2) || (l == (2 * s + 3))) { // 00|
for (int width = 1; width <= s + 2; width++) { // 000
System.out.print(" "); // 00|
} // 000
} else {
for (int width = 1; width <= s + 2; width++) {
if (width == s + 2) {
System.out.print("|");
} else {
System.out.print(" ");
}
}
}
}
break;
case '2': {
//0-0
//00|
//0-0
//|00
//0-0
if ((l == 1) || (l == s + 2) || (l == (2 * s + 3))) {//判断是否扫描在 第一行,中间一行,和最后一行
//因为这三行一定输出"-"
for (int width = 1; width <= s + 2; width++) {//对于2来说,这三行输出的一样,则按统一格式循环输出宽度
if ((width == 1) || (width == s + 2)) {//如果循环到 最前和最后一格,则输出空格
System.out.print(" ");
} else { //如果是中间的地方,则输出横线
System.out.print("-");
}
}
} else if (l<(s+2)) {//如果不是在 第一行,中间一行,和最后一行 ,而且行数在上半部分的话
for (int width=1;width<=(s + 2);width++) {//按格式循环2的输出宽度
if(width==(s+2)){ //在上半部分的最右边输出"|"
System.out.print("|");
}else{ //上半部分的其余部分为空格
System.out.print(" ");
}
}
}else {//如果不是在 第一行,中间一行,和最后一行 ,而且行数在2的下半部分的话
for (int width=1;width<=(s + 2);width++) {
if(width==1){ //在下半部分的最左边输出"|"
System.out.print("|");
}else{ //下半部分其余的都输出空格
System.out.print(" ");
}
}
}
}
break;
case '3': {
//0-0
//00|
//0-0
//00|
//0-0
if ((l == 1) || (l == s + 2) || (l == (2 * s + 3))) {
for (int width = 1; width <= (s+2); width++) {
if ((width == 1) || (width == (s+ 2))) {
System.out.print(" ");
} else {
System.out.print("-");
}
}
} else {
for (int width=1;width<=(s+2);width++) {
if(width==(s+2)){
System.out.print("|");
}else{
System.out.print(" ");
}
}
}
}
break;
case '4':{
if(l==s+2){
for (int width = 1; width <= (s + 2); width++){
if(width==1||width==(s+2)){
System.out.print(" ");
}else{
System.out.print("-");
}
}
}else if(l==1||l==2*s+3){
for (int width = 1; width <= (s+ 2); width++){
System.out.print(" ");
}
}else if(l<s+2){
for(int width=1;width<=(s+2);width++){
if(width==1||width==s+2){
System.out.print("|");
}else{
System.out.print(" ");
}
}
}else if(l<2*s+3){
for(int width=1;width<=s+2;width++){
if(width==s+2){
System.out.print("|");
}else{
System.out.print(" ");
}
}
}
}
break;
case '5': {
//0-0
//|00
//0-0
//00|
//0-0
if ((l == 1) || (l == s + 2) || (l == (2 * s + 3))) {
for (int width = 1; width <= s + 2; width++) {
if ((width == 1) || (width == s + 2)) {
System.out.print(" ");
} else {
System.out.print("-");
}
}
} else if (l<(s+2)) {
for (int width=1;width<=(s + 2);width++) {
if(width==1){
System.out.print("|");
}else{
System.out.print(" ");
}
}
}else if(l<(2*s+3)){
for (int width=1;width<=(s + 2);width++) {
if(width==(s+2)){
System.out.print("|");
}else{
System.out.print(" ");
}
}
}
}
break;
case '6':{
//0-0
//|00
//0-0
//|0|
//0-0
if ((l == 1) || (l == s + 2) || (l == (2 * s + 3))) {
for (int width = 1; width <= s + 2; width++) {
if ((width == 1) || (width == s + 2)) {
System.out.print(" ");
} else {
System.out.print("-");
}
}
} else if (l<(s+2)) {
for (int width=1;width<=(s + 2);width++) {
if(width==1){
System.out.print("|");
}else{
System.out.print(" ");
}
}
}else {
for (int width=1;width<=(s + 2);width++) {
if(width==(s+2)||(width==1)){
System.out.print("|");
}else{
System.out.print(" ");
}
}
}
}
break;
case '7':{
//0-0
//00|
//000
//00|
//000
if((l==s+2)||(l==(2*s+3))){
for(int width=1;width<=s+2;width++){
System.out.print(" ");
}
}else if((l==1)){
for(int width=1;width<=s+2;width++){
if(width==1||width==s+2){
System.out.print(" ");
}else {
System.out.print("-");
}
}
}else{
for (int width=1;width<=s+2;width++){
if(width==s+2){
System.out.print("|");
}else{
System.out.print(" ");
}
}
}
}
break;
case '8':{
//0-0
//|0|
//0-0
//|0|
//0-0
if ((l == 1) || (l == s + 2) || (l == (2 * s + 3))) {
for (int width = 1; width <= s + 2; width++) {
if ((width == 1) || (width == s + 2)) {
System.out.print(" ");
} else {
System.out.print("-");
}
}
} else {
for (int width=1;width<=(s + 2);width++) {
if(width==1||width==(s+2)){
System.out.print("|");
}else{
System.out.print(" ");
}
}
}
}
break;
case '9': {
//0-0
//|0|
//0-0
//00|
//0-0
if ((l == 1) || (l == s + 2) || (l == (2 * s + 3))) {
for (int width = 1; width <= s + 2; width++) {
if ((width == 1) || (width == s + 2)) {
System.out.print(" ");
} else {
System.out.print("-");
}
}
} else if (l<(s+2)) {
for (int width=1;width<=(s + 2);width++) {
if(width==(s+2)||width==1){
System.out.print("|");
}else{
System.out.print(" ");
}
}
}else {
for (int width=1;width<=(s + 2);width++) {
if(width==(s+2)){
System.out.print("|");
}else{
System.out.print(" ");
}
}
}
}
break;
default:
break;
}
}
}