有关java编程题目?

有关java编程题目?,第1张

按照题目要求源晌编写的圆,圆锥和测试类的Java程序如下

Test.java文件内容如下

class Circle{

private double r

private String color

public Circle(double r){

this.r=r

}

public double area(){

return Math.PI*r*r

}

public double perimeter(){

return Math.PI*2*r

}

public double getR(){

return this.r

}

public void setR(double r){

this.r=r

}

public String getColor(){

return this.color

}

public void setColor(String color){

this.color=color

}

public String toString(){

return "圆的半径为"+r+",颜色为"+color

}

}

class Cone{

private Circle c

private double h

private String color

public Cone(Circle c,double h){

this.c=c

this.h=h

}

public double volume(){

return 1.0/3*c.area()*h

}

public Circle getCircle(){

return this.c

}

public void setCircle(Circle c){

this.c=c

}

public double getH(){

return this.h

}

public void setH(double h){

this.h=h

}

public String getColor(){

return this.color

}

public void setColor(String color){

this.color=color

}

public String toString(){

return "圆锥的底面积为"燃悔+c.area()+",高为"+h+",颜色为"+color

}

}

public class Test{

public static void main(String[] args){

Circle circle1=new Circle(2.5)

circle1.setColor("红色")

System.out.println(circle1.toString())

System.out.println("圆的面积为"+circle1.area())

System.out.println("圆的周雹段锋长为"+circle1.perimeter())

Cone circlar1=new Cone(circle1,2.7)

circlar1.setColor("蓝色")

System.out.println(circlar1.toString())

System.out.println("圆锥的体积为"+circlar1.volume())

}

}

@SuppressWarnings("resource")

public static void main(String[] args) {

System.out.println("请选择类型\n 1.位数加减法    2.2位数加减法    3.混合加减法  【-1.退出练习】")

Scanner scanner = new Scanner(System.in)

String type = scanner.nextLine()

if (type == null || type.isEmpty())

System.out.println("未选择练习类型,程序退出")

else if (type.length() > 1) {

if (type.equals("-1"))

System.out.println("程序退出")

else

System.out.println("无此选项,程序退出")

} else {

try {

int t = Integer.valueOf(type)

if (t < -1 || t > 3)

System.out.println("无此选项,程序退出")

else {

int sum = 0 // 定义总题数

int okSum = 0 // 定义正确题数

Random random = new Random() // 随机数对象

int a = 0, b = 0, okRes, userRes// 定义运算的变量

List<String> errs = new ArrayList<String>()

while (true) {

// 运算开始

if (t == 1) { // 生成 0 -9

a = random.nextInt(10)

b = random.nextInt(10)

} else if (t == 2) { // 生成 10 -99

a = random.nextInt(90) + 10

b = random.nextInt(90) + 10

} else if (t == 3) { // 生成 0 -99

a = random.nextInt(100)

b = random.nextInt(100)

}

if ((a - b) >= 0) { // 小学生拍誉可能没学过负数,排除下

int flag = random.nextInt(2)

String errInfo = ""

String info = "请输入 " + a

if (flag == 0) {

okRes = a + b

info += " + " + b + "的答案 【-1.退出练习】:"

errInfo = a + " + " + b + "的答案: "

} else {

okRes = a - b

info += " - " + b + "的答案 【-1.退出练习】:"袭氏段

errInfo = a + " - " + b + "的答案: "

}

System.out.print(info)

String userResStr = scanner.nextLine()

if (userResStr == null || 核槐userResStr.isEmpty()) {

sum++

errs.add(errInfo + "是 " + okRes + " 你的答案是: " + userResStr)

}

userRes = Integer.valueOf(userResStr)

if (userRes == -1) {

System.out.println()

if (sum == okSum)

System.out.println("******一共 " + sum + " 道题目你全作对了.")

else {

System.out.println("******一共 " + sum + " 道题目, 对了 " + okSum + "道,错题集:")

for(int i = 0i < errs.size()i++) {

System.out.println("第 " + (i  + 1) + " 道错题 [" + errs.get(i) + "]")

}

}

break

} else {

sum++

if (userRes != okRes) {

errs.add(errInfo + "是 " + okRes + " 你的答案是: " + userResStr)

} else {

okSum++

}

}

}

}

}

} catch (Exception e) {

System.out.println("无此选项,程序退出")

}

}

}

第肆如一个:

import java.util.Scanner

/**

 * Created by Chen on 2015/11/11.

 */

public class T {

    public static void main(String[] args) {

        int temp = 0

        Scanner s = new Scanner(System.in)

        System.out.print("请输入3个整数")

        int a = s.nextInt()

        int b = s.nextInt()

        int c = s.nextInt()

        if (a > b) {

            temp = a

            a = b

            b = temp

        }

        if (a > c) {

            temp = a

            a = c

            c = temp

        }

        if (b > c) {

            temp = b

            b = c

            c = temp

        }

        System.out.println("a的值:" + a + "b的值:" + b + "c的值:" + c)

    }

}

第二题:

import java.util.Scanner

/**

 * Created by Chen on 纯册2015/11/11.

 */

public class T {

    public static void main(String[] args) {

        int temp = 0

        Scanner s = new Scanner(System.in)

        System.out.print("输入一个数")

        int a = s.nextInt()

        if((a%3==0)||(a%5==0))

        System.out.println("该整数是3或5的倍数")

        else  System.out.println("“该数不能被3或5中的任何一个数整除" )

    }

}

第三题:

import java.util.Scanner

/**

 * Created by Chen on 2015/11/11.

 */

public class T {

    public static void main(String[] args) {

        int price = 5000

        Scanner s = new Scanner(System.in)

        System.out.print("输入订票月份:")

        int month = s.nextInt()

        if (month > 0 && month < 13) {

            if (month > 3 && month < 11) {

                System.out.println("该月车票价格如下:头等舱" + price * 0.9 + "元" + "  " + "经济舱" + price * 0.8)

            } 做雹宏else System.out.println("该月车票价格如下:头等舱" + price * 0.5 + "元" + "  " + "经济舱" + price * 0.4)

        } else System.out.println("请输入正确的月份")

    }

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/12511550.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存