我正在解决一个问题而且我很困难,因为我刚开始学习java.我能理解这一点的任何帮助都会很棒.我必须编写一个包含两个类的程序.主类会从文件中读取,并使用第二类找多么次同样的话已经在文件中被重复,并将它们添加到contans次重复字的字和编号的数组.我对阅读文件部分感到满意.我似乎无法绕过如何从第二个类调用一个方法来将单词添加到数组并增加计数器.到目前为止,这是我的代码,如果你运行它,你会看到我对你有多少错误有多新.
import java.io.*;
public class Words{
public static void main (String [] args)
{
ProcessInput();
System.out.println("\nprogram finished");
}
public static WordList ProcessInput( )
{
BufferedReader inputFile;
String inputLine;
String[] word;
WordList words;
try
{
inputFile=new BufferedReader(new FileReader ("inputFile.txt"));
inputLine = inputFile.readLine();
while (inputLine !=null)
{
word=inputLine.toLowerCase().split(" ");
for (int i=0; i<word.length; i++){
System.out.println (word[i]);
words=addWord(word[i]);
}
inputLine = inputFile.readLine();
}
inputFile.close();
}
catch (IOException ioe)
{
System.out.println (ioe.getMessage());
ioe.printStackTrace ();
}
return words;
}
}
class WordList {
String [] …Run Code Online (Sandbox Code Playgroud) 我试图从ArrayList中获取值.以下是我的代码示例:
public static void main (String [] args){
Car toyota= new Car("Toyota", "$10000", "300"+ "2003");
Car nissan= new Car("Nissan", "$22000", "300"+ "2011");
Car ford= new Car("Ford", "$15000", "350"+ "2010");
ArrayList<Car> cars = new ArrayList<Car>();
cars.add(toyota);
cars.add(nissan);
cars.add(ford);
}
public static void processCar(ArrayList<Car> cars){
// in heare i need a way of getting the total cost of all three cars by calling
// computeCars ()
System.out.println(cars.get());
}
Run Code Online (Sandbox Code Playgroud)
感谢所有的答案,我应该添加更多的代码.在Car类中,我有另一种计算总成本的方法,包括税收.
class Car {
public Car (String name, int price, int, tax, int year){ …Run Code Online (Sandbox Code Playgroud) 过去5个小时我一直在研究这个问题.由于某种原因,我没有得到我正在寻找的结果.该方法应该使用冒泡排序按数量对项目的Arraylist进行排序.不确定我是否犯了错误,但它似乎只对前几个项目进行排序,并且只列出其余部分,而不是按顺序排列.这是代码
public static void bubblesrt(ArrayList<Drinks> list)
{
Drink temp;
if (list.size()>1) // check if the number of orders is larger than 1
{
for (int x=0; x<list.size(); x++) // bubble sort outer loop
{
for (int i=0; i < list.size()-i; i++) {
if (list.get(i).compareTo(list.get(i+1)) > 0)
{
temp = list.get(i);
list.set(i,list.get(i+1) );
list.set(i+1, temp);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是compareTo()方法,它位于Drinks类中
public int compareTo(Drinks z)
{
int res=0;
if (quantity < z.quantity) {res=-1; }
if (quantity > z.quantity){res=1;}
return res;
} …Run Code Online (Sandbox Code Playgroud)