韩顺平JAVA-D8

public class StringToBasicDetail{
    public static void main(String[] args){
 
        String str = "123";
        //转成int
        int n1 = Interger.parseInt(str);
        System.out.println(n1);
    }
}

058 String转基本数据类型

1.转换前要确保String可以转换成数据(123(ok),hello(false))

2.格式不正确,会抛出异常,程序会种植,异常处理

public class StringToBasic{
	
	//编写一个main方法
	public static void main(String[] args){

		//基本数据类型->String
		
		int n1 = 100;
		float f1 = 1.1F;
		double d1 = 4.5;
		boolean b1 = true;
		String s1 = n1 + "";
		String s2 = f1 + "";
		String s3 = d1 + "";
		String s4 = b1 + "";
		System.out.println(s1 + " " + s2 + " " + s3 + " " + s4);
		
		//String->对于得基本数据类型
		String s5 = "123";
		//会在OOP讲对象和方法得时候回来详细讲
		//解读 使用 基本数据类型对应得包装类的,相应方法,得到基本数据类型
		int num1 = Integer.parseInt(s5);
		double num2 = Double.parseDouble(s5);
		float num3 = Float.parseFloat(s5);
		long num4 = Long.parseLong(s5);
		byte num5 = Byte.parseByte(s5);
		boolean b = Boolean.parseBoolean("true");
		short num6 = Short.parseShort(s5);

		System.out.println("=======================");//
		System.out.println(num1);//123
		System.out.println(num2);//123.0
		System.out.println(num3);//123.0
		System.out.println(num4);//123
		System.out.println(num5);//123
		System.out.println(num6);//123
		System.out.println(b);//true
		
		//把字符串转成字符char ->含义是指 取出字符串的第一个字符
		//解读 s5.charAt(0) 得到 s5字符串的第一个字符'1'
		System.out.println(s5.charAt(0));

	}
}

059-060 Java作业

1.代码阅读题

public class Homework01{
    public static void main(String[] args){
        int n1;
        n1 = 13;
        int n2;
        n2 = 17;
        int n3;
        n3 = n1 + n2;
        System.out,println("n3 = " + n3);//n3=30
        int n4 = 38;
        int n5 = n4 -n3;
        System.out.println("n5 = " + n5); //n5=8
 }
}

就这?太简单了!老年痴呆检测题?

2.使用char类型,分别保存\n \t \r \\ 1 2 3等字符,再打印输出

public class Homework02{
    public static void main(String[] args){
        char c1 = '\n';//换行
        char c2 = '\t';//制表位
        char c3 = '\r';//回车
        char c4 = '\\';//输出
        char c5 = '1';
        char c6 = '2';
        char c7 = '3';     
       System.out.println(c1);
       System.out.println(c2);
       System.out.println(c3);
       System.out.println(c4);
       System.out.println(c5);
       System.out.println(c6);
       System.out.println(c7);
 }
}

3.保存两本书名,用+拼接看效果;性别,拼接;价格,拼接

public class Homework03{
    public static void main(String[] args){
        String book1 = "红楼梦";
        String book2 = "水浒传";       
        char c1 = '女';      
        char c2 = '男';c1 + c2 = 字符码相加得到一个整数
        String s2 = c2 + " ";     
        double price1 = 123.45;  
        double price2 = 100.11;   
        System.out.println(book1 + book2);
        //System.out.println(c1 + " " + c2);
        System.out.println(c1 + c2);
        System.out.println(price1 + price2);
    }
}

4.编程实现如下效果

public class Homework04{
	public static void main(String args[]){
		String name = "mumu";
        int age = 23;
        double score = 100.00;
        char gender = '女';
        String hobby = "写冒泡排序";
        System.out.println("姓名\t年龄\t成绩\t性别\t爱好\n"
            + name + "\t"+ age + "\t"+ score + "\t"+ gender + "\t" + hobby);
    
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值