JAVA语言修改的代码 java代码改了不起作用

Java语言编写代码代码如下
/**
* Author: zhyx
* Date:2017/11/30
* Time:8:56
*/
public abstract class Contailner {
double r;
abstract double volume();
}
/**
* Author: zhyx
* Date:2017/11/30
* Time:8:57
*/
public class Cube extends Contailner {
public Cube(double r) {
this.r=r;
}
@Override
double volume() {
return r*r*r;
}
}
/**
* Author: zhyx
* Date:2017/11/30
* Time:9:01
*/
public class Sphere extends Contailner {
public Sphere(double r) {
this.r=r;
}
@Override
double volume() {
return 4/3*Math.PI*r*r*r;
}
}
/**
* Author: zhyx
* Date:2017/11/30
* Time:9:02
*/
public class Tiji {
public static void main(String[] args) {
Cube cube=new Cube(4);
System.out.println("立方体体积为:" cube.volume());
Sphere sphere= new Sphere(4);
System.out.println("球体体积为:" sphere.volume());
}
}
JAVA语言 按我这个代码编下去 不会四舍五入 要改什么知识点1:
java中直接书写的小数, 默认是double类型
如果要表达是浮点类型的小数, 要在小数后面跟上字母f或者F .为了更加明显的看清楚,建议在小数后面添加大写字母F
double d = 3.14; //默认double类型
floatf = 3.14F;
double
知识点2: 四舍五入和保留小数的问题, 一般使用String.format()方法来实现
参考代码
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//double 32位, float 16位
double d1 = 1.5F;// 小范围的float的值,可以直接赋值给大范围的double
float f1 = (float) 1.3; //大范围的double,转成小范围的float,需要强制转换
System.out.println("请输入半径:");// 进行友好的提示
Scanner sc = new Scanner(System.in);
float r = sc.nextFloat();
float s = 3.14F * r * r;
// 直接写3.14 那么是double类型.
// 解决办法3.14后面加1个字母f或者F,表示float类型的数字
//四舍五入,保留2为小数
String str1 = String.format("%.2f", s);// 2代表小数点后面的位数, 不足补0 . f代表,后面的数据s是浮点类型
System.out.println("圆的面积是:"str1);
}
}
输出
请输入半径:
10
圆的面积是:314.00
用JAVA语言设计一个类,统计一篇英文文章的词频,并按照词频由高到低输出 。修改下面代码就行了 。这题目如果能增加一个类的话会高效很多 。。。如果非要在这个框框里面,代码麻烦 效率低下呢 。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class Article {
//保存文章的内容
String content;
//保存分割后的单词集合
String[] rawWords;
//保存统计后的单词集合
String[] words;
//保存单词对应的词频
int[] wordFreqs;
//构造函数,输入文章内容
//提高部分:从文件中读取
public Article() {
content = "kolya is one of the richest films i've seen in some time . zdenek sverak plays a confirmed old bachelor ( who's likely to remain so ) , who finds his life as a czech cellist increasingly impacted by the five-year old boy that he's taking care of . though it ends rather abruptly-- and i'm whining , 'cause i wanted to spend more time with these characters-- the acting , writing , and production values are as high as , if not higher than , comparable american dramas . this father-and-son delight-- sverak also wrote the script , while his son , jan , directed-- won a golden globe for best foreign language film and , a couple days after i saw it , walked away an oscar . in czech and russian , with english subtitles . ";
}
//对文章根据分隔符进行分词,将结果保存到rawWords数组中
public void splitWord(){
//分词的时候 , 因为标点符号不参与,所以所有的符号全部替换为空格
final char SPACE = ' ';
content = content.replace('\'', SPACE).replace(',', SPACE).replace('.', SPACE);
content = content.replace('(', SPACE).replace(')', SPACE).replace('-', SPACE);
rawWords = content.split("\\s ");//凡是空格隔开的都算单词,上面替换了', 所以I've 被分成2个 //单词
}
//统计词,遍历数组
public void countWordFreq() {
//将所有出现的字符串放入唯一的set中 , 不用map,是因为map寻找效率太低了
SetString set = new TreeSetString();
for(String word: rawWords){
set.add(word);
}
Iterator ite = set.iterator();
ListString wordsList = new ArrayListString();
ListInteger freqList = new ArrayListInteger();
//多少个字符串未知,所以用list来保存先
while(ite.hasNext()){
String word = (String) ite.next();
int count = 0;//统计相同字符串的个数
for(String str: rawWords){
if(str.equals(word)){
count;
}
}
wordsList.add(word);
freqList.add(count);
}
//存入数组当中
words = wordsList.toArray(new String[0]);
wordFreqs = new int[freqList.size()];
for(int i = 0; ifreqList.size(); i){
wordFreqs[i] = freqList.get(i);
}
}
//根据词频,将词数组和词频数组进行降序排序
public void sort() {
class Word{
private String word;
private int freq;
public Word(String word, int freq){
this.word = word;
this.freq = freq;
}
}
//注意:此处排序,1)首先按照词频降序排列, 2)如果词频相同 , 按照字母降序排列,
//如 'abc''ab' 'aa'
class WordComparator implements Comparator{
public int compare(Object o1, Object o2) {
Word word1 = (Word) o1;
Word word2 = (Word) o2;
if(word1.freqword2.freq){
return 1;
}else if(word1.freqword2.freq){
return -1;
}else{
int len1 = word1.word.trim().length();
int len2 = word2.word.trim().length();
String min = len1len2? word2.word: word1.word;
String max = len1len2? word1.word: word2.word;
for(int i = 0; imin.length(); i){
if(min.charAt(i)max.charAt(i)){
return 1;
}
}
return 1;
}
}
}
List wordList = new ArrayListWord();
for(int i = 0; iwords.length; i){
wordList.add(new Word(words[i], wordFreqs[i]));
}
Collections.sort(wordList, new WordComparator());
for(int i = 0; iwordList.size(); i){
Word wor = (Word) wordList.get(i);
words[i] = wor.word;
wordFreqs[i] = wor.freq;
}
}
//将排序结果输出
public void printResult() {
System.out.println("Total "words.length" different words in the content!");
for(int i = 0; iwords.length; i){
System.out.println(wordFreqs[i]""words[i]);
}
}
//测试类的功能
public static void main(String[] args) {
Article a = new Article();
a.splitWord();
a.countWordFreq();
a.sort();
a.printResult();
}
}
-----------------------
Total 99 different words in the content!
5and
4the
4i
4a
3as
2with
2who
2to
2time
2sverak
2son
2s
2old
2of
2it
2in
2his
2czech
1zdenek
1year
1wrote
1writing
1won
1whining
1while
1wanted
1walked
1ve
1values
1though
1this
1these
1that
1than
1taking
1subtitles
1spend
1some
1so
1seen
1script
1saw
1russian
1richest
1remain
1rather
1production
1plays
1oscar
1one
1not
1more
1m
1likely
1life
1language
1kolya
1jan
1is
1increasingly
1impacted
1if
1higher
1high
1he
1golden
1globe
1foreign
1for
1five
1finds
1films
1film
1father
1english
1ends
1dramas
1directed
1delight
1days
1couple
1confirmed
1comparable
1characters
1cellist
1cause
1care
1by
1boy
1best
1bachelor
1away
1are
1an
1american
1also
1after
1acting
1abruptly
【JAVA语言修改的代码 java代码改了不起作用】JAVA语言修改的代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java代码改了不起作用、JAVA语言修改的代码的信息别忘了在本站进行查找喔 。

    推荐阅读