[英]Conditional Expressions: Error message saying incomplete types
I can't seem to figure this out.我似乎无法弄清楚这一点。 I feel like it so easy that I am missing it.我觉得这很容易,以至于我想念它。 I keep getting an error message saying incomplete types and pointing at the?我不断收到一条错误消息,指出类型不完整并指向? What am I doing wrong?我究竟做错了什么?
Using a conditional expression, write a statement that increments numUsers
if updateDirection
is 1, otherwise decrements numUsers
.使用条件表达式,编写一个语句,如果updateDirection
为 1,则增加numUsers
,否则减少numUsers
。 Ex: if numUsers
is 8 and updateDirection
is 1, numUsers
becomes 9;例如:如果numUsers
为 8, updateDirection
为 1, numUsers
变为 9; if updateDirection
is 0, numUsers
becomes 7.如果updateDirection
为 0, numUsers
变为 7。
Hint: Start with " numUsers =...
".提示:以“ numUsers =...
”开头。
import java.util.Scanner;
public class UpdateNumberOfUsers {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
int numUsers;
int updateDirection;
numUsers = scnr.nextInt();
updateDirection = scnr.nextInt();
String condStr;
condStr = (numUsers < 8) ? +1 : -1;
System.out.println("New value is: " + numUsers);
}
}
I give you only guidlines and not the complete answer as it looks like an exercice and it's better to do it by yourself.我只给你指导而不是完整的答案,因为它看起来像一个练习,最好自己做。
Try to write the same line without a ternary operator:尝试在没有三元运算符的情况下编写同一行:
if (numUsers < 8) {
condStr = +1;
} else {
condStr = -1;
}
This code:这段代码:
numUsers
从不更新numUsers
的值updateDirection
不使用updateDirection
的值The increment/decrement operators:递增/递减运算符:
myVar++;
myVar--;
The equivalent (Note the order of the operators):等效项(注意运算符的顺序):
myVar += 1;
myVar -= 1;
The complete form:完整的表格:
myVar = myVar + 1;
myVar = myVar - 1;
Now you need to use those operators to increment/decrement numUsers
(so numUsers++;
or numUsers--;
) according to the value updateDirection
(the variable you need to use the if
condition or in the ternary condition).现在您需要使用这些运算符根据值updateDirection
(您需要使用if
条件或三元条件的变量)来增加/减少numUsers
(所以numUsers++;
或numUsers--;
)。 There is no need for condStr
in your code.您的代码中不需要condStr
。
Thank you for the help and advice you both have given me.谢谢你们给我的帮助和建议。 I was able to complete the assigment.我能够完成任务。 This was the input I put in and everythign outputed perfectly.这是我输入的输入,并且所有输出都完美。 Thank you jhamon for not giving me the answer and making me use my brain and the guides you provided.感谢 jhamon 没有给我答案,让我使用我的大脑和你提供的指南。
if (updateDirection >= 1) {
numUsers += 1;
}
else {
numUsers -= 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.