标签:棋盘 编写 二维数组 二维 ber 输入 equal line justify
一.五子棋盘
利用二维数组输出一个15*15的棋盘,棋盘的原素为“+”,就是输出一个横纵都是15个“+”的二维数组,然后再以坐标形式输入棋子“●”,替换掉原来棋盘里的“+”。再编写是否游戏结束的代码。
二.数字转换
源代码:
import java.util.Scanner;
public class zhuanhuan
{
private String[] hanArr = {"零" , "一" , "二" , "三" , "四" ,
"五" , "六" , "七" , "八" , "九"};
private String[] unitArr = {"十" , "百" , "千","万","十万","百万"};
private String toHanStr(String numStr)
{
String result = "";
int numLen = numStr.length();
for (int i = 0 ; i < numLen ; i++ )
{
int num = numStr.charAt(i) - 48;
if ( i != numLen - 1 && num != 0)
{
result += hanArr[num] + unitArr[numLen - 2 - i];
}
else
{
if(result.length()>0 && hanArr[num].equals("零") && result.charAt(result.length()-1)==‘零‘)
continue;
result += hanArr[num];
}
}
if(result.length()==1)
return result;
int index=result.length()-1;
while(result.charAt(index)==‘零‘)
{
index--;
}
if(index!=result.length()-1)
return result.substring(0,index+1);
else
{
return result;
}
}
public static void main(String[] args)
{
zhuanhuan nr = new zhuanhuan();
System.out.println("只支持整数(0~百万)");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个百万以下的整数:");
String str = scanner.next();
System.out.println(nr.toHanStr(str));
}
}
运行结果截图:
三.大数
源代码:
import java.util.Scanner;
public class dashu
{
public static void main(String args[])
{
System.out.println("请输入第一个数:");
Scanner scanner = new Scanner(System.in);
String input1 = scanner.next();
System.out.println("请输入第二个数:");
String input2 = scanner.next();
int[] number1 = new int[100];
int[] number2 = new int[100];
int[] number3 = new int[100];
int[] number4 = new int[100];
int a = 0;
int b = 0;
for(int i = input1.length() - 1; i >= 0; i--)
{
int number = input1.charAt(i) - 48;
number1[a] = number;
a++;
}
for(int i = input2.length() - 1; i >= 0; i--)
{
int number = input2.charAt(i) - 48;
number2[b] = number;
b++;
}
if(input1.length() > input2.length())
{
for(int i = 0; i < b; i++)
{
number3[i] = number1[i] + number2[i];
}
for(int i = b; i < a; i++)
{
number3[i] = number1[i];
}
for(int i = input1.length() - 2; i >= 0; i--)
{
if(number3[i]>9)
{
number3[i] = number3[i] - 10;
number3[i + 1] = number3[i + 1] + 1;
}
}
System.out.println("两数相加的和为:");
for(int i = input1.length() - 1; i >= 0; i--)
{
System.out.print(number3[i]);
}
}
else if(input1.length() <input2.length())
{
for(int i = 0; i < a; i++)
{
number3[i] = number1[i] + number2[i];
}
for(int i = a; i < b; i++)
{
number3[i] = number2[i];
}
for(int i = input2.length() - 1; i >= 0; i--)
{
if(number3[i]>9)
{
number3[i] = number3[i] - 10;
number3[i + 1] = number3[i + 1] + 1;
}
}
System.out.println("两数相加的和为:");
for(int i = input2.length() - 1; i >= 0; i--)
{
System.out.print(number3[i]);
}
}
else
{
for(int i = 0; i < a; i++)
{
number3[i] = number1[i] + number2[i];
}
for(int i = input2.length() - 2; i >= 0; i--)
{
if(number3[i]>9)
{
number3[i] = number3[i] - 10;
number3[i + 1] = number3[i + 1] + 1;
}
if(number3[input2.length() - 1] > 9)
{
}
}
System.out.println("两数相加的和为:");
for(int i = input2.length() - 1; i >= 0; i--)
{
System.out.print(number3[i]);
}
}
System.out.println();
if(input1.length() > input2.length())
{
for(int i = 0; i < b; i++)
{
number3[i] = number1[i] - number2[i];
}
for(int i = b; i < a; i++)
{
number3[i] = number1[i];
}
for(int i = input1.length() - 1; i >= 0; i--)
{
if(number3[i] < 0)
{
number3[i] = number3[i] + 10;
number3[i + 1] = number3[i + 1] - 1;
}
}
if(number3[input1.length() - 1] == 0)
{
System.out.println("第一个数减第二个数的差为:");
for(int i = input1.length() - 2; i >= 0; i--)
{
System.out.print(number3[i]);
}
}
else
{
System.out.println("第一个数减第二个数的差为:");
for(int i = input1.length() - 1; i >= 0; i--)
{
System.out.print(number3[i]);
}
}
}
else if(input1.length() < input2.length())
{
for(int i = 0; i < a; i++)
{
number3[i] = number2[i] - number1[i];
}
for(int i = a; i < b; i++)
{
number3[i] = number2[i];
}
for(int i = input2.length() - 1; i >= 0; i--)
{
if(number3[i] < 0)
{
number3[i] = number3[i] + 10;
number3[i + 1] = number3[i + 1] - 1;
}
}
if(number3[input2.length() - 1] == 0)
{
System.out.println("第一个数减第二个数的差为:");
System.out.print("-");
for(int i = input2.length() - 2; i >= 0; i--)
{
System.out.print(number3[i]);
}
}
else
{
System.out.println("第一个数减第二个数的差为:");
System.out.print("-");
for(int i = input2.length() - 1; i >= 0; i--)
{
System.out.print(number3[i]);
}
}
}
else
{
for(int i = 0; i < a; i++)
{
number3[i] = number1[i] - number2[i];
}
for(int i = input2.length() - 2; i >= 0; i--)
{
if(number3[i] < 0)
{
number3[i] = number3[i] + 10;
number3[i + 1] = number3[i + 1] - 1;
}
}
if(number3[input2.length() - 1] == 0)
{
System.out.println("第一个数减第二个数的差为:");
for(int i = input2.length() - 2; i >= 0; i--)
{
System.out.print(number3[i]);
}
}
else
{
System.out.println("第一个数减第二个数的差为:");
for(int i = input2.length() - 1; i >= 0; i--)
{
System.out.print(number3[i]);
}
}
}
}
}
运行结果截图:
四.数组随机数
标签:棋盘 编写 二维数组 二维 ber 输入 equal line justify
原文地址:http://www.cnblogs.com/Conqueredcba/p/6035507.html