「java超难题」java比较难得问题

博主:adminadmin 2023-03-19 23:36:09 477

本篇文章给大家谈谈java超难题,以及java比较难得问题对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java超难题,高手帮我看一下下哪里有错误,重谢!

你的程序没有错误,只是没有实现反转写入。

//package com.color.io;

/*

* Main.java

*

* Created on 2000年9月29日, 下午5:17

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.Reader;

import java.io.Writer;

/**

*

* @author admin

*/

public class Main {

/** Creates a new instance of Main */

public Main() {

}

/**

* @param args

* the command line arguments

* @throws IOException

*/

public static void main(String[] args) throws IOException {

// TODO code application logic here

//按文件大小来产生构建buffer

char[] buffer = new char[(int)new File("D:\\新建 文本文档.txt").length()];

Reader reader = null;

try {

reader = new FileReader("D:\\新建 文本文档.txt");

int offset;

while ((offset = reader.read(buffer)) 0)

System.out.print(new String(buffer, 0, offset));

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (reader != null)

try {

reader.close();

} catch (IOException e) {

}

}

//将整个文件读入,然后构造成一个String

String s = new String(buffer);

//将这个String按换行符拆分成String数组

String [] reverse = s.split("\r\n");

// 构造文件,原来那个文件

File file = new File("D:\\新建 文本文档.txt");

Writer writer = new FileWriter(file);

for(int i=reverse.length-1;i=0;i--){

//反转写入

writer.write(reverse[i]+"\r\n");

}

writer.close();

}

}

java难题,欢迎各位能够帮一下忙,谢谢各位帮忙

public interface Drinkable {

/**

* 饮用建议

*/

void drinkAdvice();

}

public class Wine implements Drinkable {

private String color;

@Override

public void drinkAdvice() {

}

public String showColor(){

System.out.println("酒的颜色:" + this.color);

return "酒的颜色:" + this.color;

}

public void setColor(String color) {

this.color = color;

}

}

public class Milk implements Drinkable {

private String flavor;

@Override

public void drinkAdvice() {

}

public String showFavor() {

System.out.println("牛奶风味:" + this.flavor);

return "牛奶风味:" + this.flavor;

}

public void setFlavor(String flavor) {

this.flavor = flavor;

}

}

public class Drinker {

public void toDrink(Drinkable drink, String str) {

if (drink instanceof Wine) {

Wine wine = (Wine) drink;

wine.setColor(str);

wine.showColor();

} else {

Milk milk = (Milk) drink;

milk.setFlavor(str);

milk.showFavor();

}

}

}

public class Test {

public static void main(String[] args) {

Drinkable wine = new Wine();

Drinkable milk = new Milk();

Drinker drinker = new Drinker();

drinker.toDrink(wine, "红色");

drinker.toDrink(milk, "原味");

}

}

Java 三道难题,希望大神详细解答。拜托拜托

三个题目一起贴代码太多了, 百度不允许贴这么多代码。 只能贴前面2题的代码

第一题:

import java.util.Scanner;

/**

 * @author young

 * @Description: TODO

 * @date 2016年5月31日下午5:52:11

 */

public class Test {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Please input the number: ");

int num = sc.nextInt();

if (num  0 || num  1000000) {

System.out.println("Input Error");

System.exit(0);

} else {

String word = String.valueOf(num);

int i = word.length();// 获取长度

int j = 0;

while (j = (i / 2) - 1  word.charAt(j) == word.charAt(i - j - 1))

j++;

if (j == i / 2)

System.out.println("palindrome number.");

else

System.out.println("not palindrome number.");

sc.close();

}

}

}

第二题:

/**

 * @author young

 * @Description: TODO

 * @date 2016年5月31日下午5:52:11

 */

public class Test {

public static void main(String[] args) {

int[] b = new int[50];

int sum = 0, avg = 0, t1 = 0, t2 = 0, t3 = 0,t4 = 0,t5 = 0;

for(int i = 0; i  b.length; i++){

// b[i] = (int)((100)*Math.random());

b[i] = (int)(0+Math.random()*(100-0+1));

sum+=b[i];

if(b[i]=0  b[i]  60){

t1++;

}else if(b[i]=60  b[i]  70){

t2++;

}else if(b[i]=70  b[i]  80){

t3++;

}else if(b[i]=80  b[i]  90){

t4++;

}else if(b[i]=90  b[i] = 100){

t5++;

}

}

//每10行输出数组

int count = 1;

for(int i = 0; i  b.length; i++){

System.out.format("%4s", b[i]);

count++;

if(count%10 == 0){

System.out.println();

count = 1;

}

}

//冒泡排序 从大到小

for (int k = 0; k  b.length - 1; k++) {

for (int j = k + 1; j  b.length; j++) { // 升序把改成

if (b[k]  b[j]) {

int temp = b[k];

b[k] = b[j];

b[j] = temp;

}

}

}

System.out.println("\n最大值:" + b[0]);

System.out.println("最小值:" + b[49]);

System.out.println("平均值:" + (sum / 50));

System.out.println("[0,60)区间个数:" + t1);

System.out.println("[60,70)区间个数:" + t2);

System.out.println("[70,80)区间个数:" + t3);

System.out.println("[80,90)区间个数:" + t4);

System.out.println("[90,100]区间个数:" + t5);

}

}

java难题,请求各位高手帮助我解答一下,大家有难题互帮互助?

import java.util.Scanner;

interface Traffic{

double calc(double weight,double distance);//计算运费

}

class Trunk implements Traffic{

@Override

public double calc(double weight,double distance) {

return (distance=1000weight=60)?(weight*distance*120):(-1);

}

}

class Train implements Traffic{

@Override

public double calc(double weight, double distance) {

return (distance=900)?(weight*distance*250):(distance*weight*300);

}

}

class Plane implements Traffic{

@Override

public double calc(double weight, double distance) {

return (distance500)?(weight*distance*750):(-1);

}

}

class ZuChe{

private double distance;//距离

private double weight;//重量

public String useTraffic(Traffic t){

double result = t.calc(weight, distance);

return (result!=-1)?(""+result):(t.getClass().getName().equals(Trunk.class.getName())?"超重或者距离太远不能接单":"拒载");

}

public ZuChe setDistance(double distance) {

this.distance = distance;

return this;

}

public ZuChe setWeight(double weight) {

this.weight = weight;

return this;

}

}

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

double weight = sc.nextDouble();

double distance = sc.nextDouble();

ZuChe zuche = new ZuChe().setDistance(distance).setWeight(weight);

System.out.println("卡车的运费是:"+zuche.useTraffic(new Trunk()));

System.out.println("火车的运费是:"+zuche.useTraffic(new Train()));

System.out.println("飞机的运费是:"+zuche.useTraffic(new Plane()));

}

}

java超难题的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java比较难得问题、java超难题的信息别忘了在本站进行查找喔。