实验目的:
已知学生有学号,姓名,性别,年龄,身高,体重六项基本信息,依次从键盘录入该学生信息,完成后输出该生详细信息。
实验代码:
public class daiMa1 {
public static void main(String[] args) {
System.out.println("请输入学生的姓名");
String name = Console.readLine();
System.out.println("请输入学生的学号");
int stuNo = Console.readInt();
System.out.println("请输入学生的性别");
String sex = Console.readLine();
System.out.println("请输入学生的年龄");
int age = Console.readInt();
System.out.println("请输入学生的身高");
String high = Console.readLine();
System.out.println("请输入学生的体重");
String weight =Console.readLine();
System.out.println("\n");
System.out.println("学号 姓名 性别 年龄 身高 体重 ");
System.out.println(stuNo+" "+name+" "+sex+" "+age+" "+high+" "+weight);
}
}
实验结果:
请输入学生的姓名
张三
请输入学生的学号
1415
请输入学生的性别
男
请输入学生的年龄
21
请输入学生的身高
180cm
请输入学生的体重
75kg
学号 姓名 性别 年龄 身高 体重
1415 张三 男 21 180cm 75kg
实验心得:
技术可老师说Java语言没有像C++那样的输入流语句cin>>,于是老师给了一段代码Console,要求放在Project的src中。才可以实现从屏幕将信息的输入
Console:
import java.util.Scanner;
/*
* @Copyright:2011 Shandong Strong software co,ltd.
* All right reserved.
* @author:zy
* @date:2012-9-14
* @version:1.0
*/
/*
*comment:一句话概括类完成的功能
*description:详细描述类的功能
*/
public class Console
{
public static String readLine(){
Scanner sc=new Scanner(System.in);
return sc.next();
}
public static int readInt(){
Scanner sc=new Scanner(System.in);
String str=sc.next();
return Integer.parseInt(str);
}
public static double readDouble(){
Scanner sc=new Scanner(System.in);
String str=sc.next();
return Double.parseDouble(str);
}
}
有了这段代码,就可以就不同类型的数据进行输入了,Console?感觉和C#的输入有些许相似的地方,看来各门语言之间真的是有剪不断理还乱的复杂关系的。当然之后的代码就显得很简单了,只要注意最后输出学生数据时的格式就可以了,呵呵,果然,代码精华的甜头还是让老师抢去了,不太爽啊!