小编Era*_*ran的帖子

读取文本文件时无限循环

我想从文本文件中读取一些数据.这是文本文件中数据的格式:

A,20, ,0
B,30, ,0
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

public ArrayList rechercherSalle() 
{
    String nom;
    String ligne;
    ArrayList<Salle> listeSalles = new ArrayList<Salle>();
    Salle salle = new Salle();
    try {
        InputStream flux = new FileInputStream("salle.txt");
        InputStreamReader lecture = new InputStreamReader(flux);
        BufferedReader buff = new BufferedReader(lecture);
        ligne = buff.readLine();
        while (ligne != null) {
            String[] objetSalle = ligne.split(",");
            nom = objetSalle[0];
            String capacite_maxString = objetSalle[1];
            Integer capacite_max = Integer.parseInt(capacite_maxString);
            String capacite_actuelleString = objetSalle[3];
            Integer capacite_actuelle = Integer.parseInt(capacite_actuelleString);
            String proprietaire = objetSalle[2];
            salle = new Salle(); …
Run Code Online (Sandbox Code Playgroud)

java buffer inputstream outputstream

-1
推荐指数
1
解决办法
126
查看次数

如何用Java中的子类方法覆盖父类的方法?

我有一个任务:

修改类汽车,使其覆盖有它自己版本的方法setCapacity其输出的消息"无法改变汽车的能力",不改变发动机的能力.

我试图解决下面代码的任务,但它继续使用Vehicle类的setCapacity方法而不是Car方法.

class Vehicle // base class
{  
    int capacity;
    String make;

    Vehicle(int theCapacity, String theMake)
    {
        capacity = theCapacity;
        make = theMake;
    }

    void print() 
    {
        System.out.println("Vehicle Info:");
        System.out.println("  capacity = " + capacity + "cc" );
        System.out.println("  make = " + make );
    }

    public void setCapacity(int newCapacity)
    {
        capacity = newCapacity;
        System.out.println("New capacity = " + capacity);
    }
}

class Car extends Vehicle 
{
    String type, model;

    Car(int theCapacity, String …
Run Code Online (Sandbox Code Playgroud)

java overriding

-1
推荐指数
1
解决办法
127
查看次数

在Java中,运算符优先级、递增和递减,为什么不执行第一个println语句?

class Example{
    public static void main(String args[]){
        int x=99;
        if(x++==x){
            System.out.println("x++==x : "+x); //Why this code line is not run?
        }
        if(++x==x ){
            System.out.println("++x==x : "+x); 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么第一条语句没有println执行?

java

-1
推荐指数
2
解决办法
91
查看次数

关键字"this"在TextView中的含义是什么textView = new TextView(this);

关键字"this"TextView中含义是 什么textView = new TextView(this);

下面是引用片段

public class DisplayMessageActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_display_message);
        Intent intent = getIntent();
        String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);
        setContentView(textView);
    }
    ....
Run Code Online (Sandbox Code Playgroud)

我的猜测是初始化对象textView但是用什么?

java android

-2
推荐指数
1
解决办法
179
查看次数

如何使用在方法中的构造函数中初始化的数组?

我正在尝试定义一个用户定义的元素数量(度)的数组,然后是一个允许用户一次设置一个元素(系数)的方法.

class Polynomial 
{

    public Polynomial(int degree)
    {
        double[] coef = new double[degree];
    }

    public double[] setCoefficient(int index, double value) 
    {
        coef[index] = value; //coef was set in the constructor
        return coef;
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到了编译错误coef[index] = value;.

java arrays methods constructor

-2
推荐指数
1
解决办法
103
查看次数

使用Java 8 IntStream的沙漏求和

我试图解决矩阵中最大沙漏数

给定2D矩阵,任务是找到一个沙漏的最大和。

沙漏杯由以下形式的7个电池组成。

A B C
  D
E F G
Run Code Online (Sandbox Code Playgroud)

我的实施出现问题,我得到了奇怪的答案。我应该得到19,但返回36。

这是我的代码:

static int hourglassSums(int[][] arr)
{
    return IntStream.range(0, 4).map(x -> {
        return IntStream.range(0, 4).map(y ->
                  arr[y][x] + arr[y][x + 1] + arr[y][x + 2] 
                  + arr[y + 1][x + 1] 
                  + arr[y + 2][x] + arr[y + 2][x + 1] + arr[y + 2][x + 2]
        ).sum();
    }).max().getAsInt();
}

public static void main(String[] args) 
{
    System.out.println("Max sum :: " + hourglassSums(new int[][] {
                    {1, 1, …
Run Code Online (Sandbox Code Playgroud)

java arrays java-8

-2
推荐指数
1
解决办法
150
查看次数

为什么在Bifunction接口Java 8中省略了compose和identity方法?

我刚刚开始阅读Java 8概念,而我却发现一些不足以说服我的东西是Interface中缺少composeidentity方法BiFunction。就我从Java 8文档中读取的内容来看,我Bifunction似乎与Functioninterface 相同,期望它需要一个附加参数,因此在这种情况下Bifunction应该具有与Functioninterface相同的所有功能,但事实并非如此。

因此,有人可以帮助我找到省略这些方法的原因吗?

java java-8

-2
推荐指数
1
解决办法
63
查看次数

虽然Loop不会终止?

我正在使用while循环,它应该在它应该时终止.如果它正常工作,那么当randno它是== highbound或时它会终止== lowbound.

循环代码:

do {
    do {
        randno = (int) (Math.round((Math.random()*(4)) + 0.5)-1);
        direction = getDirection(randno,heading);      
    } while (robot.look(direction)==IRobot.WALL);
    System.out.println(randno);
    System.out.println(highbound);
    System.out.println(lowbound);
    System.out.println("---------------");
} while (randno!=lowbound | randno!=highbound);
Run Code Online (Sandbox Code Playgroud)

输出是3 3 2 ------,或者2 3 2 ------,因此循环应该结束.第一个循环正确结束(我嵌入它们试图让它工作......).出了什么问题?

java do-while

-3
推荐指数
1
解决办法
330
查看次数

这段代码的重要特征是什么?

有人可以帮我找到这个代码的大O吗?我已经尝试过计算它,但我只是不明白怎么做.

int n = //user input
for (int i = 0; i < n; i++){
    for (int j = 1; j < n; j = j * 2){
        System.out.println(i * j);
    }
}
Run Code Online (Sandbox Code Playgroud)

java sorting methods big-o nested-loops

-4
推荐指数
1
解决办法
76
查看次数

赋值的左侧必须是变量

对于以下方法:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    if(v.getId() != R.id.listViewToDo) {
        return;
    }

    menu.setHeaderTitle("What will you like to do?");
    String[] options = ( "Delete Task", "Return" );

    for(String option : options) {
        menu.add(option);
    }

}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

赋值的左侧必须是变量.

我该如何解决?

java android

-4
推荐指数
1
解决办法
1953
查看次数