寒假集训篇--Java基础1.

寒假集训篇–Java基础1.


1.编写程序,使用三条输出语句分别显示"Welcome to java" 、“Welcome to computer science,”、“programming is fun”,使输出为如下形式

Welcome to java
Welcome to computer science,programming is fun

public class Demo20 {
    public static void main(String[] args) {
        System.out.print("Welcome to java,");
        System.out.print("Welcome to computer science,");
        System.out.print("programming is fun.");
    }
}

截屏2021-07-11 下午7.12.59

2.找到最大和最小的byte short int long float double。这些数据类型种哪个需要的内存最小?

System.out.println("位数");
System.out.println("byte:"+Byte.SIZE);
System.out.println("short:"+Short.SIZE);
System.out.println("int:"+Integer.SIZE);
System.out.println("long:"+Long.SIZE);
System.out.println("float:"+Float.SIZE);
System.out.println("double:"+Double.SIZE);
System.out.println(" ");
System.out.println("字节数");
System.out.println("byte:"+Byte.BYTES);
System.out.println("short:"+Short.BYTES);
System.out.println("int:"+Integer.BYTES);
System.out.println("long:"+Long.BYTES);
System.out.println("float:"+Float.BYTES);
System.out.println("double:"+Double.BYTES);
System.out.println(" ");

截屏2021-07-11 下午7.28.31

3.编写程序,输入一个姓名,输出hello ,XXX

例:

name:Zhang San
hello,Zhang San

import java.util.Scanner;

public class Scan {
    String n;
   public String  scan(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入n");
         n = scanner.nextLine();
        return n;
    }
}
public class Demo21 {

    public static void main(String[] args) {
        ScanString scanString=new ScanString();
        System.out.println("hello ," +scanString.scan());

    }
}


截屏2021-07-11 下午7.37.36

4.找出无效标识符并解释原因。

RESULT

result

12345

x12345y

black&white

answer_

while

$4

为此我写了一个字符串比较程序


package java标识符识别;

public class Judge {
    public boolean flag(String s) {
        String[] str = {"goto", "const", "public", "protected", "private", "class", "interface", "abstract", "extends", "implements", "new", "super", "this", "instanceof", "void", "byte", "short", "int", "long", "float", "double", "char", "boolean", "enum", "null", "true",
                "false", "synchronized", "volatile"};
        boolean result = false;

        if (s.charAt(0) >= 'a' && s.charAt(0) <= 'z'||s.charAt(0) >= 'A' && s.charAt(0) <= 'Z') {
            result = true;
        }
        if (s.charAt(0)=='$'||s.charAt(0)=='_') {
            result =true;
        }
            for (int i = 0; i < str.length; i++) {
                if (s.equals(str[i]))
                    result =false;

            }

        return result;
    }

 }




package java标识符识别;
import java.util.Scanner;
public class Test {
    public void scan() {
        Scanner scanner=new Scanner(System.in);
        Judge judge=new Judge();
        while (!scanner.hasNext("#close#")){
            String s=scanner.next();
            boolean flag=judge.flag(s);
            System.out.println(s+":"+ flag);
        }

    }

    public static void main(String[] args) {
        Test test=new Test();
        test.scan();
    }
}

在敲代码的时候遇到一个小问题,这里说一下
s.equals(str[i])就是这个
字符串的相等要用equals------>值的比较
直接==是地址的比较
截屏2021-07-12 下午4.25.445.学习使用数据类型。声明一些有关个人信息的变量,包括姓、名、年龄、住址等信息。

String lastname;
String firstname;
int age;
String address;

6.写出以下每步操作对应的语句

  1. 声明一个字节型变量100
  2. 转换成字符串类型
  3. 将字符串类型转换为整型
  4. 得到的整型转换为双精度浮点型
Byte a=100;
String b=a.toString();
int c= Integer.parseInt(b);
Double d= Double.valueOf(c);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值