小编Ste*_*ski的帖子

试图编写99瓶啤酒歌曲

public class apples {
    public static void main(String[] args) {
        int beerNum = 99;
        String word = "bottles";

        while (beerNum > 0) {

            if (beerNum == 1) {
                word = "bottle"; // ONE bottle
            }

            System.out.println(beerNum + " " + word + " of beer on the wall, " + beerNum + " " + word + " of beer");
            beerNum = beerNum - 1;

            if (beerNum > 0) {
                System.out.println("Take one down, pass it round " + beerNum + …
Run Code Online (Sandbox Code Playgroud)

java

7
推荐指数
2
解决办法
8776
查看次数

如何覆盖方法使其仅返回数字2?(JAVA)

import java.util.Random;

public class Dice {
private int numOfSides;
private int[] sideValues = new int[getNumOfSides()];


public Dice(int numOfSides) {
    this.setNumOfSides(numOfSides);


    for(int i = 0; i < this.sideValues.length; i++) {
        sideValues[i] = i + 1;

    }
}
    public int Roll() {
        return (new Random()).nextInt(getNumOfSides()) + 1;


    }
    public int getNumOfSides() {
        return numOfSides;
    }
    public void setNumOfSides(int numOfSides) {
        this.numOfSides = numOfSides;
    }

}

import java.util.Random;

public class CheatDice extends Dice {



public CheatDice(int numOfSides) {
    super(numOfSides);

}

public int Roll() …
Run Code Online (Sandbox Code Playgroud)

java overriding

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

为什么不'尾x:头x'工作?

Haskell新手.为什么下面的代码工作?我如何连接这样的尾部和头部?

func :: [String] -> [String]
func x = tail x:head x
Run Code Online (Sandbox Code Playgroud)

haskell

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

需要使这个返回声明工作,如何?

我在理解return语句时遇到了麻烦.

public class apples {

    public static void main(String[] args) {

        public static double slope(int x1, int y1, int x2, int y2) {

            double dy = y2 -y1;
            double dx = x2 - x1;
            return dy / dx;
        }

        slope(5, 11, 1, 3);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要归还的总和dy / dx.

java parameters return

0
推荐指数
1
解决办法
69
查看次数

为什么从我的文件中读取的最小数字始终为0?(JAVA)

当我运行程序时,最小的数字总是为0.这是为什么?最大和平均值似乎是正确的.

问题最可能在于我如何使用随机类.

import java.io.*;
import java.util.*;
import java.util.Arrays;

public class ReadAndWrite {
public static void main(String[] args) {
    Random ran = new Random();
    int i_data = 0;
    int[] sort = new int[100];
    File file = new File("Test4");
    int total = 0;
    int average = 0;    

    try {
    file.createNewFile();
    }       
    catch(Exception e) {
        System.out.println("Could not create the file for some reason. Try again.");
        System.exit(0);
    }

    try {
        FileOutputStream fos = new FileOutputStream("Test4");
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        for(int i …
Run Code Online (Sandbox Code Playgroud)

java random file-io file

0
推荐指数
1
解决办法
52
查看次数

不太明白我们如何在这个程序中传递对象作为参数(Java)

public class PassingObjects {

    double height;
    double width;
    double length;

    public PassingObjects(double height, double length, double width) {
        this.height = height;
        this.width = width;
        this.length = length;
    }

    public PassingObjects(PassingObjects obj) {
        height = obj.height;
        length = obj.length;
        width = obj.width;
     }

    public void calculateVolume() {
        System.out.println(this.length * this.width * this.height);
  }

    public static void main(String[] args) {
         PassingObjects obj = new PassingObjects(30, 40, 10);
         PassingObjects obj2 = new PassingObjects(obj);

         obj.calculateVolume();
         obj2.calculateVolume();


  }

}
Run Code Online (Sandbox Code Playgroud)

当我calculateVolume()从两个对象运行方法时,为什么我得到相同的值?

我不明白如何传入obj构造函数参数允许编译器知道我希望30,40和10作为参数传入.

java constructor

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

标签 统计

java ×5

constructor ×1

file ×1

file-io ×1

haskell ×1

overriding ×1

parameters ×1

random ×1

return ×1