课程作业01 2017.10.7

1.回文数

设计思想:通过对数字的位数进行判断,将各个数字为分离开,判断对应的位数是否相等来判断是否为回文数

import javax.swing.JOptionPane;
public class Number
{
public static void main(String args[])
{
int number=0,d5,d4,d3,d2,d1;
String str=JOptionPane.showInputDialog("输入一个 1 至 99999 之间的数");
number=Integer.parseInt(str);
if(number>=1&&number<=99999) //判断 number 在 1 至 99999 之间的条件,且&&,或||
{
d5=number%100000/10000;//计算 number 的最高位(万位)d5
d4=number%10000/1000;//计算 number 的千位 d4
d3=number%1000/100;//计算 number 的百位 d3
d2=number%100/10;
d1=number%10;
if(d5!=0) //判断 number 是 5 位数的条件
{
System.out.println(number+"是 5 位数");
if(d1==d5&&d2==d4) //判断 number 是回文数的条件
{
System.out.println(number+"是回文数");
}
else
{
System.out.println(number+"不是回文数");
}
}
else if(d4!=0) //判断 number 是 4 位数的条件
{
System.out.println(number+"是 4 位数");
if(d1==d4&&d2==d3) //判断 number 是回文数的条件码
{
System.out.println(number+"是回文数");
}
else
{
System.out.println(number+"不是回文数");
}
}
else if(d3!=0) //判断 number 是 3 位数的条件
{
System.out.println(number+"是 3 位数");
if(d1==d3) //判断 number 是回文数的条件
{
System.out.println(number+"是回文数");
}
else
{
System.out.println(number+"不是回文数");
}
}
else if(d2==0)
{
System.out.println(number+"是 2 位数");
if(d1==d2)
{
System.out.println(number+"是回文数");
}
else
{
System.out.println(number+"不是回文数");
}
}
else if(d1!=0)
{
System.out.println(number+"是 1 位数");
System.out.println(number+"是回文数");
}
}
else
{
System.out.printf("\n%d 不在 1 至 99999 之间",number);
}
}
}

程序结果:

 

 流程图:

                                   

       

2. Elementary:

public class Elementary
{
public static void main(String[] args)
{
int i=1234578900+987543210;
System.out.println(i);
long j=1234578900+987543210;
System.out.println(j);
long k=1234578900+987543210L;
System.out.println(k);
long l=1234578900L+987543210L;
System.out.println(l);
}
}

3.Elementary2

public class Elementary2
{
public static void main(String[] args)
{
System.out.println("H"+"Bird");
System.out.println('H'+"Bird");
String s="";
char c=65;
int i=1;
System.out.println(c);
System.out.println(i);
System.out.println(c+1);
System.out.println(c++);
System.out.println(c+i);
System.out.println(s+c);
System.out.println(s+i);
System.out.println(s+c+i);
System.out.println(i+c+s);
s=s+i;
System.out.println(s);
s=s+c;
System.out.println(s);
}
}

 

4.calculate


// An addition program
import javax.swing.JOptionPane; // import class JOptionPane
public class calculate
{
public static void main( String args[] )
{
String firstNumber, // first string entered by user
secondNumber; // second string entered by user
int number1, // first number to add
number2, // second number to add
sum, // sum of number1 and number2
minus,
multiply,
divide;
// read in first number from user as a string
firstNumber =
JOptionPane.showInputDialog( "Enter first integer" );
// read in second number from user as a string
secondNumber =
JOptionPane.showInputDialog( "Enter second integer" );
// convert numbers from type String to type int
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );
// add the numbers
sum = number1 + number2;
minus = number1 - number2;
multiply = number1 * number2;
divide = number1 / number2;
// display the results
JOptionPane.showMessageDialog(
null,
"The sum is " + sum,
"Results",JOptionPane.PLAIN_MESSAGE );
JOptionPane.showMessageDialog(
null,
"The minus is " + minus,
"Results",JOptionPane.PLAIN_MESSAGE );
JOptionPane.showMessageDialog(
null,
"The multiply is " + multiply,
"Results",JOptionPane.PLAIN_MESSAGE );
JOptionPane.showMessageDialog(
null,
"The divide is " + divide,
"Results",JOptionPane.PLAIN_MESSAGE );
System.exit( 0 ); // terminate the program
}
}

流程图:

        

 

 

 

posted @ 2017-10-07 23:30  cts1234  阅读(241)  评论(0编辑  收藏  举报