我在使用Java时遇到了一些麻烦(仍然在学习)。我正在尝试制作一个计算学生人数的程序,然后计算注册所需的平板电脑数量,但我没有快速到达任何地方。我尝试过使用布尔运算符,但我只是遇到了语法错误。
我正在努力解决的实际问题是:
如果学生人数不超过40人,则只需要一台平板电脑。如果数量超过40,则每增加30名学生需要更多的平板电脑。因此,一个41-70名学生需要2个平板电脑,70-100个课程需要3个,依此类推。
您的输出应包括所需的平板电脑数量和班级中的学生人数:
这不是我的预期,我很困难,任何帮助都会非常感激!
import javax.swing.JOptionPane;
public class Students {
public static void main(String[] args) {
int numberOfStudents;
int tablets = 1;
numberOfStudents=Integer.parseInt(JOptionPane.showInputDialog("Enter the number of students in the class: "));
if (numberOfStudents <= 40){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + " tablet for the e-register");
else if (numberOfStudents => 41 || <= 70){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + 1 + " tablets for the e-register");
else if (numberOfStudents => 71 || <= 100){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + 2 + " tablets for the e-register");
else if (numberOfStudents => 101 || <= 120){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + 3 + " tablets for the e-register");
{ }
}
}
答案 0 :(得分:2)
您必须同时考虑条件,并再次键入var名称。另外,在其他if语句的开头之前放置一个结束括号(}
)。 Alos,你最后不需要{ }
。这是正确的代码。
if (numberOfStudents <= 40){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + " tablet for the e-register");
}
else if (numberOfStudents >= 41 && numberOfStudents <= 70){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + (tablets + 1) + " tablets for the e-register");
}
else if (numberOfStudents >= 71 && numberOfStudents <= 100){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + (tablets + 2) + " tablets for the e-register");
}
else if (numberOfStudents >= 101 && numberOfStudents <= 120){
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + (tablets + 3) + " tablets for the e-register");
}
答案 1 :(得分:2)
是否缺少近距离?
if (numberOfStudents <= 40) {
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + " tablet for the e-register");
} else if (numberOfStudents >= 41 && numberOfStudents <= 70) {
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + 1 + " tablets for the e-register");
} else if (numberOfStudents >= 71 && numberOfStudents <= 100) {
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + 2 + " tablets for the e-register");
} else if (numberOfStudents >= 101 && numberOfStudents <= 120) {
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + 3 + " tablets for the e-register");
}
更新:请注意:
<=
代替=<
numberOfStudents >= 41 || numberOfStudents <= 70
代替numberOfStudents >= 41 || <= 70
&&
代替||
答案 2 :(得分:2)
这里最大的问题是你正在严重解决问题。是的,我可以帮助您解决布尔运算符的问题,但是您打算输出几乎相同的if
语句以允许越来越多的学生?一旦你完成了其中的20个,如果你想改变它打印出来会发生什么?#34;你需要为X学生提供Y平板电脑。&#34;?您必须经历所有打印过邮件并更改邮件的地方。或者,如果有30,000名学生且您的代码仅包含允许1,000的if
条件会发生什么?
这是你的计划应该做什么,而且如果你不得不写一个标志贴在墙上,它就是你在现实生活中所做的事情:
首先让我们看一下规则:
如果学生人数不超过40人,则只需要一台平板电脑。如果数量超过40,则每增加30名学生需要更多的平板电脑。因此,一个41-70名学生需要2个平板电脑,70-100个课程需要3个,依此类推。
首先,这是不明确的。 40片或更少需要一片:很好。然后它表示超过40岁,那么除了下一个例子外,每增加30名学生需要更多的平板电脑,这是有意义的。它说,对于41名学生,需要2片。然而,41不是&#34;另外30&#34; 40以上。它是上面的一个。似乎规则确实是#34; 如果数量超过40,则需要额外的平板电脑,并且每增加30名学生需要额外的平板电脑&#34;。最后,它说41-70名学生需要2片,而70-100名学生需要3片。呃,所以 70名学生需要多少个平板电脑? 2或3?
我认为我们必须假设,因为41是我们需要2片的重点,71就是我们达到3片的地步。
现在让我们看看所有代码。这一行可以为您提供学生人数:
numberOfStudents=Integer.parseInt(
JOptionPane.showInputDialog("Enter the number of students in the class: "));
没关系。现在,使用数学让计算机决定我们需要多少平板电脑,而不是放入所有这些ifs?计算机相当擅长数学。
我们知道我们总是需要一台平板电脑,对吧?因为&#34; 40或更少&#34;学生需要一台平板电脑所以我们可以在那里开始:
int tablets = 1;
现在我们可以做下一步了:如果有超过40名学生,那么我们还有一个额外的平板电脑,另外还有一个额外的平板电脑,每30个学生超过原来的40个。喜欢这个:
if(numberOfStudents > 40) {
tablets += 1 + (numberOfStudents - 41) / 30;
}
现在我们知道需要多少平板电脑,我们可以告诉用户。你已经完成了这一点 - 但现在你只需要做一次。
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + " tablet for the e-register");
这样做的另一个好处是,现在您的tablet
变量可以保存您实际需要的平板电脑数量。因此,如果您要尽快再次使用该价值,那就已经准备好了。在您最初发布的代码中,tablets
变量始终只包含1
。它也可能是一个常数。
通常,硬编码的数字40和30将变成在代码顶部定义的常量。这将使未来的代码读者更容易理解这些数字。
编辑:只是为了显示所有代码的内容:
numberOfStudents=Integer.parseInt(
JOptionPane.showInputDialog("Enter the number of students in the class: "));
int tablets = 1;
if(numberOfStudents > 40) {
tablets += 1 + (numberOfStudents - 41) / 30;
}
System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + " tablet for the e-register");
答案 3 :(得分:1)
您的代码中有3个问题..
&&
代替||
>=
代替=>
)您的if控件结构应如下所示..
if (numberOfStudents >= 40){
}
else if (numberOfStudents >= 41 && <= 70){
}
else if (numberOfStudents >= 71 && <= 100){
}
else if (numberOfStudents >= 101 && <= 120){
}
答案 4 :(得分:1)
1)你的条件很差:
else if (numberOfStudents => 41 || <= 70){
//code
}
应该是(你需要确切的间隔):
else if (numberOfStudents => 41 && numberOfStudents <= 70){
//code
}
2)你的支架也很少垃圾
if(condition){
//code condition match
}else if(cond2){
//code condition match 2
}
顺便说一下:
import javax.swing.JOptionPane;
public class Students {
public static void main(String[] args) {
int numberOfStudents;
int tablets = 1;
int limitForOne = 40;
int limitForEveryNext = 30;
numberOfStudents=Integer.parseInt(JOptionPane.showInputDialog("Enter the number of students in the class: "));
System.out.println("There are a total of " + numberOfStudents + " students in this class");
if (numberOfStudents >limitForOne){
//get how much more we need
int diffCount = (numberOfStudents-limitForOne)/limitForEveryNext;
//currenct count is above limit for one, but diff is smaller than limitForEveryNext (in fact we need 1 more)
//maybe diffCount=diffCount+1; instead if, not sure
if(diffCount == 0){
diffCount=1;
}
tablets= tablets+diffCount;
}
System.out.println("You will need " + tablets + " tablet(s) for the e-register");
//end main
}
//end class
}
也应该有效:)