寒假集训篇–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.");
}
}
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(" ");
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());
}
}
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------>值的比较
直接==是地址的比较
5.学习使用数据类型。声明一些有关个人信息的变量,包括姓、名、年龄、住址等信息。
String lastname;
String firstname;
int age;
String address;
6.写出以下每步操作对应的语句
- 声明一个字节型变量100
- 转换成字符串类型
- 将字符串类型转换为整型
- 得到的整型转换为双精度浮点型
Byte a=100;
String b=a.toString();
int c= Integer.parseInt(b);
Double d= Double.valueOf(c);