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) 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) Haskell新手.为什么下面的代码不工作?我如何连接这样的尾部和头部?
func :: [String] -> [String]
func x = tail x:head x
Run Code Online (Sandbox Code Playgroud) 我在理解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.
当我运行程序时,最小的数字总是为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) 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 ×5
constructor ×1
file ×1
file-io ×1
haskell ×1
overriding ×1
parameters ×1
random ×1
return ×1