一道JAVA编程题 请帮我解答 谢谢 没多少分 只要做对了 我的分数都给你

作者&投稿:恽菊 (若有异议请与网页底部的电邮联系)
JAVA网络编程基础试题!300分!请一定要帮我做一下,两个解答题。一个编程题,重点是编程题,谢谢了!!~

2.百度百科搜索很详细
3.附件中的代码仅供参考
TcpSocketServerDemo.java
package yaoshun.InetAddress;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.util.ArrayList;import java.util.List;import java.util.Random;/* * 客户端:TCP服务器端 */public class TcpSocketServerDemo {private ServerSocket serverSocket;// 服务器连接private int port; // 服务器监听端口private List sockets;private int ranNumber;/** * 服务器结束的标志 */public boolean ISEND;public TcpSocketServerDemo(int port) {this.port = port;serverSocket = null;sockets = new ArrayList();ranNumber = new Random().nextInt(50);}/* * 启动服务器 */public void starServer() {try {// 第一步:建立服务器连接(绑定监听端口)serverSocket = new ServerSocket(port);System.out.println("姚舜的服务器已经启动...");Socket socket;// 客户端连接(如果建立连接时,本机网络不容或服务器程序未启动则会抛出异常)ServerThread serverThread;Thread t2;while (!ISEND) {// 第二步:监听端口(获取客户端连接)socket = serverSocket.accept();if (ISEND) {while (!socket.isClosed()) {socket.close();Thread.sleep(100);}} else {sockets.add(socket);serverThread = new ServerThread(this, socket);t2 = new Thread(serverThread);t2.start();}}for (Socket s : sockets) {s.close();}serverSocket.close();} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {TcpSocketServerDemo tDemo = new TcpSocketServerDemo(1000);tDemo.starServer();}/** * 校验 * * @param i * @return */public int check(int i) {if (i ranNumber) {return 1;}return 0;}}/** * 服务端处理线程 * */class ServerThread implements Runnable {private TcpSocketServerDemo tDemo;private Socket socket;private DataInputStream dis;// 输入流(读取数据)private DataOutputStream dos;// 输出流(发送数据)public ServerThread(TcpSocketServerDemo tDemo, Socket socket) {super();this.tDemo = tDemo;this.socket = socket;try {dis = new DataInputStream(socket.getInputStream());dos = new DataOutputStream(socket.getOutputStream());} catch (IOException e) {e.printStackTrace();}}public void run() {String message = "";int i;int index = 0;int result = 0;while (!message.equals("Quit") && index < 5) {try {message = dis.readUTF();} catch (IOException e) {e.printStackTrace();}try {i = Integer.parseInt(message);result = tDemo.check(i);if (result == 1) {dos.writeUTF("数字大了");} else if (result == -1) {dos.writeUTF("数字小了");}if (result == 0) {dos.writeUTF("猜中了");break;}} catch (NumberFormatException | IOException e) {continue;}index++;}try {dis.close();dos.close();socket.close();} catch (IOException e) {e.printStackTrace();}}}TcpSocketClientDemo.java
package yaoshun.InetAddress;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;import java.net.UnknownHostException;import java.util.Scanner;/* * 演示类:TCP客户端 */public class TcpSocketClientDemo {private Socket socket;// 客户端连接(如果建立连接时,本机网络不容或服务器程序未启动则会抛出异常)private DataInputStream dis;// 输入流(读取数据)private DataOutputStream dos;// 输出流(发送数据)// private String serverIP;// 服务器IP地址// private int serverPort;// 服务器监听端口private String sendData;// 发送的数据public TcpSocketClientDemo(String serverIP, int serverPort) throws UnknownHostException, IOException {// this.serverIP = serverIP;// this.serverPort = serverPort;socket = new Socket(serverIP, serverPort);dis = new DataInputStream(socket.getInputStream());dos = new DataOutputStream(socket.getOutputStream());sendData = null;startClient();}public void startClient() {try {// 第一步:创建客户端连接System.out.println("===我的客户端界面===");// 第二步:客户端向服务器发送数据int i = 0;Scanner input = new Scanner(System.in);while (i < 5) {System.out.println("请输入你的内容(客户端的):");sendData = input.nextLine();if (sendData.equals("quit")) {break;}dos.writeUTF(sendData);sendData = dis.readUTF();System.out.println("接收到服务器内容:" + sendData);if (i == 5) {System.out.println("超过允许次数,请重新连接服务器.");}if (sendData.equals("猜中了")) {i = 5;}i++;}input.close();} catch (Exception e) {e.printStackTrace();} finally {// 第四步:关闭输入流、输出流和客户端连接try {dis.close();dos.close();socket.close();} catch (Exception e) {e.printStackTrace();}}}public static void main(String[] args) {try {new TcpSocketClientDemo("127.0.0.1", 1000);} catch (IOException e) {e.printStackTrace();}}}

仅供参考,未测试
Question1
package com.kidd.atmtest;import java.util.Scanner;public class Question1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("输入基本运费,货重,距离(均为整数,用,分割):"); String[] ss = scanner.next().split(","); int p = Integer.parseInt(ss[0]); int w = Integer.parseInt(ss[1]); int s = Integer.parseInt(ss[2]); double d ; if(s < 250){ d=0 ; }else if(s < 500){ d=0.02; }else if(s < 1000){ d=0.05; }else if(s < 2000){ d=0.08 ; }else{ d=0.1; } System.out.println("总运费f="+(p*w*s*(1-d))); }}Question2
package com.kidd.atmtest;import java.util.Scanner;public class Question2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入x:"); int x = scanner.nextInt(); int y ; if(x0){ y=1; }else{ y=0; } System.out.println("y="+y); }}Question3
package com.kidd.atmtest;import java.util.Scanner;public class Question3 { public static void main(String[] args) { int index = 0; Scanner scanner = new Scanner(System.in); boolean end = false; while (!end) { System.out.print("请输入卡号,密码:"); scanner.next(); index++; System.out.print("账号或密码错误次数" + index + ","); if (index >= 3) { System.out.println("吞卡"); end = true; } else { System.out.println("请重新输入."); } } }}Question4
package com.kidd.atmtest;import java.util.Scanner;public class Question4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("输入一个整数:"); String string = scanner.next(); for (int i = 0, k = string.length(); i < k; i++) { System.out.println(string.charAt(i)); } }}Question5
package com.kidd.atmtest;import java.util.Random;import java.util.Scanner;public class Question5 { public static void main(String[] args) { int i = new Random().nextInt(40) + 60; boolean end = false; int n; int index = 1; Scanner scanner = new Scanner(System.in); while (!end) { System.out.print("请输入你猜的结果:"); n = scanner.nextInt(); if (n > i) { System.out.println("大了"); index++; } else if (n < i) { System.out.println("小了"); index++; } else { end = true; } } System.out.print("猜对了,一共猜了" + index + "次,你是"); if (index < 5) { System.out.print("天才."); } else { System.out.print("笨蛋."); } }}Question6
package com.kidd.atmtest;import java.util.Scanner;public class Question6 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入要打印的元素个数:"); int n = scanner.nextInt(); if (n = 1) { System.out.print("1"); } if (n >= 2) { System.out.print(",2"); } if (n > 2) { int f1 = 1; int f2 = 2; int sum; for (int i = 3; i <= n; i++) { sum = f1 + f2; System.out.print("," + (f1 + f2)); f1 = f2; f2 = sum; } } }}

package xue;
import java.util.ArrayList;import java.util.List;
public class SortTest {
public static void main(String[] args){
ArrayList<String[]> sortList=new ArrayList<String[]>();
sortList.add(new String[]{"企业1","110",null,null,"410","1"});
sortList.add(new String[]{"企业2","100",null,null,null,null});
sortList.add(new String[]{"企业3","100","250",null,"400",null});
sortList.add(new String[]{"企业4","110",null,null,null,null});
sortList.add(new String[]{"企业5","100","220",null,null,null});

System.out.println("原来的排序");
display(sortList);
sortList=sortArray(sortList);
System.out.println("新的排序");
display(sortList);
}

public static ArrayList<String[]> sortArray(ArrayList<String[]> sortList){
ArrayList<String[]> result=new ArrayList<String[]>();
int firstTime=1;//第一次报价下标
int lastTime=4;//最后一次报价下标
ArrayList<String[]> temList=new ArrayList<String[]>();
temList.addAll(sortList);
int count=0;
for(int i=lastTime;i>=firstTime;i--){//从第4此报价开始比较
if(hasPrice(temList,i)==false){//全部都没有报价
continue;
}
ArrayList<ArrayList<Integer>> priceSortList=sortPriceAsc(temList,i);//得到第i次有报价的公司
ArrayList<Integer> addList=priceSortList.get(0);//公司按报价从低到高的公司序号(序号是混乱的)
for(int k=0;k<addList.size();k++){//往结果集中按报价递增顺序添加公司
result.add(temList.get(addList.get(k)));
count++;
}
if(count>=sortList.size()){
break;//如果已经排序完所有的公司,则跳出循环
}
List<Integer> removeList=priceSortList.get(1);//这些报价公司序号(序号从小到大,序号有序)
int lastindex=removeList.size()-1;
for(int j=lastindex;j>=0;j--){//移除临时列表temList已经报价了的公司
int index=removeList.get(j);
temList.remove(index);
}
}
//把中标的公司移到末尾
for(int i=0;i<result.size();i++){
if(result.get(i)[5]!=null&&result.get(i)[5].equals("1")){
String[] tem=result.get(i);
result.remove(i);
result.add(tem);
break;
}
}
return result;
}
public static ArrayList<ArrayList<Integer>> sortPriceAsc(ArrayList<String[]> pList,int pTime){
ArrayList<int[]> tarList=new ArrayList<int[]>();
ArrayList<Integer> removeList=new ArrayList<Integer>();
outer:for(int i=0;i<pList.size();i++){
if(pList.get(i)[pTime]==null){//没报价的不考虑
continue;
}
int price=Integer.valueOf(pList.get(i)[pTime]);//报价价格
for(int j=0;j<tarList.size();j++){
if(price<tarList.get(j)[1]){//如果报价及格比J的价格高,则应该把该公司拍到J的前面
tarList.add(j,new int[]{i,price});
removeList.add(i);//添加一个公司
continue outer;//跳出,继续判断下一个公司
}
}
//如果价格比tarList里面的虽有价格都高,则放到最后面
tarList.add(new int[]{i,price});
removeList.add(i);
}
ArrayList<Integer> addList=new ArrayList<Integer>();
for(int p=0;p<tarList.size();p++){
addList.add(tarList.get(p)[0]);
}
ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Integer>>();
result.add(addList);//公司按照价格的列表
result.add(removeList);//公司序号的列表(pList的下标索引递增列表)
return result;
}

public static boolean hasPrice(ArrayList<String[]> sortList,int pTime){
for(int i=0;i<sortList.size();i++){//判断某一轮是否有公司报价
if(sortList.get(i)[pTime]!=null){
return true;
}
}
return false;
}

public static void display(ArrayList<String[]> sortList){
for(int i=0;i<sortList.size();i++){
String[] tem=sortList.get(i);
for(int j=0;j<tem.length-1;j++){
System.out.print(tem[j]+",");
}
System.out.println(tem[tem.length-1]);
}
}
}

其实自己想想更好!更能学到知识,以上代码可以参考一下

package test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class SortTest {
public static void main(String[] args) {
ArrayList<String[]> sortList = new ArrayList<String[]>();
sortList.add(new String[] { "企业1", "110", null, null, "410", "1" });
sortList.add(new String[] { "企业2", "100", null, null, null, null });
sortList.add(new String[] { "企业3", "100", "250", null, "400", null });
sortList.add(new String[] { "企业4", "110", null, null, null, null });
sortList.add(new String[] { "企业5", "100", "220", null, null, null });
System.out.println("原来的排序");
display(sortList);
sortList = sortArray(sortList);
System.out.println("新的排序");
display(sortList);
}
public static ArrayList<String[]> sortArray(List<String[]> sortList){
ArrayList<String[]> backList = new ArrayList<String[]>(); //返回的
Map<Integer,Map<Integer,Integer>> map = new TreeMap<Integer,Map<Integer,Integer>>();
String[] zb = null;
int size = sortList.size();
for(int i=0;i<size;i++){
String [] temp = sortList.get(i);
if("1".equals(temp[5])){
zb = temp;
continue;
}
for(int j=4;j>0;j--){
if(temp[j]!=null){
Map<Integer,Integer> temap = map.get(4-j);
if(temap==null){
temap = new TreeMap<Integer,Integer>();
}
temap.put(Integer.parseInt(temp[j]),i);
map.put(4-j,temap);
break;
}
}
}
Set<Integer> tset = map.keySet();
Iterator<Integer> it = tset.iterator();
while(it.hasNext()){
backList.addAll(getPriceSortList(sortList,map.get(it.next())));
}
backList.add(zb);
return backList;
}
private static List<String[]> getPriceSortList(List<String[]> sortList,Map<Integer,Integer> m){
List<String[]> backList = new ArrayList<String[]>();
Set<Integer> kset = m.keySet();
Iterator<Integer> it = kset.iterator();
while(it.hasNext()){
backList.add(sortList.get(m.get(it.next())));
}
return backList;
}
public static void display(ArrayList<String[]> sortList) {
for (int i = 0; i < sortList.size(); i++) {
String[] tem = sortList.get(i);
for (int j = 0; j < tem.length - 1; j++) {
System.out.print(tem[j] + ",");
}
System.out.println(tem[tem.length - 1]);
}
}
}

嗯 比上面那个哥们的效率低了一点,给你参考参考吧

自己想想更好!更能学到知识 +1


双台子区17038338964: 请帮我用java程序回答以下问题,谢谢了!1.试编写一程序,读入5个数据,输出其中的最大数和最小数.2.试编写一程序,读入10个学生的成绩,成绩在0~... -
贯娥鹿茸:[答案] //question 1 import java.io.*; public class Q1{ static InputStreamReader in; static BufferedReader breader = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) { int a[] = new int[5]; int i,max,min; try{ for(i=0;i { ...

双台子区17038338964: 简单小题,大家帮忙这是java最基础的一道编程题,是构造一个数组,然后输出最大值,下面的代码也是正确的,但貌似不是很符合编码规范,大家帮忙转... -
贯娥鹿茸:[答案] public class Max {private int array[] = { 2,5,3,8,6 };public Max(){}public void getMax(){int i;int max = array[0];for (i = 0; i max) {max = array[i];}}System.out.p...

双台子区17038338964: 跪求大佬解答一道Java编程题!!! -
贯娥鹿茸: 参考代码 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JSFrame extends Frame implements ActionListener { //本类继承于Frame,实现ActionListener TextField tf1, tf2, tf3, tf4; public JSFrame() { Panel jp = new Panel(...

双台子区17038338964: 一道Java编程题 -
贯娥鹿茸: import java.util.Scanner; public class Test { int x; public void show() { Scanner scanner = new Scanner(System.in); x= scanner.nextInt(); StringBuffer stringBuffer = new StringBuffer(new String(Integer.toString(x))); String reverse = stringBuffer.reverse().toString(); System.out.println(reverse); } }

双台子区17038338964: 一道JAVA程序设计题,高手帮忙,谢了 -
贯娥鹿茸: public class Square { private double a; public Square(double a) { this.a = a; } public double getV() { return a * a * a; } public double getA() { return 6 * a * a; } public static void main(String[] args) { Square s = Square(3.0); System.out.println("体积为:" + s.getV()); System.out.println("面积为:" + s.getA()); } }

双台子区17038338964: 这是一个JAVA编程题目,求大神解答 -
贯娥鹿茸: 用二维数组肯定没什么难度了 a[i][j] = i * j 即可把结果存到数组中.需要数组大小为81 一维数组存储也很简单.数组大小为45 把数据结果存进去,按照楼上的方法就能将乘法表里的各个结果从上到下依次存进去.既然存储肯定是用来查找的,比如2*3,怎么找到它的结果呢,在乘法表上2*3的位置在第三行第2个位置,计算第一行和第二行的所有结果个数是个等差数列3(3-1)/2,再加2就是所求结果所在的位置,i * j的位置在 result[j*(j-1)/2+i-1].也许你觉得用乘除法来计算乘法表结果的位置有点搞笑,如果存储的是复杂数据的话就有用了 说了半天,其实跟散列表差不多啦

双台子区17038338964: 求救!! 一道JAVA题目! 重谢!今晚完成!!! -
贯娥鹿茸: import java.util.*;class P{ public List ct(int max,int n){ List list=new ArrayList(); for(int i=0;i<n;i++) list.add((int)(Math.random()*100000)%max)...

双台子区17038338964: 有一道Java编写程序的题求大神解答,万分感谢. -
贯娥鹿茸: public class Student { private String id; private String name; public Student(String id, String name) { super(); this.id = id; this.name = name; } public String getId() { return id; } public String getName() { return name; } public void disp() { System.out....

双台子区17038338964: 一道java题目,求解答 -
贯娥鹿茸: try { String filePath = "d:/temp/"; File inFile = new File(filePath); File[] files = inFile.listFiles(); for(int i = 0; i < files.length; i++) { File file = files[i]; if(file != null && file.isFile()) { String fileName = file.getName(); if("java".equalsIgnoreCase(fileName....

双台子区17038338964: 急!求高手帮我解决一道java编程题~
贯娥鹿茸: import javax.swing.JOptionPane; class YuanDan{ public static void main(String args[]){ int s,y,m,d; String day=JOptionPane.showInputDialog("请输入一个日期(年-月-日):"); y=Integer.parseInt(day.substring(0,day.indexOf("-"))); m=...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网