《Java》JAVA计算任务,
分享于 点击 24419 次 点评:275
《Java》JAVA计算任务,
JAVA计算任务
JAVA编译过程:
源程序代码 > 编译器(JDK) > 字节码 > JVM(Java虚拟机)(JRE) > 机器码 > 计算机
计算任务A:
目标:编程实现1+2+3+…+100
- 源代码:
package calc;
public class calc {
public static void main(String[] args) {
// TODO 自动生成的方法存根
int a = 0; //声明int型变量a并赋值
for(byte i=1;i<101;i++) //循环100次,实现i从1至100
{
a += i; //实现从1加到100
}
System.out.println("结果为:" +a); //将以上相加的结果输出
}
}
- 运行结果:
计算任务B:
目标:编程实现1+3+5+7+9+…+101
- 源代码:
package calc;
public class calc {
public static void main(String[] args) {
// TODO 自动生成的方法存根
int a = 0; //声明int型变量a并赋值
for(int i=1;i<102;i=i+2) //循环101次,i每次+2,实现i从1至101
{
a += i; //实现1~101中的奇数相加
}
System.out.println("结果为:" +a); //将以上相加的结果输出
}
}
- 运行结果:
计算任务C:
目标:编程实现输出10个随机整数,值在0到100之间
- 源代码:
package calc;
public class calc {
public static void main(String[] args) {
// TODO 自动生成的方法存根
int a[] =new int[11]; //声明int型数组a并使用new关键字进行内存分配
for(int i=1;i<11;i++) //循环10次,为数组a赋值
{
a[i] = (int)(Math.random()*100); /*用Math类的Random方法只能产生0~1之间的随机数,所以用(0~1)*100,就能产生0~100的随机数*/
}
for(byte j=1;j<11;j++) //利用循环依次输出储存在数组a中的十个数
{
System.out.println(a[j]+"");
}
}
}
- 运行结果:
计算任务D:
目标:对C步的10个随机整数并行排序
- 源代码:
package calc;
public class Calc {
public static void main(String[] args) {
int a[] = new int[11]; //定义数组a
for(int i=1;i<11;i++) //生成随机整数存入数组a
{
a[i] = (int)(Math.random()*100);
}
for(int j=1;j<a.length-1;j++) //冒泡排序
{
for(int k=1;k<a.length-j-1;k++)
{
if(a[k]>a[k+1]) //比较相邻元素,最大的元素放在最后
{
int str=a[k];
a[k]=a[k+1];
a[k+1]=str;
}
}
}
for(int l=1;l<11;l++) //将排序后的10个数从小到大输出
{
System.out.println(a[l]);
}
}
}
- 运行结果:
计算任务E:
目标:设有两字串构成的集合,字符串内容值为
A:{a,b,c,d,e,f,g,in,off,about,get}
B:{f,g,a,come,get,go}
求出:(1)AB集合的交集
(2)只在A集中出现的字串集合
(3)AB集合的并集
- 目标(1)源代码:
package calc;
public class Calc {
public static void main(String[] args) {
String A[] = {"a","b","c","d","e","f","g","in","off","about","get"}; //定义数组A
String B[] = {"f","g","a","come","get","go"}; //定义数组B
System.out.print("AB集合的交集为:");
for(int i=0;i<A.length;i++){
for(int j=0;j<B.length;j++){
if(A[i]==B[j]){ //判断字符串是否相同
System.out.print(A[i]+" "); //输出相同字符串
}
}
}
System.out.print("\n");
}
}
-
目标(1)运行结果:
-
目标(2)源代码:
package calc;
import java.util.ArrayList; //导入集合类List类
public class Calc {
public static void main(String[] args) {
String A[] = {"a","b","c","d","e","f","g","in","off","about","get"}; //定义数组A
String B[] = {"f","g","a","come","get","go"}; //定义数组B
ArrayList str1 = new ArrayList();
ArrayList str2 = new ArrayList();
for(int i=0;i<A.length;i++) { //将字符串数组A的元素送入动态数组str1中
str1.add(A[i]);
}
for(int j=0;j<B.length;j++) { //将字符串数组B的元素送入动态数组str2中
str2.add(B[j]);
}
str1.removeAll(str2); //移除所有A字符串数组中B字符串数组所含元素
System.out.print("只在A集中出现的字串集合为:");
System.out.print(str1); //输出A中移除B后的元素
System.out.print("\n");
}
}
-
目标(2)运行结果:
-
目标(3)源代码:
package calc;
import java.util.HashSet; //导入HashSet类
import java.util.Set;
public class Calc {
public static String[] union(String str1[], String str2[]){ // 利用Set的唯一性
Set<String> set = new HashSet<String>();
for (String str : str1) {
set.add(str);
}
for (String str : str2) {
set.add(str);
}
String result[] = {};
return set.toArray(result);
}
public static void main(String[] args) {
String A[] = {"a","b","c","d","e","f","g","in","off","about","get"}; //定义数组A
String B[] = {"f","g","a","come","get","go"}; //定义数组B
System.out.print("AB集合的并集为:");
String union[] = union(A,B);
for(int j=0;j<union.length;j++) {//循环输出字符串
if(union[j] != null) {
System.out.print(union[j]+" ");
}
}
System.out.print("\n");
}
}
- 目标(3)运行结果:
相关文章
- 暂无相关文章
用户点评