导航菜单
首页 >  » 正文

JAVA题库的题 求答案啊!!!!!!!!!!!!!!!! 请教几题JAVA 试题

JAVA题库的题 求答案啊!!!!!!!!!!!!!!!!

1.有点没太明白意思,是要求2个数相加的和吗?
import java.util.Scanner;
public class Application1 {
/**
* @param args
*/
public static void main(String[] args) {
byte b = 0;
byte a = 0;
Scanner scan = new Scanner(System.in);
System.out.println("请输入第一个值:");
a = scan.nextByte();
System.out.println("你输入的值:" + b) ;
System.out.println("请输入第2个值:");
b = (byte) (scan.nextByte() + a);
System.out.println("b的值:" + b) ;
}
}
2,是这样吗?
import java.applet.Applet;
import java.awt.Graphics;
public class App2 extends Applet{
@Override
public void paint(Graphics g) {
boolean b = 12+5 > 3 || 12 - 5 < 7;
g.drawString("12+5 > 3 || 12 - 5 < 7的结果为:" + b, 100, 100);
}
@Override
public void init() {
// TODO Auto-generated method stub
super.init();
setSize(500, 399);
}
}
3.
import java.util.Scanner;
public class App3 {
/**
* @param args
*/
public static void main(String[] args) {
int a,b,c = 0;
System.out.println("请输入三角形的3条变的长(以逗号分隔每个边的长度):");
Scanner s = new Scanner(System.in);
String sNum = s.nextLine();
if (sNum == null || sNum.length() == 0) {
System.out.println("没有输入长度,退出。");
return;
}
String[] abc = sNum.split(",");
if (abc.length != 3) {
System.out.println("必须输入三角形的3条变的长(以逗号分隔每个边的长度),退出。。");
return;
}
a = Integer.valueOf(abc[0]);
b = Integer.valueOf(abc[1]);
c = Integer.valueOf(abc[2]);
int zc = a + b + c;// 周长
int p = (a+b+c) / 2;
double S = Math.pow(p*(p-a)*(p-b)*(p-c),0.5); // 海伦公式计算的面积。
System.out.println("周长:" + zc + ",面积:" + S);
}
}
望采纳。

请教几题JAVA 试题

1Java编译器使用文件系统来保存程序包。 ( 对 )
2Java源程序文件中是不区分字母的大小写的。 ( 错 )
3、Java applet不能够存取客户机磁盘上的文件。 ( 错 )
4、可以用new来创建一个类的实例,即”对象”。 ( 对 )
5、Java程序对计算机硬件平台的依赖性很低。 (对 )
6、Java可以用来进行多媒体及网络编程。 ( 对 )
7、用final修饰的类必须被继承。 ( 错 )
8、类是对象实例。 ( 错 )
9、每个类都只能有一个构造方法。 ( 错 )
10、applet可以运行在浏览器中。 ( 对 )
填空题
1. 【1】 是Java程序中基本的结构单位。
2.Java语言中,移位运算符包括:>>、<>>】 ; 3.构件不能独立地显示出来,必须将构件放在一定的 【3】 中才能显示。 4.能将显示空间分成很多层的布局管理器是 【4】 。 5.Applet是能够嵌入到 【exe】 格式的文件中,并能够在浏览器中运行的Java。 6.使用Swing编写Applet,则该Applet的主类应该定义为 【6】 类的子类。 7.在Java中,线程的模型就是一个CPU、程序代码和 【7】 的封装体。 8.键盘键入字符串并在计算机屏幕上显示,这时的数据源是 【键盘】 。 9.任何一个Java程序都默认引入了一个包,这个包的名字叫java. 【unitl】 。 10.Java语言中,有一个类是所有类或接口的父类,这个类的名称是 【Object】 因只是有限,只给你写了我知道的!不好意思 希望对你有帮助!

java题map集合

package test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class Test {
   public static void main(String[] args) throws Exception {
   //创建一个map集合
   Mapmap = new HashMap(); //香蕉=5.6; 樱桃=25; 桃子=5.6 苹果=2.3 map.put("香蕉", "5.6"); map.put("樱桃", "25"); map.put("桃子", "5.6"); map.put("苹果", "2.3"); System.out.println("map.size = " + map.size()); //遍历该map并且统计出有多少种水果Key用两种方法遍历 System.out.println("方法一:begin"); //遍历map中的键 for (String key : map.keySet()) { System.out.println("key = " + key); } //遍历map中的值 for (String value : map.values()) { System.out.println("value = " + value); } System.out.println("方法一:end"); System.out.println("------------"); System.out.println("方法二:begin"); Iterator> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); System.out.println("key="+entry.getKey()+"value="+entry.getValue()); } System.out.println("方法二:end"); //将香蕉的价格改为10.9 map.put("香蕉", "10.9"); //删除桃子这组数据 map.remove("桃子"); //写入文件 String filePath = Test.class.getClass().getResource("/").getPath()+ "map.txt"; System.out.println(filePath); File f = new File(filePath); FileWriter fw = new FileWriter(f);; BufferedWriter bw = new BufferedWriter(fw); Iterator> it2 = map.entrySet().iterator(); while (it2.hasNext()) { Map.Entry entry = (Map.Entry) it2.next(); bw.write(entry.getKey()+"="+entry.getValue()+"元" ); bw.newLine(); } bw.flush(); bw.close(); fw.close(); } }

求下题代码,java题目。

Java程序:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
 * 汽车类
 */
class Car {
/**
 * 汽车编号
 */
protected int id = 0;
/**
 * 汽车款式
 */
protected String type = null;
/**
 * 构造汽车对象
 */
public Car() {
}
/**
 * 构造汽车对象
 * @param id 汽车编号
 * @param type 汽车款式
 */
public Car(int id, String type) {
this.id = id;
this.type = type;
}
/**
 * 获得汽车编号
 * @return 汽车编号
 */
public int getId() {
return this.id;
}
/**
 * 获得汽车款式
 * @return 汽车款式
 */
public String getType() {
return this.type;
}
}
/**
 * 汽车销售人员类
 */
class Saler {
/**
 * 姓名
 */
protected String name = null;
public Listcars = new ArrayList(); /** * 构造销售汽车人员对象 */ public Saler() { } /** * 构造汽车销售人员对象 * @param name 姓名 */ public Saler(String name) { this.name = name; } /** * 获得姓名 * @return 姓名 */ public String getName() { return this.name; } } public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); List allCar = new ArrayList(); //待售汽车对象的集合 allCar.add(new Car(1001, "凯越")); allCar.add(new Car(1002, "凯越")); allCar.add(new Car(1003, "凯越")); allCar.add(new Car(1004, "凯越")); allCar.add(new Car(2001, "君威")); allCar.add(new Car(2002, "君威")); allCar.add(new Car(2003, "君威")); allCar.add(new Car(2004, "君威")); allCar.add(new Car(2005, "君威")); Saler saler = new Saler("张三其"); int choice = 0; int type; //销售车型 int num; //销售数量 while(true) { System.out.println("请选择销售方式"); System.out.println("按车辆销售: 1"); System.out.println("按车型销售: 2"); System.out.println("查看销售情况: 3"); System.out.println("退出: 0"); System.out.print("您的选择:"); choice = scan.nextInt(); switch(choice) { case 0: //退出系统 System.out.println("退出系统"); System.exit(0); break; case 1: //按车辆销售 for(Car car : allCar) { if(! exists(saler.cars, car)) { saler.cars.add(car); System.out.printf(" 售出 %s 1 辆 ", car.getType()); break; } } break; case 2: //按车型销售 System.out.print("车型(凯越 0/君威 1):"); type = scan.nextInt(); System.out.print("销售数量:"); num = scan.nextInt(); int c = 0; //实际销售数量 for(Car car : allCar) { if(c >= num) { break; } if(car.getType().equals(type == 0 ? "凯越" : "君威") && ! exists(saler.cars, car)) { saler.cars.add(car); c++; } } if(c < num) { System.out.printf(" 库存不足,实际售出 %s %d 辆 ", type == 0 ? "凯越" : "君威", c); } else { System.out.printf(" 售出 %s %d 辆 ", type == 0 ? "凯越" : "君威", num); } break; case 3: //查看销售情况 System.out.println(" 当前销售情况一览"); System.out.printf(" %10s%10s ", "汽车款式", "汽车编号"); for(Car car : saler.cars) { System.out.printf(" %10s%10d ", car.getType(), car.getId()); } System.out.println("---------------------------"); System.out.printf(" 小计: %d 辆 ", saler.cars.size()); break; default: break; } try { System.in.read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //判断car在cars中是否存在 public static boolean exists(List cars, Car car) { for(Car c : cars) { if(c.getId() == car.getId()) { return true; } } return false; } } 运行测试: 请选择销售方式 按车辆销售: 1 按车型销售: 2 查看销售情况: 3 退出: 0 您的选择:1 售出 凯越 1 辆 请选择销售方式 按车辆销售: 1 按车型销售: 2 查看销售情况: 3 退出: 0 您的选择:2 车型(凯越 0/君威 1):0 销售数量:3 售出 凯越 3 辆 请选择销售方式 按车辆销售: 1 按车型销售: 2 查看销售情况: 3 退出: 0 您的选择:3 当前销售情况一览 汽车款式 汽车编号 凯越 1001 凯越 1002 凯越 1003 凯越 1004 --------------------------- 小计: 4 辆 请选择销售方式 按车辆销售: 1 按车型销售: 2 查看销售情况: 3 退出: 0 您的选择:0 退出系统

java题:设计point类用来定义平面上的一个点,用构造函数传递坐标位置编写测试类在该类中实现point类的对

第一题:
/* MyPoint.java */
// class MyPoint
public class MyPoint {
public MyPoint(int x,int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
private int x;
private int y;
}
/* Test.java */
// class Test
public class Test{
public static void main(String[] args) {
MyPoint point = new MyPoint(3, 4);
System.out.println(point.getX());
System.out.println(point.getY());
}
}
第二题:
/* MyRect.java */
package myShapes;
// class MyRect
public class myRect {
public MyRect() {};
public MyRect(int h, int w) {
width = w;
height = h;
}
public void setWidth(int w) {
width = w;
}
public void setHeight(int h) {
height = h;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int area() {
return width * height;
}
public int perimeter() {
return 2 * (width + height);
}
private int width;
private int height;
}
/* Test.java */
package myShapes;
// class Test
public class Test() {
public static void main(String[] args) {
MyRect rect = new MyRect(8, 9);
System.out.println(rect.getWidth());
System.out.println(rect.getHeight());
System.out.println(rect.area());
System.out.println(rect.perimeter());
MyRect rect=new MyRect();
rect.setWidth(7);
rect.setHeight(8);
System.out.println(rect.getWidth());
System.out.println(rect.getHeight());
System.out.println(rect.area());
System.out.println(rect.perimeter());
}
}
第二题的程序文件要存在myShapes的文件夹中。

java题目是这样的:随机输入一个人的姓名,然后分别输出姓和名

package baidu;
import java.util.Scanner;
//java.util.Scanner的使用
public class h
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("请输入姓名:");
while (input.hasNext()) {
String a = input.next();
System.out.println("您输入的姓名的姓是: "+a.substring(0,1));
System.out.println("您输入的姓名的名字是: "+a.substring(1));
//System.out.println(a.replaceAll("\D",""));
}
input.close();
}
}
运行结果
请输入姓名:
李岩
您输入的姓名的姓是: 李
您输入的姓名的名字是: 岩

java题:设计一个班有10名学生,每个学生有姓名、学号、成绩等信息,利用对象数组的概念编程,求平均成绩

package org.dylan.test;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Class {
private List<Student> students = new ArrayList<Student>();
//求班级所有学生的平均成绩
public double avg() {
double sum = 0;
for (Student stu : students) {
System.out.println("学生["+stu.name+"]的成绩为:" + stu.score);
sum += stu.score;
}
return sum / students.size();
}
//给班级中增加一个学生
public void addStudent(Student stu) {
students.add(stu);
}
public static void main(String[] args) {
Class cla = new Class();//创建一个班级,
//为班级增加10个学生
for(int i = 0; i < 10; i++){
String name = "student" + i;
String code = "code" + i;
cla.addStudent(new Student(name,code,geneScore()));
}
System.out.println("班级成员的平均成绩为:" + cla.avg());
}
//随机生成成绩
public static double geneScore(){
return new Random().nextInt(60) + 40;
}
}
class Student {
String name;
String code;
double score;
public Student(String name, String code, double score) {
this.name = name;
this.code = code;
this.score = score;
}
}

java 题目:有三个学生,每个学生有五门课程的成绩,从键盘输入以下数据:学生号,五门课程成绩。

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String [][] arrs = new String[3][6];
for (int i = 0; i <arrs.length; i++) {
System.out.println("请输入第"+(i+1)+"学生号!");
String stuID = input.next();
arrs[i][0] = stuID;
for (int j = 1; j <arrs[i].length; j++) {
System.out.println("请输入第"+j+"门课成绩!");
int grade = input.nextInt();
if(grade<0){
System.out.println("输入有误,请重新输入第"+j+"门课成绩!");
}
arrs[i][j] = grade+"";
}
}
for (int i = 0; i < arrs.length; i++) {
for (int j = 0; j < arrs[i].length; j++) {
System.out.print(arrs[i][j]+" ");
}
System.out.println();
}
System.out.println("--------------------------------");
for (int i = 1; i < arrs[0].length; i++) {
int [] index = {i};
sort(arrs,index);
System.out.println("第"+i+"门的成绩最好的学号为:"+arrs[2][0]);
}
}
public static void sort(String[][] ob, final int[] order) {
Arrays.sort(ob, new Comparator<Object>() {
public int compare(Object o1, Object o2) {
String[] one = (String[]) o1;
String[] two = (String[]) o2;
for (int i = 0; i < order.length; i++) {
int k = order[i];
if (Integer.parseInt(one[k]) > Integer.parseInt(two[k])) {
return 1;
} else if (Integer.parseInt(one[k]) < Integer.parseInt(two[k])) {
return -1;
} else {
continue; //如果按一条件比较结果相等,就使用第二个条件进行比较。
}
}
return 0;
}
});
}
我用的是二维数组录入成绩的方便用比较最大值!你可以换个容器试试,希望能帮到你!望采纳给赞!谢谢

相关推荐: