我试图找出某人的年龄.我按照这里给出的答案: 如何用Java计算某人的年龄?
这是我到目前为止:
public void setDOB(String day, String month, String year){
LocalDate birthDate = new LocalDate(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
}
Run Code Online (Sandbox Code Playgroud)
我在声明birthDate变量时遇到错误.我收到以下错误:LocalDate(int,int,int)在LocalDate中具有私有访问权限.我不知道这个错误意味着什么,但我假设它与数据访问有关(例如私人,公共等)
是否可以跳过if语句并在if语句中执行另一个if语句?
if(...){
A code.
}else if(...){
B. Call to C.
}else if(...){
C code.
}else(...){
D code.
}
Run Code Online (Sandbox Code Playgroud)
例如,如果我在包含A的if语句中,并且条件发生变化以便执行B,那么我将如何在B else if语句中调用C WHILST中的代码.
编辑=忘了说我的if语句除了一个返回一个名为Dice []的自定义对象数组.我已经实现了以下解决方案,现在收到返回语句错误.
public Die[] ifA(){
A
}
public void ifB(){
ifC(); ifD();
}
public Die[] ifC(){
C
}
public Die[] ifD(){
D
}
public Die[] roll(){
if(...){ return ifA();
}else if(...){ifC();ifD();
}else if(...){return ifC();
}else(...){return ifD();
}
}
Run Code Online (Sandbox Code Playgroud)
我刚刚在roll()方法结束时收到返回错误.当然我不应该因为我使用了else {}块,因此如果没有执行if/else if语句,则必须运行else,不是吗?
编辑#2 =刚刚找到了没有返回值的方法的解决方法.谢谢你们的意见.让我的代码更整洁,更容易理解!
所以我有一个按钮,按下后,通过使用actionListener调用以下方法:
public Die[] roll(){
Die[] playerRoll = new Die[5];
//code goes in here...
return playerRoll;
}
Run Code Online (Sandbox Code Playgroud)
按下按钮后我想访问返回的playerRoll数组,但我不知道该怎么做.这是我试过的:
Die[] returnedArray = throwButton.addActionListener(new MyActionListener());
Run Code Online (Sandbox Code Playgroud)
但它给了我一个错误的说法 Type mismatch: Cannot convert from void to Die[]
我的整个代码工作包括actionListener中的代码,我只是想访问返回的变量.如果您需要其他代码,我将编辑此帖子.
编辑.我尝试尽可能少地粘贴代码,因为有很多代码所以我尝试粘贴相关的东西:
public class Dice {
static JButton throwButton = new JButton("Throw!");
static JButton scoreTitle = new JButton("Score!");
static JLabel playerRoll1 = new JLabel();
static JLabel playerRoll2 = new JLabel();
static JLabel playerRoll3 = new JLabel();
Die[] playerCurrentRoll;
boolean playerRolled = false;
static JButton throwButton = new JButton("Throw!");
frame.getContentPane().add(scoreTitle); …Run Code Online (Sandbox Code Playgroud) 我试图让我的程序从左到右评估一个表达式,而忽略操作的顺序。
例如5/3 + 2 * 3 = 12
除法应四舍五入到最接近的整数。在此示例中,由于5/3 = 1.666(向上舍入为2),因此5/3应等于2。
我制作了一个函数,可以计算表达式,并且可以完美地工作到表达式涉及除法为止。无法正确舍入数字。这是它在我的代码中计算格的方式:
if (runningTotal % numbers.get(i + 1) >= 5) {
runningTotal = (int) Math.ceil(runningTotal / numbers.get(i + 1));
} else {
runningTotal = (int) Math.floor(runningTotal / numbers.get(i + 1));
}
Run Code Online (Sandbox Code Playgroud)
runningTotal是一个整数,数字是一个包含整数的数组列表。我的演员表出问题了吗?
感谢您的帮助。
编辑=我自己想通了。忘记了Math.round()...