首页 > 代码库 > JAVA练习

JAVA练习

1.给java,stb,sql三门科目的成绩算出JAVA和SQL的成绩差,JAVA和SQL的成绩共是,三门课的总分是,三门课的平均分是多少

import java.util.Scanner;
public class grade {
public static void main(String []args) {
System.out.println("请输入STB的成绩");
Scanner input=new Scanner(System.in);
int stbGreade=input.nextInt();

System.out.println("请输入JAVA的成绩");
int javaGreade=input.nextInt();

System.out.println("请输入SQL的成绩");
int sqlGreade=input.nextInt();

int numA=javaGreade-sqlGreade;
int numB =javaGreade+sqlGreade;
int numC =stbGreade+javaGreade+sqlGreade;
int numD =(stbGreade+javaGreade+sqlGreade)/3;

System.out.println("STB\tJAVA\tSQL\n"+stbGreade+"\t"+javaGreade+"\t"+sqlGreade+"");

System.out.println("JAVA和SQL的成绩差是:"+numA+"");
System.out.println("JAVA和SQL的成绩共是:"+numB+"");
System.out.println("三门课的总分是:"+numC+"");
System.out.println("三门课的平均分是:"+numD+"");
}
}

2,输入两件商品名称,数量,价格和金额,按格式输出

import java.util.Scanner;
public class cargo {
public static void main(String []args) {

Scanner input= new Scanner(System.in);
System.out.println("请输入第一件商品名称");
String unmA = input.nextLine();
System.out.println("请输入第一件商品数量");
int unmB = input.nextInt();
System.out.println("请输入第一件商品单价");
int unmC = input.nextInt();


System.out.println("请输入第二件商品名称");
String unma = input.nextLine();
System.out.println("请输入第二件商品数量");
int unmb = input.nextInt();
System.out.println("请输入第二件商品单价");
int unmc = input.nextInt();

System.out.println("\t购物清单");
System.out.println("商品名称\t购买数量\t商品单价\t金额");
System.out.println(unmA+‘\t‘+unmB+‘\t‘+unmC+‘\t‘+(unmB*unmC));
System.out.println(unma+‘\t‘+unmb+‘\t‘+unmc+‘\t‘+(unmb*unmc));

}
}

3.输入两个整数,交互两个数,使用两种方法

方法一:

import java.util.Scanner;
public class Replace {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入a的值");
int a = sc.nextInt();
System.out.println("请输入b的值");
int b = sc.nextInt();
System.out.println("a的值为"+a+" "+"b的值为"+b);
int x =a ;
int y =b ;

System.out.println("x的换之前的值为"+x+" "+"y换之前的值为"+y);
x = x - y;
y = x + y;
x = y - x;

System.out.println("x的换之后的值为" + x + " " + "y换之后的值为" + y);
}
}

方法二:

import java.util.Scanner;

public class Replace2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入a的值");
int a = sc.nextInt();
System.out.println("请输入b的值");
int b = sc.nextInt();
int c;
c=a;
a=b;
b=c;
System.out.println("交换后a="+a+",交换后b="+b);
}

}

4.根据天数(46)计算周数和剩余天数

public class fate {
public static void main(String []args) {
int zhouNum=46/7;
int yuNum=46%7;
System.out.println("周数:"+zhouNum+"余下的天数:"+yuNum);
}
}

 

5,已知圆的半径r=1.5,求周长和面积分别是

import java.util.Scanner;
public class circle {
public static void main(String[] args) {
System.out.println("请输入圆半径:");
Scanner input = new Scanner(System.in);
float r = input.nextFloat();
float perimeter = getPerimeter(r);
float area = getArea(r)

System.out.println("圆的周长为:" + perimeter);
System.out.println("圆的面积为:" + area);
}

static float getArea(float r) {
return (float) (3.14 * r * r);
}

static float getPerimeter(float r) {
return (float) (3.14 * r * 2);
}
}

 

6.某班一次java考试平均分81.29,第二次比第一次多2分,计算第二次的平均分

 

7.实现一个数字加密,规则:加密结果=(整数*10+5)/2+3.14159,加密结果为整数

 

JAVA练习