JAVA语言程序设计流程控制语句实验报告
- 格式:pdf
- 大小:350.20 KB
- 文档页数:4
java流程控制语句实验报告下载温馨提示:该文档是我店铺精心编制而成,希望大家下载以后,能够帮助大家解决实际的问题。
文档下载后可定制随意修改,请根据实际需要进行相应的调整和使用,谢谢!并且,本店铺为大家提供各种各样类型的实用资料,如教育随笔、日记赏析、句子摘抄、古诗大全、经典美文、话题作文、工作总结、词语解析、文案摘录、其他资料等等,如想了解不同资料格式和写法,敬请关注!Download tips: This document is carefully compiled by theeditor. I hope that after you download them,they can help yousolve practical problems. The document can be customized andmodified after downloading,please adjust and use it according toactual needs, thank you!In addition, our shop provides you with various types ofpractical materials,such as educational essays, diaryappreciation,sentence excerpts,ancient poems,classic articles,topic composition,work summary,word parsing,copy excerpts,other materials and so on,want to know different data formats andwriting methods,please pay attention!一、实验目的1. 熟悉 Java 中的流程控制语句,包括顺序结构、选择结构和循环结构。
java语言程序设计实验报告
《Java语言程序设计实验报告》
实验目的:通过本次实验,学生将学会使用Java语言进行程序设计,掌握Java
语言的基本语法和编程技巧,并能够编写简单的Java程序。
实验内容:本次实验主要包括以下内容:1. Java语言的基本语法和数据类型;2. Java程序的输入输出;3. Java程序的流程控制;4. Java程序的函数和类的定义;
5. Java程序的异常处理等。
实验步骤:1. 阅读相关的Java语言教材和资料,了解Java语言的基本概念和
语法规则;2. 编写简单的Java程序,包括输入输出、流程控制、函数和类的定
义等;3. 调试和运行编写的Java程序,查看程序的运行结果;4. 对程序进行优
化和改进,提高程序的效率和性能。
实验结果:通过本次实验,学生将能够熟练掌握Java语言的基本语法和编程技巧,能够编写简单的Java程序,并能够运用Java语言解决实际的问题。
实验总结:通过本次实验,学生将对Java语言有了更深入的了解,掌握了Java
语言的基本概念和编程技巧,能够独立编写简单的Java程序。
同时,学生也将
培养了良好的编程习惯和解决问题的能力,为今后的学习和工作打下了良好的
基础。
结语:通过本次实验,学生将能够熟练掌握Java语言的基本语法和编程技巧,
为今后的学习和工作打下了良好的基础。
同时,也希望学生能够在今后的学习
和工作中不断提高自己的编程能力,为科技创新和社会发展做出更大的贡献。
本科实验报告课程名称:Java语言程序设计实验地点:致远楼专业班级:软工1507班学号:2015005682 学生姓名:李俊慧指导教师:李君婵实验结果1.2.3.System.out.println("Please input and ending with '.' :");BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));A= buf.readLine();// System.out.println(A);B=A.toCharArray();for(i=0;B[i]!='.';i++) {if(B[i]==' ') sum++;}sum+=1;System.out.println("the number of words:"+(sum));}}实验结果1.2.3.public void check() {int t2;for(t2=0;t2<100;t2++) {if(Merchandise[t2]!=null) {System.out.print("Name:"+Merchandise[t2]);System.out.print(" Inventory:"+Inventory);System.out.println();}}}public void query() {ShowMe();// System.out.print(" Inventory:"+Inventory);}}实验结果1.2.3.4.public static void main(String args[]) {Javashiyan5_3 tp1 = new Javashiyan5_3(4000,"Fast");tp1.start();Javashiyan5_3 tp2 = new Javashiyan5_3(5000,"Middle");tp2.start();Javashiyan5_3 tp3 = new Javashiyan5_3(6000,"Slow");tp3.start();}}实验结果1.actionevent myevent = new actionevent(btn);btn.addActionListener(myevent);frm.setVisible(true);}}class actionevent implements ActionListener { JButton btn;public actionevent(JButton btn) {this.btn = btn;}public void actionPerformed(ActionEvent e) {String s1="Click Me";String s2="Click Me Again";String s=((JButton)(e.getSource())).getText();// ((JButton)e.getSource()).setText(s);if(s==s1) btn.setText(s2);if(s==s2) btn.setText(s1);}}实验结果1.2.if(mark==1) {A[n]=i;n++;}}for(j=0;j<200&A[j]!=0;j++){fw.write(A[j]);}fw.close();fr = new FileReader("/home/liberty/myJavachengxu/myJavashiyan/Javashiyan7 /Javashiyan7_5/Javashiyan7_5_sushu.txt");int value;while((value=fr.read())!=-1) {System.out.print(value+"\t");q++;if(q%5==0)System.out.println();}fr.close();System.out.println();System.out.println("However,the 'txt' file cannot be viewed by VIM!!!");}}实验结果1.2.3.4. 5.。
java语言程序设计实验报告篇一:Java实验八报告《Java语言程序设计》实验报告学生姓名:郭倩班级:信息112学号:201152275208实验地点:经管实验中心证券实验室指导教师:赵利平实验日期:实验环境:Windows 2000+JDK1.61. 实验目的(1)理解子类派生的概念(2)学习创建子类对象(3)掌握方法覆盖的使用2.实验内容(1)设计三个类:完成实验教材P52页程序清单4-2;(2)通过编制一个程序来加深对嵌套类、内部类和局部类的理解:完成实验教材P56页程序清单4-3;2. 实验过程报告撰写具体要求:上述实验内容要求写出源代码及运行结果。
实验内容(1)://Polymorphism.abstract class Student{final static int CourseNo=3;String name;String type;int[] courses;String courseGrade;public Student(String name){=name;courses=new int[CourseNo];courseGrade=null;}public abstract void calculateGrade();public String getName(){return name;}public String getType(){return type;}public String getCourseGrade(){return courseGrade;}public int getCourseScore(int courseNumber){return courses[courseNumber];}public void setName(String name){=name;}public void setType(String type){this.type=type;}public void setCourseScore(int courseNumber,int courseScore){this.courses[courseNumber]=courseScore;}}class Undergraduate extends Student{public Undergraduate(String name){super(name);type="本科生";}public void calculateGrade(){int total=0;double average=0;for(int i=0;i<CourseNo;i++){total+=courses[i];};average=total/CourseNo;if(average>=80&&average<100) coursegrade="优秀" else="" average="">=70&&average<80) coursegrade="良好" else="" average="">=60&&average<70) coursegrade="一般" else="" average="">=50&&average<60) courseGrade="及格";else courseGrade="不及格";}}class Postgraduate extends Student{public Postgraduate(String name){super(name);type="研究生";}public void calculateGrade(){int total=0;double average=0;for(int i=0;i<CourseNo;i++){total+=courses[i];};average=total/CourseNo;if(average>=90&&average<100) coursegrade="优秀" else="" average="">=80&&average<90) coursegrade="良好"else="" average="">=70&&average<80) coursegrade="一般" else="" average="">=60&&average<70) courseGrade="及格";else courseGrade="不及格";}}public class Polymorphism{public static void main(String[] args){Student[] students=new Student[5];students[0]=new Undergraduate("陈建平");students[1]=new Undergraduate("鲁向东");students[2]=new Postgraduate("匡晓华");students[3]=new Undergraduate("周丽娜");students[4]=new Postgraduate("梁欣欣");for(int i=0;i<5;i++){students[i].setCourseScore(0,87);students[i].setCourseScore(1,90);students[i].setCourseScore(2,78);}for(int i=0;i<5;i++){students[i].calculateGrade();}System.out.println("姓名"+" 类型"+" 成绩");}}System.out.println("-------------------------"); for(int i=0;i<5;i++){ System.out.println(students[i].getName()+" "+students[i].getType()+" "+students[i].getCourseGrade()); }(1) 都有(2) Student[] students=new Student[5];(3) Student是抽象的,不能用new操作符创建抽象类的实例。
java流程控制实验总结英文回答:Java Flow Control Experiment Summary.Objective:The objective of this experiment was to explore the different flow control statements available in the Java programming language. Flow control statements allow programmers to control the execution flow of their programs, enabling them to make decisions and perform differentactions based on specific conditions.Materials:Java Development Kit (JDK)。
Java Integrated Development Environment (IDE)。
Methods:The following flow control statements were investigated:if-else: Conditional statement that executes a blockof code only if a given condition is true.switch-case: Conditional statement that executes a different block of code based on the value of a given variable.while: Loop statement that executes a block of code repeatedly as long as a given condition is true.do-while: Loop statement that executes a block of code at least once, even if the condition is false.for: Loop statement that executes a block of code a specified number of times.break: Statement that exits a loop or switch statement prematurely.continue: Statement that skips the remaining code in a loop iteration and proceeds to the next iteration.Results:Each flow control statement was implemented in a Java program and tested to verify its functionality. The results confirmed that Java provides a comprehensive set of flow control statements that enable programmers to effectively control the execution flow of their programs.Conclusion:This experiment provided valuable insights into the different flow control statements available in the Java programming language. By understanding and utilizing these statements, programmers can achieve greater control over the behavior of their programs and develop more efficient and reliable applications.中文回答:Java 流程控制实验总结。
一、实验目的1.理解定义变量的作用,掌握定义变量的方法。
2.掌握顺序结构程序控制。
3.掌握算法与程序设计的基本方法。
4.掌握if语句、if-else-if结构的使用。
5.掌握switch语句的使用。
6.掌握使用while语句实现循环。
7.了解使用do-while语句实现循环。
8.掌握使用for语句实现循环。
9.了解continue语句和break语句的使用。
10.掌握一维数组的概念、定义和使用。
二、实验内容及步骤1.顺序结构(1)任务写一个程序,读入三个整数并打印他们的平均值。
源代码:public class renwu1 {public static void main(String[] args) {int sum = 0;int aver = 0;Scanner scanner = new Scanner(System.in);for(int i = 1;i <= 3; i++) {System.out.print("请输入第" + i +"个数:");sum +=scanner.nextInt();aver = sum / i;}System.out.println("平均数是:" + aver);}}运行结果:写一个程序,读入两个浮点数并打印他们的和、差和积。
源代码:public static void main(String[] args) {float sum = 0;float cha = 0;float ji = 0;Scanner scanner = new Scanner(System.in);System.out.println("请输入第一个浮点数:");float num1 = scanner.nextFloat();System.out.println("请输入第二个浮点数:");float num2 = scanner.nextFloat();sum = num1 + num2;cha = num1 - num2;ji = num1 * num2;System.out.println("和是:" + sum);System.out.println("差是:" + cha);System.out.println("积是:" + ji);}}运行结果:2.分支结构(1)任务任务1:使用简单if语句编程,从键盘键入三个整数,然后按照从小到大的顺序将其输出。
java流程控制语句实验总结在Java编程语言中,流程控制语句是用来控制程序执行流程的重要工具。
通过合理运用流程控制语句,我们可以实现条件判断、循环迭代和跳转等功能,从而使程序能够按照我们预期的方式执行。
在本文中,我将对Java流程控制语句进行实验总结,并分享一些实践经验。
1. 条件语句条件语句是根据给定的条件选择性地执行不同的代码块。
在Java中,常用的条件语句有if语句、if-else语句和switch语句。
1.1 if语句if语句用于根据一个条件的真假来执行相应的代码块。
下面是一个简单的示例:```javaint num = 10;if (num > 0) {System.out.println("num是正数");}```1.2 if-else语句if-else语句用于在条件为真和条件为假时执行不同的代码块。
下面是一个示例:```javaint num = 10;if (num > 0) {System.out.println("num是正数");} else {System.out.println("num是负数");}```1.3 switch语句switch语句根据给定的表达式的值执行相应的代码块。
下面是一个示例:```javaint day = 1;String dayOfWeek;switch (day) {case 1:dayOfWeek = "Monday";break;case 2:dayOfWeek = "Tuesday";break;// 其他case语句...default:dayOfWeek = "Invalid day";}System.out.println("Today is " + dayOfWeek);```2. 循环语句循环语句是用于重复执行某段代码块的工具。
java流程控制及数组实验报告Java flow control and array are important topics in programming. Java流程控制和数组是编程中重要的主题。
Flow control refers to the order in which the statements in a program are executed. 流程控制指的是程序中语句执行的顺序。
In Java, flow control can be achieved using if-else statements, switch-case statements, loops, and jump statements. 在Java中,可以使用if-else语句、switch-case语句、循环和跳转语句来实现流程控制。
These constructs allow developers to control the execution flow of a program based on certain conditions. 这些结构允许开发人员根据特定条件控制程序的执行流程。
Arrays are used to store multiple values in a single variable. 数组用于在一个变量中存储多个值。
In Java, arrays are a way to store collections of similar type of items. 在Java中,数组是一种存储相似类型项的集合的方式。
Arrays can hold primitives, objects, or even other arrays. 数组可以保存基本类型、对象,甚至其他数组。
Understanding how to work with arrays is essential for efficient programming in Java. 理解如何使用数组对于在Java中进行高效的编程至关重要。
java实验报告Java 实验报告一、实验目的本次 Java 实验的主要目的是通过实际编程操作,深入理解和掌握Java 语言的基本语法、面向对象编程的概念和方法,以及常用类库的使用。
同时,培养我们的问题解决能力、逻辑思维能力和代码规范意识,为今后的软件开发工作打下坚实的基础。
二、实验环境1、操作系统:Windows 102、开发工具:Eclipse IDE for Java Developers3、 JDK 版本:JDK 18三、实验内容本次实验共包括以下三个部分:1、 Java 基本语法练习变量与数据类型运算符与表达式控制流语句(ifelse、for、while、dowhile)数组2、面向对象编程实践类与对象的定义和使用构造函数与方法重载封装、继承与多态抽象类与接口3、 Java 常用类库应用String 类与字符串操作集合框架(ArrayList、HashMap)文件输入输出(File、FileReader、FileWriter)四、实验步骤及代码实现1、 Java 基本语法练习变量与数据类型:```javapublic class VariableDataType {public static void main(String args) {int age = 20;double salary = 50005;String name ="张三";boolean isStudent = true;Systemoutprintln("年龄:"+ age);Systemoutprintln("工资:"+ salary);Systemoutprintln("姓名:"+ name);Systemoutprintln("是否是学生:"+ isStudent);}}```运算符与表达式:```javapublic class OperatorExpression {public static void main(String args) {int num1 = 10;int num2 = 5;int sum = num1 + num2;int difference = num1 num2;int product = num1 num2;int quotient = num1 / num2;int remainder = num1 % num2; Systemoutprintln("和:"+ sum);Systemoutprintln("差:"+ difference);Systemoutprintln("积:"+ product);Systemoutprintln("商:"+ quotient);Systemoutprintln("余数:"+ remainder);}}```控制流语句:```javapublic class ControlFlowStatement {public static void main(String args) {// ifelse 语句int score = 80;if (score >= 90) {Systemoutprintln("优秀");} else if (score >= 80) {Systemoutprintln("良好");} else if (score >= 70) {Systemoutprintln("中等");} else if (score >= 60) {Systemoutprintln("及格");} else {Systemoutprintln("不及格");}// for 循环for (int i = 1; i <= 5; i++){Systemoutprintln("第" + i +"次循环");}// while 循环int j = 1;while (j <= 5) {Systemoutprintln("第" + j +"次 while 循环");j++;}// dowhile 循环int k = 1;do {Systemoutprintln("第" + k +"次 dowhile 循环");k++;} while (k <= 5);}}```数组:```javapublic class ArrayExample {public static void main(String args) {//一维数组int numbers ={1, 2, 3, 4, 5};for (int num : numbers) {Systemoutprintln(num);}//二维数组int matrix ={{1, 2, 3},{4, 5, 6},{7, 8, 9}};for (int row : matrix) {for (int num : row) {Systemoutprint(num +"");}Systemoutprintln();}}}```2、面向对象编程实践类与对象的定义和使用:```javapublic class Person {private String name;private int age;public Person(String name, int age) {thisname = name;thisage = age;}public void showInfo(){Systemoutprintln("姓名:"+ name +",年龄:"+ age);}public static void main(String args) {Person person1 = new Person("张三", 20);person1showInfo();}}```构造函数与方法重载:```javapublic class ConstructorOverloading {private String name;private int age;public ConstructorOverloading(String name) {thisname = name;}public ConstructorOverloading(String name, int age) {thisname = name;thisage = age;}public void showInfo(){if (age == 0) {Systemoutprintln("姓名:"+ name);} else {Systemoutprintln("姓名:"+ name +",年龄:"+ age);}}public static void main(String args) {ConstructorOverloading person1 = new ConstructorOverloading("张三");person1showInfo();ConstructorOverloading person2 = new ConstructorOverloading("李四", 25);person2showInfo();}}```封装、继承与多态:```java//父类class Animal {private String name;public Animal(String name) {thisname = name;}public void eat(){Systemoutprintln(name +"正在吃东西");}}//子类继承父类class Dog extends Animal {public Dog(String name) {super(name);}public void bark(){Systemoutprintln(name +"在叫");}}public class InheritancePolymorphism {public static void main(String args) {Animal animal = new Dog("小黑");animaleat();//向下转型为 Dog 类型调用 bark 方法if (animal instanceof Dog) {Dog dog =(Dog) animal;dogbark();}}}```抽象类与接口:```java//抽象类abstract class Shape {abstract void draw();}//实现抽象类的子类class Circle extends Shape {@Overridevoid draw(){Systemoutprintln("画一个圆");}}//接口interface Moveable {void move();}//实现接口的类class Car implements Moveable {@Overridepublic void move(){Systemoutprintln("汽车在移动");}}public class AbstractInterfaceExample {public static void main(String args) {Shape shape = new Circle();shapedraw();Moveable car = new Car();carmove();}}```3、 Java 常用类库应用String 类与字符串操作:```javapublic class StringOperation {public static void main(String args) {String str1 ="Hello, ";String str2 ="World!";String str3 = str1 + str2;Systemoutprintln(str3);int length = str3length();Systemoutprintln("字符串长度:"+ length);char charAt = str3charAt(5);Systemoutprintln("第 5 个字符:"+ charAt);boolean contains = str3contains("World");Systemoutprintln("是否包含 World:"+ contains);}}```集合框架(ArrayList、HashMap):```javaimport javautilArrayList;import javautilHashMap;import javautilMap;public class CollectionFramework {public static void main(String args) {// ArrayListArrayList<String> names = new ArrayList<>();namesadd("张三");namesadd("李四");namesadd("王五");for (String name : names) {Systemoutprintln(name);}// HashMapHashMap<String, Integer> ages = new HashMap<>();agesput("张三", 20);agesput("李四", 25);agesput("王五", 30);for (MapEntry<String, Integer> entry : agesentrySet()){Systemoutprintln(entrygetKey()+"的年龄是" +entrygetValue());}}}```文件输入输出(File、FileReader、FileWriter):```javaimport javaioFile;import javaioFileReader;import javaioFileWriter;import javaioIOException;public class FileIOExample {public static void main(String args) {//写入文件try (FileWriter writer = new FileWriter("outputtxt")){writerwrite("这是写入文件的内容");} catch (IOException e) {eprintStackTrace();}//读取文件try (FileReader reader = new FileReader("outputtxt")){int character;while ((character = readerread())!=-1) {Systemoutprint((char) character);}} catch (IOException e) {eprintStackTrace();}}}```五、实验结果与分析1、 Java 基本语法练习变量与数据类型:能够正确定义和使用各种数据类型的变量,并进行基本的运算和输出。