试题三
来源:互联网 发布:phpfpm与php 编辑:程序博客网 时间:2024/04/29 06:46
import java.io.*;
class StringToBinary
{
static boolean num=true;
char [] c=null;
int x;
public void isNumberic(String s) //extends NumberFormatException
{
c=s.toCharArray();
for(int i=0;i<c.length;i++)
{
if(Character.isDigit(c[i])==false)
{
num=false;
System.out.println("Input the string is not numberic.");
}
}
if(num==true&&Integer.parseInt(s)<Integer.MAX_VALUE)
{
num=true;
}
else
{
System.out.println("超出了int范围");
throw new NumberFormatException(s);
}
}
public void stringToBinary(String s2) //throws NumberFormatException
{
String s=new String();
x=Integer.parseInt(s2);
while(x!=0)
{
if(x%2==1)
{
s+="1";
x=x/2;
}
else if(x%2==0)
{
s+="0";
x=x/2;
}
}
System.out.println("The String:"+s2+"'s Binary is:");
System.out.println(new StringBuffer(s).reverse());
}
public static void main(String [] args)
{
String s1=new String();
System.out.println("Please input the number string:");
try
{
BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
s1=bfr.readLine();
/* if(s1==null)
{
System.out.println("You input the number string is null!");
throw new NullPointerException();
}*/
}
catch (Exception e )
{
e.printStackTrace();
}
StringToBinary stb=new StringToBinary();
stb.isNumberic(s1);
if(num==true)
{
stb.stringToBinary(s1);
}
}
}
class StringToBinary
{
static boolean num=true;
char [] c=null;
int x;
public void isNumberic(String s) //extends NumberFormatException
{
c=s.toCharArray();
for(int i=0;i<c.length;i++)
{
if(Character.isDigit(c[i])==false)
{
num=false;
System.out.println("Input the string is not numberic.");
}
}
if(num==true&&Integer.parseInt(s)<Integer.MAX_VALUE)
{
num=true;
}
else
{
System.out.println("超出了int范围");
throw new NumberFormatException(s);
}
}
public void stringToBinary(String s2) //throws NumberFormatException
{
String s=new String();
x=Integer.parseInt(s2);
while(x!=0)
{
if(x%2==1)
{
s+="1";
x=x/2;
}
else if(x%2==0)
{
s+="0";
x=x/2;
}
}
System.out.println("The String:"+s2+"'s Binary is:");
System.out.println(new StringBuffer(s).reverse());
}
public static void main(String [] args)
{
String s1=new String();
System.out.println("Please input the number string:");
try
{
BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
s1=bfr.readLine();
/* if(s1==null)
{
System.out.println("You input the number string is null!");
throw new NullPointerException();
}*/
}
catch (Exception e )
{
e.printStackTrace();
}
StringToBinary stb=new StringToBinary();
stb.isNumberic(s1);
if(num==true)
{
stb.stringToBinary(s1);
}
}
}
- 试题三
- 试题三
- 【面试题】五道面试题三
- 面试题三
- C++ 面试题 三
- C#面试题(三)
- 三道baidu试题
- 面试题之三
- java面试题(三)
- php面试题三
- java面试题三
- java面试题三
- SCJP试题三
- Java 面试题 三
- Flex面试题(三)
- android 面试题三
- 算法面试题三
- 笔试面试题三
- 结合struts和hibernate谈J2EE架构的数据表示
- 嵌入式系统开发人员C语言测试题--填空题(数据类型和变量)
- 有效编写软件的75条建议
- 嵌入式系统开发人员C语言测试题--选择题(运算符与表达式)
- javascript进阶之变量篇
- 试题三
- 检测硬盘盒 茶水杯
- 老婆的日记(看了你会想结婚的)^0^
- 心目中的编程高手
- http1.1状态码
- 同济大学软件学院万院长谈择业
- 影响中国青年的100句人生名言
- 无题
- JBoss4.0配置MySQL数据源