Java实验(1)程序控制语句 您所在的位置:网站首页 java流程控制语句实验报告 Java实验(1)程序控制语句

Java实验(1)程序控制语句

2024-07-09 20:58| 来源: 网络整理| 查看: 265

1、输入华氏温度f,计算并输出相应的摄氏温度c。c=5*(f-50)/9+10.

import java.util.Scanner; public class Main { public static void main(String[] args) { //write your own codes Scanner in = new Scanner(System.in); Double n = in.nextDouble(); System.out.printf("The temprature is "); System.out.println(String.format("%.2f",5*(n-50)/9+10)); } }

2、输入一个正整数n,计算1-1/4+1/7-1/10+1/13-1/16+…前n项之和。

import java.util.Scanner; public class Main { public static void main(String[] args) { int n; Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); //write your code double sum = 0; int t = 1; for (int i = 1; i public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int m, n; m = scanner.nextInt(); n = scanner.nextInt(); //write your code int i, temp = 1; for (i = m; i System.out.printf("%d ", i); if (temp % 4 == 0) System.out.println(""); temp++; } } }

4、输入1个四位数,将其加密后输出。方法是将该数每一位上的数字加9,然后除以10取余,做为该位上的新数字,最后将第1位和第3位上的数字互换,第2位和第4位上的数字互换,组成加密后的新数。

import java.util.*; public class Main { public static void main(String[] args) { int number, digit1, digit2, digit3, digit4, newnum; int temp; Scanner scanner = new Scanner(System.in); number = scanner.nextInt(); //WRITE CODE HERE digit1=((number/10)%10+9)%10; digit2=(number%10+9)%10; digit3=((number/1000)%10+9)%10; digit4=((number/100)%10+9)%10; newnum=digit1*1000+digit2*100+digit3*10+digit4*1; System.out.println("The encrypted number is " + newnum); } }

5、 找出4*4的矩阵中的最大值,并输出其所在的行列值 。

public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i, j; int[][] a = new int[4][4]; for (i = 0; i a[i][j]=sc.nextInt(); } } int max = a[0][0],max_i=0,max_j=0; for (i = 0; i if(a[i][j]>max){ max=a[i][j]; max_i=i; max_j=j; } } } System.out.println("最大值为"+max+",位于第"+max_i+"行 第"+max_j+"列"); } }

6、求a+aa+aaa+aa…a。输入一个正整数repeat (0 Scanner in = new Scanner(System.in); int repeat=in.nextInt(); while(repeat!=0){ int a=in.nextInt(); int n=in.nextInt(); int i,sum=0; int t=a; int [] b=new int[n]; for(i=0;i public static void main(String[] args) { Scanner sc = new Scanner(System.in); int repeat, n; repeat = sc.nextInt(); do { n = sc.nextInt(); int i,j,temp; int[] array = new int[n]; for (i = 0; i for(j=0;j temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } }//冒泡排序 for(i=0;i



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有