我正在关注"Java的艺术与科学"一书,它展示了如何计算闰年.本书使用ACM Java Task Force的库.
这是书籍使用的代码:
import acm.program.*;
public class LeapYear extends ConsoleProgram {
public void run()
{
println("This program calculates leap year.");
int year = readInt("Enter the year: ");
boolean isLeapYear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
if (isLeapYear)
{
println(year + " is a leap year.");
} else
println(year + " is not a leap year.");
}
}
Run Code Online (Sandbox Code Playgroud)
现在,这就是我计算闰年的方法.
import acm.program.*;
public class LeapYear extends ConsoleProgram { …Run Code Online (Sandbox Code Playgroud) 我正在使用ACM库在Java中编写一个简单的战舰游戏.游戏开始后,应该将船只放在画布上的随机位置,但问题是船只可能会互相覆盖,这在游戏中是不允许的.如何避免将船放在彼此之上?
我的代码是:
private void putSmallShips() {
for (int i = 0; i < SMALL_SHIP_QUANTITY; i++){
smallShip = new GRect(SMALL_SHIP_WIDTH, SHIP_HEIGHT);
int x = rgen.nextInt(10, 510);
int y = rgen.nextInt(10, 510);
while (true){
gobj = getElementAt(x, y);
if (gobj == null) break;
x = rgen.nextInt(10, 510);
y = rgen.nextInt(10, 510);
}
smallShip.setLocation(x, y);
add(smallShip);
}
}
private void putMiddleShips() {
for (int i = 0; i < MIDDLE_SHIP_QUANTITY; i++){
middleShip = new GRect(MIDDLE_SHIP_WIDTH, SHIP_HEIGHT);
int x = rgen.nextInt(10, 530);
int y …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Eclipse中编写一个java应用程序.
我真的想要使用ACM.Program包,但是,我的Eclipse副本没有安装它!
我看了整个网络,我找不到ACM包的单一下载.
更多信息:每当我尝试代码时:
package helloGeiodo;
import acm.program.*;
public class Add2 extends Program {
public void run() {
println("This program adds two numbers.");
int n1 = readInt("Enter n1: ");
int n2 = readInt("Enter n2: ");
int total = n1 + n2;
println("The total is " + total + ".");
}
}
Run Code Online (Sandbox Code Playgroud)
我在ying-yang上下错误,都暗示没有"acm.program"这样的东西.
无论如何,我需要知道在哪里可以找到ACM包,以及如何安装它.
谢谢!
--Flynn
下面的代码以Caesar的方式加密单词或句子.你把移位值和程序取出单词/句子的每个字母,并根据移位(键)值在字母表中"移动".但这不是问题.我在互联网上找到了代码,我无法解释其中的一些内容.我知道它是如何工作的,但我需要一些关于它的一些特定答案.这是代码:
import acm.program.*;
public class CaesarCipher extends ConsoleProgram {
public void run() {
println("This program implements a Caesar cipher.");
int key = readInt("Character positions to shift: ");
String plaintext = readLine("Enter a message: ");
String ciphertext = encodeCaesarCipher(plaintext, key);
println("Encoded message: " + ciphertext);
}
private String encodeCaesarCipher(String str, int key) {
if (key < 0) key = 26 - (-key % 26);
String result = "";
for (int i = 0; i < str.length(); i++) {
char ch = …Run Code Online (Sandbox Code Playgroud) 到目前为止我的代码是:
package graphics;
import acm.graphics.*;
import acm.program.*;
public class project1 {
public class graphics extends GraphicsProgram {
private static final long serialVersionUID = 1L;
public void run() {
add( new GLabel( "hello, world", 100, 75));
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
线程"main"中的异常acm.util.ErrorException:无法确定主类.at acm.program.Program.main(Program.java:1358)
我已经通过在线参考获得了这一点,除了我在运行配置中在我自己的帐户上做的两个修改,acm.program.Program在主选项卡中设置为主类,并且还设置code=acm.program.Program为程序参数,不确定这是否相关或不.
我正在尝试编写一组 for 循环,这些循环会产生以下一系列数字。我试图让我的循环在同一行上打印每个系列,每个术语之间有空格。我是 Java 新手,对如何完成它感到非常困惑。右侧是我增加计数的数字。
1. 4 5 6 7 8 9 10 (+1)
2. 6 5 4 3 2 1 (-1)
3. 2 4 6 8 10 12 14 16 (+2)
4. 19 17 15 13 11 9 7 5 (-2)
5. 7 15 23 31 39 (+8)
6. 2 4 8 16 32 64 (*2)
Run Code Online (Sandbox Code Playgroud)
这是我尝试完成它的方式的代码。我得到了第一行工作,但我想知道天气有一种简单的方法可以创建其余部分而无需重新复制程序。
import acm.program.*;
public class ppLoop extends ConsoleProgram {
public void run()
{
{
for(int row = 1; row < 2; row++)
{ …Run Code Online (Sandbox Code Playgroud) 我正在阅读 GObject /acm.graphics 的 Java 文档。
我在看这个方法:
move(double dx, double dy)
Run Code Online (Sandbox Code Playgroud)
使用位移 dx 和 dy 在屏幕上移动对象。
我想了解“d”代表什么以及 (dx,dy) 与 (x,y) 之间有什么区别?有什么区别还是没有区别,只是相同坐标的不同名称?
这是代码:
import acm.program.*;
public class test extends GraphicsProgram{
public test(){
println(getHeight());
}
public void run(){
println(getHeight());
}
}
Run Code Online (Sandbox Code Playgroud)
执行结果是0 472.为什么getHeight()在构造函数中返回0,而run()返回472,这是真正的值?